CreateProcess()运行时出现访问冲突错误

18

我的目标是在我的程序中执行外部可执行文件。首先,我使用了system()函数,但我不想让用户看到控制台。所以,我搜索了一下,找到了CreateProcess()函数。然而,当我尝试传递参数给它时,不知道为什么,它失败了。我从MSDN上复制了这段代码,并稍作修改:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    /*
    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }
    */
    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        L"c:\\users\\e\\desktop\\mspaint.exe",        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

然而,这段代码会出现访问冲突的问题。我能否在不显示控制台给用户的情况下执行 mspaint?

非常感谢。


3
首先,CreateProcess要求它的第二个参数(如果提供)是一个非const字符串。我不确定这在实践中是否会成为问题,但出于完整性考虑,我想提一下。 - reuben
除此之外,AV 正在发生的地方在哪里?你有调用堆栈吗? - reuben
@reuben 嗯...我不太确定,但我猜这是调用堆栈的输出:kernel32.dll!76da70ac() [下面的帧可能不正确和/或缺失,kernel32.dll没有加载符号] > msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302 + 0x1d bytes C++ - John Doe
3个回答

22
第二个参数是一个 LPTSTR,即指向非 const 字符数组的指针。文档中明确说明:(链接)

此参数不能为只读内存的指针(例如 const 变量或字面字符串)。

传递字符串字面值的问题在于:

系统会添加一个终止空字符来将命令行字符串分隔成文件名和参数两个部分以供内部处理。

这意味着,在您的情况下,它试图修改只读内存,因此导致崩溃。

2
@JohnDoe: wchar_t path[] = L"C:\\users\\e\\desktop\\" - hmjd
wchar_t path[] = L"C:\\blahblah" 不同于 LPWSTR path = L"C:\\blahblah",但与 wchar_t* path = L"C:\\blahblah" 相同。你应该使用 @hmjd 建议的方法,或者定义 LPWSTR path,然后为该变量分配内存并将路径复制到其中。 - Denis V
2
@DenisV - wchar_t path[] = L"C:\\users\\e\\desktop\\" 显然不同于 wchar_t* path = L"C:\\blahblah"(但后者实际上与 LPWSTR path = L"C:\\blahblah" 相同,这是不好的)。 - Eran
@eran 是的,你说得对,我写那个的时候被什么东西分心了,把例子放错了位置。 - Denis V
1
@GSerg,那可能是我了解这些东西的地方,当时是那个博客的忠实读者。事实上,人们仍然犯这个错误(这意味着操作系统不使用额外的缓冲区,编译器会将字符串字面值静默转换为char*),这有点令人不安,尽管它偶尔会让我得到一些赞扬 ;) - Eran
显示剩余2条评论

9

试试这个,应该可以正常工作。

TCHAR lpszClientPath[500]= TEXT("c:\\users\\e\\desktop\\mspaint.exe");
if(!CreateProcess(NULL, lpszClientPath, NULL, NULL, FALSE,  NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE|CREATE_UNICODE_ENVIRONMENT,NULL, NULL, &si, &pi))
            {
    printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
            }
...
...

0

将你的代码改为这样:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    TCHAR ProcessName[256];
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    wcscpy(ProcessName,L"c:\\users\\e\\desktop\\mspaint.exe");
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    /*
    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }
    */
    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        ProcessName,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接