#include <windows.h>
#include <ctype.h>
+EXTERN_C HRESULT WINAPI SHCreateSessionKey(REGSAM samDesired, PHKEY phKey);
+
/**
* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
* Returns FALSE if there was an error, or otherwise if all is ok.
/* First, set the current directory to SystemRoot */
TCHAR gen_path[MAX_PATH];
DWORD res;
+ HKEY hSessionKey, hKey;
+ HRESULT hr;
res = GetWindowsDirectory(gen_path, sizeof(gen_path));
return 100;
}
- if (res>=sizeof(gen_path))
+ if (!SetCurrentDirectory(gen_path))
{
- printf("Windows path too long (%ld)\n", res);
+ wprintf(L"Cannot set the dir to %s (%ld)\n", gen_path, GetLastError());
- return 100;
+ return 100;
}
- if (!SetCurrentDirectory(gen_path))
+ hr = SHCreateSessionKey(KEY_WRITE, &hSessionKey);
+ if (SUCCEEDED(hr))
{
- wprintf(L"Cannot set the dir to %s (%ld)\n", gen_path, GetLastError());
+ LONG Error;
+ DWORD dwDisp;
- return 100;
+ Error = RegCreateKeyEx(hSessionKey, L"StartupHasBeenRun", 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
+ RegCloseKey(hSessionKey);
+ if (Error == ERROR_SUCCESS)
+ {
+ RegCloseKey(hKey);
+ if (dwDisp == REG_OPENED_EXISTING_KEY)
+ {
+ /* Startup programs has already been run */
+ return 0;
+ }
+ }
}
- if (argc>1)
+ if (argc > 1)
{
switch(argv[1][0])
{
case 'r': /* Restart */
- ops=SETUP;
+ ops = SETUP;
break;
case 's': /* Full start */
- ops=SESSION_START;
+ ops = SESSION_START;
break;
default:
- ops=DEFAULT;
+ ops = DEFAULT;
break;
}
} else
- ops=DEFAULT;
+ ops = DEFAULT;
/* do not run certain items in Safe Mode */
if(GetSystemMetrics(SM_CLEANBOOT)) ops.startup = FALSE;
/* Perform the ops by order, stopping if one fails, skipping if necessary */
/* Shachar: Sorry for the perl syntax */
- res=(ops.ntonly || !ops.preboot || wininit()) &&
- (ops.w9xonly || !ops.preboot || pendingRename()) &&
- (ops.ntonly || !ops.prelogin ||
- ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICESONCE], TRUE, FALSE)) &&
- (ops.ntonly || !ops.prelogin || !ops.startup ||
- ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICES], FALSE, FALSE)) &&
- (!ops.postlogin ||
- ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNONCE], TRUE, TRUE)) &&
- (!ops.postlogin || !ops.startup ||
- ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUN], FALSE, FALSE)) &&
- (!ops.postlogin || !ops.startup ||
- ProcessRunKeys(HKEY_CURRENT_USER, runkeys_names[RUNKEY_RUN], FALSE, FALSE));
+ res = TRUE;
+ if (res && !ops.ntonly && ops.preboot)
+ res = wininit();
+ if (res && !ops.w9xonly && ops.preboot)
+ res = pendingRename();
+ if (res && !ops.ntonly && ops.prelogin)
+ res = ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICESONCE], TRUE, FALSE);
+ if (res && !ops.ntonly && ops.prelogin && ops.startup)
+ res = ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICES], FALSE, FALSE);
+ if (res && ops.postlogin)
+ res = ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNONCE], TRUE, TRUE);
+ if (res && ops.postlogin && ops.startup)
+ res = ProcessRunKeys(HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUN], FALSE, FALSE);
+ if (res && ops.postlogin && ops.startup)
+ res = ProcessRunKeys(HKEY_CURRENT_USER, runkeys_names[RUNKEY_RUN], FALSE, FALSE);
printf("Operation done\n");
- return res?0:101;
+ return res ? 0 : 101;
}
WINAPI
SHCreateSessionKey(REGSAM samDesired, PHKEY phKey)
{
- static HKEY hSessionKey = NULL;
HRESULT hr = S_OK;
+ static WCHAR wszSessionKey[256];
+ LONG Error;
- if (!hSessionKey)
+ if (!wszSessionKey[0]) // FIXME: Critical Section
{
HANDLE hToken;
if (GetTokenInformation(hToken, TokenStatistics, &Stats, sizeof(Stats), &ReturnLength))
{
- WCHAR wszBuf[256];
- LONG Error;
-
- swprintf(wszBuf,
+ swprintf(wszSessionKey,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%08x%08x",
Stats.AuthenticationId.HighPart, Stats.AuthenticationId.LowPart);
-
- Error = RegCreateKeyExW(HKEY_LOCAL_MACHINE, wszBuf, 0, NULL, REG_OPTION_VOLATILE, samDesired, NULL, &hSessionKey, NULL);
- if (Error != ERROR_SUCCESS)
- hr = HRESULT_FROM_WIN32(Error);
}
else
hr = HRESULT_FROM_WIN32(GetLastError());
hr = HRESULT_FROM_WIN32(GetLastError());
}
- *phKey = hSessionKey;
+ if(SUCCEEDED(hr))
+ {
+ Error = RegCreateKeyExW(HKEY_LOCAL_MACHINE, wszSessionKey, 0, NULL,
+ REG_OPTION_VOLATILE, samDesired, NULL, phKey, NULL);
+ if (Error != ERROR_SUCCESS)
+ hr = HRESULT_FROM_WIN32(Error);
+ }
+
return hr;
}