Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
7+ Taskbar Tweaker portable launcher source code
#include "stdafx.h"
#include "buffer.h"
#define NULCHAR TEXT('\0')
#define SPACECHAR TEXT(' ')
#define TABCHAR TEXT('\t')
#define DQUOTECHAR TEXT('\"')
#define SLASHCHAR TEXT('\\')
#define EXEC_32_PATH L"bin\\32\\7+ Taskbar Tweaker.ex2"
#define EXEC_64_PATH L"bin\\64\\7+ Taskbar Tweaker.ex2"
UINT MakeThisFilePath(WCHAR *pThisFilePath);
UINT MakeExecFilePath(WCHAR *pThisFilePath, WCHAR *pExecFilePath);
UINT MakeIniFilePath(WCHAR *pThisFilePath, WCHAR *pIniFilePath);
BOOL GetTempFilePath(TCHAR *pPrefixString, TCHAR *pTempFilePath);
TCHAR *GetCmdLineArgs(TCHAR *cmdstart);
BOOL SimpleCreateProcess(WCHAR *pApplicationName, WCHAR *pCommandLine, BOOL bWait);
void main()
{
WCHAR szThisFilePath[MAX_PATH];
UINT uThisFilePathLen;
WCHAR szExecFilePath[MAX_PATH];
UINT uExecFilePathLen;
WCHAR szIniFilePath[MAX_PATH];
UINT uIniFilePathLen;
WCHAR *pCmdLineArgs;
UINT uCmdLineArgsLen;
WCHAR *pExecCmdLine;
UINT uExecCmdLineLen;
if(
(uThisFilePathLen = MakeThisFilePath(szThisFilePath)) != 0 &&
(uExecFilePathLen = MakeExecFilePath(szThisFilePath, szExecFilePath)) != 0 &&
(uIniFilePathLen = MakeIniFilePath(szThisFilePath, szIniFilePath)) != 0
)
{
pCmdLineArgs = GetCmdLineArgs(GetCommandLine());
uCmdLineArgsLen = lstrlen(pCmdLineArgs);
uExecCmdLineLen = 1+uExecFilePathLen+1;
uExecCmdLineLen += (sizeof(" -launcher ")-1)+1+uThisFilePathLen+1;
uExecCmdLineLen += (sizeof(" -ini ")-1)+1+uIniFilePathLen+1;
if(uCmdLineArgsLen > 0)
uExecCmdLineLen += (sizeof(" ")-1)+uCmdLineArgsLen;
pExecCmdLine = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (uExecCmdLineLen+1)*sizeof(WCHAR));
if(pExecCmdLine)
{
pExecCmdLine[0] = L'"';
lstrcpy(pExecCmdLine+1, szExecFilePath);
lstrcat(pExecCmdLine, L"\" -launcher \"");
lstrcat(pExecCmdLine, szThisFilePath);
lstrcat(pExecCmdLine, L"\" -ini \"");
lstrcat(pExecCmdLine, szIniFilePath);
if(uCmdLineArgsLen > 0)
{
lstrcat(pExecCmdLine, L"\" ");
lstrcat(pExecCmdLine, pCmdLineArgs);
}
else
lstrcat(pExecCmdLine, L"\"");
if(!SimpleCreateProcess(szExecFilePath, pExecCmdLine, FALSE))
MessageBox(NULL, L"Process creation failed", NULL, MB_ICONERROR);
HeapFree(GetProcessHeap(), 0, pExecCmdLine);
}
else
MessageBox(NULL, L"Allocation failed", NULL, MB_ICONERROR);
}
else
MessageBox(NULL, L"Some path manipulation error occurred", NULL, MB_ICONERROR);
ExitProcess(0);
}
UINT MakeThisFilePath(WCHAR *pThisFilePath)
{
UINT uStringLen;
uStringLen = GetModuleFileName(NULL, pThisFilePath, MAX_PATH);
if(uStringLen == 0 || uStringLen == MAX_PATH)
return 0;
return uStringLen;
}
UINT MakeExecFilePath(WCHAR *pThisFilePath, WCHAR *pExecFilePath)
{
WCHAR *pExecRelativePath;
UINT uExecRelativePathLen;
BOOL bWow64Process;
UINT uStringLen;
if(IsWow64Process(GetCurrentProcess(), &bWow64Process) && bWow64Process)
{
pExecRelativePath = EXEC_64_PATH;
uExecRelativePathLen = sizeof(EXEC_64_PATH)/sizeof(WCHAR)-1;
}
else
{
pExecRelativePath = EXEC_32_PATH;
uExecRelativePathLen = sizeof(EXEC_32_PATH)/sizeof(WCHAR)-1;
}
lstrcpy(pExecFilePath, pThisFilePath);
uStringLen = lstrlen(pExecFilePath);
do {
uStringLen--;
} while(uStringLen > 0 && pExecFilePath[uStringLen] != L'\\');
if(uStringLen == 0)
return 0;
uStringLen++;
if(uStringLen + uExecRelativePathLen > MAX_PATH-1)
return 0;
lstrcpy(pExecFilePath+uStringLen, pExecRelativePath);
return uStringLen + uExecRelativePathLen;
}
UINT MakeIniFilePath(WCHAR *pThisFilePath, WCHAR *pIniFilePath)
{
UINT uStringLen;
lstrcpy(pIniFilePath, pThisFilePath);
uStringLen = lstrlen(pIniFilePath);
if(uStringLen > 4 && lstrcmpi(&pIniFilePath[uStringLen-4], L".exe") == 0)
{
lstrcpy(&pIniFilePath[uStringLen-4], L".ini");
return uStringLen;
}
else if(uStringLen <= MAX_PATH-1-4)
{
lstrcat(pIniFilePath, L".ini");
return uStringLen + 4;
}
else if(GetTempFilePath(L"7tt", pIniFilePath))
return lstrlen(pIniFilePath);
else
return 0;
}
BOOL GetTempFilePath(TCHAR *pPrefixString, TCHAR *pTempFilePath)
{
WCHAR szTempPath[MAX_PATH];
UINT uStringLen;
uStringLen = GetTempPath(MAX_PATH, szTempPath);
if(uStringLen == 0 || uStringLen > MAX_PATH-1)
return 0;
return (GetTempFileName(szTempPath, pPrefixString, 0, pTempFilePath) != 0);
}
TCHAR *GetCmdLineArgs(TCHAR *cmdstart)
{
TCHAR *p;
TCHAR c;
int inquote; /* 1 = inside quotes */
/* first scan the program name, copy it, and count the bytes */
p = cmdstart;
/* A quoted program name is handled here. The handling is much
simpler than for other arguments. Basically, whatever lies
between the leading double-quote and next one, or a terminal null
character is simply accepted. Fancier handling is not required
because the program name must be a legal NTFS/HPFS file name.
Note that the double-quote characters are not copied, nor do they
contribute to numchars. */
inquote = FALSE;
do {
if (*p == DQUOTECHAR )
{
inquote = !inquote;
c = *p++;
continue;
}
c = *p++;
} while ( (c != NULCHAR && (inquote || (c !=SPACECHAR && c != TABCHAR))) );
if ( c == NULCHAR ) {
p--;
}
inquote = 0;
if ( *p ) {
while (*p == SPACECHAR || *p == TABCHAR)
++p;
}
return p;
}
BOOL SimpleCreateProcess(WCHAR *pApplicationName, WCHAR *pCommandLine, BOOL bWait)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
if(!CreateProcess(pApplicationName, pCommandLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
return FALSE;
if(bWait)
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment