向各位咨询个问题:有个外部程序无需管理员权限即可正常双击运行(无界面)。但是当system权限的服务程序,降权获取当前用户的令牌去执行该外部程序,当该用户无管理员权限时,被启动的外部程序总是崩溃,崩溃模块ntdll.dll,但是当该用户赋予了管理员权限后,则被启动的程序正常运行,这个问题该如何分析解决?
执行CMD,notepad不会出现异常,该外部程序有联网,读写文件需求。
BOOL GetTokenByName(HANDLE &hToken, LPSTR lpName)
{
if (!lpName)
{
return FALSE;
}
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = { 0 };
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return FALSE;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32))
{
char szExeFile[260];
char szName[260];
do
{
// 将文件名和目标名称转换为大写
strcpy_s(szExeFile, pe32.szExeFile);
strcpy_s(szName, lpName);
_strupr_s(szExeFile, sizeof(szExeFile) - 1);
_strupr_s(szName, sizeof(szName) - 1);
if (strcmp(szExeFile, szName) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID);
if (hProcess)
{
bRet = OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken);
CloseHandle(hProcess);
}
CloseHandle(hProcessSnap);
return bRet;
}
} while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap);
return FALSE;
}
void StartProcessAsCurrentUser(LPCSTR lpImage)
{
HANDLE hToken;
if (!GetTokenByName(hToken, (LPSTR)"EXPLORER.EXE"))
{
return;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
//si.lpDesktop = (LPSTR)TEXT("winsta0\\default");
si.lpDesktop = NULL;
BOOL bResult = CreateProcessAsUser(hToken, NULL, (LPSTR)lpImage, NULL, NULL,
FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
if (bResult)
{
OutputDebugString("CreateProcessAsUser ok!\r");
}
else
{
OutputDebugString("CreateProcessAsUser false!\r");
}
CloseHandle(hToken);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}