#include <windows.h>
#include <shlwapi.h>
#define elemof(x) (sizeof(x)/sizeof(*(x)))
#define T(x) TEXT(x)
static TCHAR StartName[MAX_PATH]; // the (Win9x:long!) file name this executable gets called
static TCHAR StartAlias[MAX_PATH]; // the (long) file name for start
// puts a string to console
static void _declspec(noreturn) _cdecl Error(UINT StringId,...) {
TCHAR s[256],t[256];
DWORD l;
HANDLE stderr=GetStdHandle(STD_ERROR_HANDLE);
if (*StartName) {
PTSTR p=StartName; // don't include too much symbols (here: wnsprintf)
l=wvnsprintf(s,elemof(s),T("%s: "),(va_list)&p);
WriteConsole(stderr,s,l,&l,NULL);
}
LoadString(0,StringId,t,elemof(t));
l=wvnsprintf(s,elemof(s)-2,t,(va_list)(&StringId+1));
lstrcpyn(s+l,T("\r\n"),3);
WriteConsole(stderr,s,l+2,&l,NULL);
if (StringId==1) {
l=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,*(PDWORD)(&StringId+1),0,s,elemof(s),NULL);
WriteConsole(stderr,s,l,&l,NULL);
}
ExitProcess(3);
}
// Gets the desired alias for current executable file name
static void GetAlias() {
TCHAR IniName[MAX_PATH];
GetModuleFileName(0,IniName,elemof(IniName));
PTSTR p=PathFindFileName(IniName); // get executable's directory
LoadString(0,3,p,IniName+elemof(IniName)-p); // append "symlink.ini"
PTSTR k=PathFindFileName(StartName); // search for name part only
TCHAR alias[MAX_PATH];
if (GetPrivateProfileString(T("EXE"),k,T(""),alias,elemof(alias),IniName)) {
}else if (GetPrivateProfileString(T("EXE"),T("*"),T(""),alias,elemof(alias),IniName)) {
}else Error(2,k,IniName);
if (alias[0]=='/' || alias[0]=='\\') {
lstrcpyn(IniName+2,alias,elemof(IniName)-2); // add current drive letter
lstrcpyn(alias,IniName,elemof(alias));
}else if (alias[1]!=':') {
lstrcpyn(p,alias,IniName+elemof(IniName)-p); // add executable's directory
PathCanonicalize(alias,IniName);
}
lstrcpyn(StartAlias,alias,elemof(StartAlias));
p=StrChr(StartAlias,'*'); // wildcard character?
if (p) {
int l=StartAlias+elemof(StartAlias)-p; // residual space in buffer
lstrcpyn(p,k,l); // insert file name
StrNCat(p,alias+(p-StartAlias)+1,l); // append trailing characters
}
}
extern "C" void WINAPI mainCRTStartup() {
STARTUPINFO si;
si.cb=sizeof(si);
GetStartupInfo(&si);
PROCESS_INFORMATION pi;
PTSTR cmdline=GetCommandLine();
PTSTR StartArgs=PathGetArgs(cmdline); // the command tail
for (PTSTR p=StartArgs; --p>cmdline;) {
while (*p==' ') *p=0; // delimit executable file name and arguments, remove multiple blanks
}
PathUnquoteSpaces(cmdline);
GetLongPathName(cmdline,StartName,elemof(StartName));
GetAlias();
if (CreateProcess(StartAlias,StartArgs,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)) {
WaitForSingleObject(pi.hProcess,INFINITE);
DWORD ec;
GetExitCodeProcess(pi.hProcess,&ec);
ExitProcess(ec);
}
Error(1,GetLastError(),StartAlias);
}
Detected encoding: ASCII (7 bit) | 2
|