windows DLL函数调用外部exe文件无效

时间:2016-01-07 13:40:52

标签: c++ dll exe

编译环境:Visual Studio 2010

我已编写程序并编译为createFile.exe文件

ofstream file;
file.open("C:\\test.txt", ios::app);
file << "hello test "<< argv[0] << argv[1] << argv[2] <<endl;
file.close();

然后我创建一个dll文件,这是我在dll中调用createFile.exe的函数

if (!CreateProcess(L"C:\\", L"createFile.exe hello test", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
    printf("CreateProcess failed (%d).\n", GetLastError());
    return 0;
}

已创建进程(我可以获取它的processid和threadid),CreateProcess返回成功(0),但test.txt尚未创建。

我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是你应该使用CreateProcess的方式:

STARTUPINFO         siStartupInfo;
PROCESS_INFORMATION piProcessInfo;

memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));

siStartupInfo.cb = sizeof(siStartupInfo);

if(CreateProcess("c:\\winnt\\notepad.exe",       // Application name
                 " example.txt",                 // Additional application arguments
                 NULL,
                 NULL,
                 FALSE,
                 CREATE_DEFAULT_ERROR_MODE,
                 NULL,
                 NULL,
                 &siStartupInfo,
                 &piProcessInfo) == FALSE)
  // Could not start application

// Wait until application has terminated
WaitForSingleObject(piProcessInfo.hProcess, INFINITE);

// Close process and thread handles
::CloseHandle(piProcessInfo.hThread);
::CloseHandle(piProcessInfo.hProcess);

如果函数失败,则返回值为零。要获取扩展错误信息,请调用GetLastError。