Example #1
0
BOOL ShellFunctions::RunRegistryCommand(HKEY hKey,LPCWSTR szFile)
{
	if (!IsUnicodeSystem())
		return RunRegistryCommand(hKey,W2A(szFile));


	PROCESS_INFORMATION pi;
	STARTUPINFOW si;
	CRegKey CommandKey;
	CStringW ExecuteStr;
	CStringW CommandLine;
	int i;

	if (CommandKey.OpenKey(hKey,"command",CRegKey::openExist|CRegKey::samAll)!=ERROR_SUCCESS)
		return FALSE;
	if (CommandKey.QueryValue(L"",ExecuteStr)<2)
		return FALSE;
	i=ExecuteStr.FindFirst(L'%');
	while (i!=-1)
	{
		if (ExecuteStr[i+1]==L'1')
		{
			CommandLine.Copy(ExecuteStr,i);
			CommandLine<<szFile;
			CommandLine<<((LPCWSTR)ExecuteStr+i+2);
			break;
		}
		i=ExecuteStr.FindNext(L'%',i);
	}
	if (i==-1)
		CommandLine=ExecuteStr;
	if (!ExpandEnvironmentStringsW(CommandLine,ExecuteStr.GetBuffer(1000),1000))
		ExecuteStr.Swap(CommandLine);
	si.cb=sizeof(STARTUPINFOW);
	si.lpReserved=NULL;
	si.cbReserved2=0;
	si.lpReserved2=NULL;
	si.lpDesktop=NULL;
	si.lpTitle=NULL;
	si.dwFlags=STARTF_USESHOWWINDOW;
	si.wShowWindow=SW_SHOWDEFAULT;
	if (!CreateProcessW(NULL,ExecuteStr.GetBuffer(),NULL,
		NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS,
		NULL,NULL,&si,&pi))
	{
		CommandKey.CloseKey();
		return FALSE;
	}
	CloseHandle(pi.hThread);
	CloseHandle(pi.hProcess);
	CommandKey.CloseKey();
	return TRUE;
}