我使用带有参数lpCurrentDirectory的createprocess()从我的应用程序创建一个进程,但它不起作用。它给出错误'系统找不到指定的路径'。我的代码是
CreateProcess( "XYZ\\bin\\run.bat", NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, "XYZ\\bin", &siStartupInfo, &piProcessInfo ) ;
我还通过使用GetCurrentDir()获取curent目录并将XYZ\\bin
添加到当前目录,给出了lpCurrentDirectory的完整路径,但它仍然不起作用。
我希望我的批处理文件从lpCurrentDirectory运行。
我不明白createProcess()如何使用lpCurrentDirectory参数。是期望完整路径还是相对路径?
我也尝试使用硬编码完整路径但是,它显示相同的错误。其实我原来的代码是, `
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
printf("\n error...");
}
string path(cCurrentPath);
path += "\\XYZ\\bin";
wstring_convert<std::codecvt_utf8<wchar_t>> converter;
wstring wstringData = converter.from_bytes(path);
TCHAR lpszClientPath[500]= TEXT("XYZ\\bin\\run.bat /y");
if(::CreateProcess(NULL, lpszClientPath, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,NULL, wstringData.c_str(), &si, &pi) > 0)
`。
答案 0 :(得分:0)
请查看this。您应该阅读整个帖子,因为它重申了在先前帖子中给出的答案。
答案 1 :(得分:0)
此代码适用于Windows 7和Windows XP。
#include <Windows.h>
void showcd(wchar_t * caption)
{
wchar_t buffer[512];
if (GetCurrentDirectory(512, buffer) == 0)
{
DWORD err = GetLastError();
MessageBox(NULL, L"GetCurrentDirectory failed", caption, MB_OK);
ExitProcess(err);
}
buffer[511] = L'\0';
MessageBox(NULL, buffer, caption, MB_OK);
}
void parent(wchar_t * cd)
{
wchar_t cmd[512];
wchar_t buffer[512];
STARTUPINFO sinfo;
PROCESS_INFORMATION pinfo;
GetStartupInfo(&sinfo);
showcd(L"Parent Process");
if (GetCurrentDirectory(512, buffer) == 0)
{
DWORD err = GetLastError();
MessageBox(NULL, L"GetCurrentDirectory failed", L"Parent Process", MB_OK);
ExitProcess(err);
}
buffer[511] = L'\0';
wcscat_s(buffer, 512, L"\\");
wcscat_s(buffer, 512, cd);
if (GetModuleFileName(NULL, cmd, 512) == 0)
{
MessageBox(NULL, L"GetModuleFileName failed", L"Parent Process", MB_OK);
ExitProcess(GetLastError());
}
cmd[511] = L'\0';
if (!CreateProcess(
cmd, NULL, NULL, NULL, FALSE, 0, NULL, buffer, &sinfo, &pinfo
))
{
DWORD err = GetLastError();
MessageBox(NULL, L"CreateProcess failed", L"Oops", MB_OK);
ExitProcess(err);
}
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
wchar_t * cmdline;
for (cmdline = GetCommandLine(); *cmdline; cmdline++)
{
if (*cmdline == L'*')
{
parent(cmdline + 1);
return 0;
}
}
showcd(L"Child Process");
return 0;
}
你可以这样测试:
C:\Documents and Settings\Administrator>dir
Volume in drive C has no label.
Volume Serial Number is 6080-CDD5
Directory of C:\Documents and Settings\Administrator
13/12/2012 09:56 a.m. <DIR> .
13/12/2012 09:56 a.m. <DIR> ..
18/05/2012 03:10 p.m. <DIR> Desktop
20/06/2008 11:45 a.m. <DIR> Favorites
19/10/2010 02:54 p.m. <DIR> My Documents
20/06/2008 04:15 a.m. <DIR> Start Menu
1 File(s) 43 bytes
6 Dir(s) 29,912,666,112 bytes free
C:\Documents and Settings\Administrator>\appendtocurrentdirectory.exe *My Documents
答案 2 :(得分:0)
CreateProcess用于启动进程,您必须传递可执行文件。你不能传递.bat。相反,您需要使用/ C选项启动cmd.exe并将批处理文件作为参数传递。