小编Ram*_*a B的帖子

CreateProcessAsUser返回错误代码2

我的应用程序中有两个进程.1."myService.exe"这是一个Windows服务.2."myApp.exe",它位于与"myService.exe"相同的目录中.

此过程由"myService.exe"使用CreateProcessAsUser api生成.我们必须使用这个api而不是直接启动进程(使用系统调用),因为我们需要访问当前用户的vpn配置文件.

当我硬编码"myApp.exe"的路径时,它工作正常并且创建了进程,但通过获取"myService.exe"的当前目录获得的相同路径不是创建进程并返回错误代码2(找不到文件) .

我正在使用Visual Studio 2008.该项目是以ASCII模式编译的,而不是下面的代码中的Unicode.我尝试使用Unicode api(最后没有'A').它也没用.

问题不在于获得当前路径.验证该路径不是System32文件夹.

    HANDLE hToken;
LPSTR exePath = GetCommandLineA();
string exePathStr = exePath;
char fileExeChar[256];
strcpy(fileExeChar,exePathStr.c_str());
string serverExe = "myService.exe";
for(unsigned int i=0;i<exePathStr.length()-(serverExe.length() + 1);i++)
{
    fileLocation += fileExeChar[i];// removing the service.exe from the path

}
LPSTR fileLocationLp = const_cast<LPSTR>(fileLocation.c_str());

LPCSTR progName = (LPCSTR)"myapp.exe";

char errStr[100];
DWORD dwCreationFlag = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
STARTUPINFO si;
PROCESS_INFORMATION pi;
int k = WTSQueryUserToken (WTSGetActiveConsoleSessionId (), &hToken);
ZeroMemory( &si, sizeof( STARTUPINFO ) );
si.cb = sizeof( STARTUPINFO …
Run Code Online (Sandbox Code Playgroud)

c++ windows winapi visual-studio

2
推荐指数
1
解决办法
5015
查看次数

标签 统计

c++ ×1

visual-studio ×1

winapi ×1

windows ×1