0

我使用带有参数 lpCurrentDirectory 的 createprocess() 从我的应用程序创建一个进程,但它不起作用。它给出错误“系统找不到指定的路径”。我的代码是 CreateProcess( "XYZ\\bin\\run.bat", NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, "XYZ\\bin", &siStartupInfo, &piProcessInfo ) ; 我还通过使用 GetCurrentDir() 获取当前目录并添加到当前目录来提供完整路径作为 lpCurrentDirectory XYZ\\bin,但它仍然不起作用。我希望我的批处理文件从 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)

`。

4

3 回答 3

0

请看看这个。您应该阅读整个帖子,因为它重申了在之前的帖子中给您的答案。

于 2013-10-10T13:08:38.030 回答
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
于 2013-10-13T19:28:56.857 回答
0

CreateProcess 用于启动进程,您必须传递一个可执行文件。您不能传递 .bat。相反,您需要使用 /C 选项启动 cmd.exe 并将批处理文件作为参数传递。

于 2013-10-13T19:34:45.077 回答