1

首先,我已经在网上阅读了几天与此功能相关的所有内容,但仍然无法正确理解。我在这里使用 msdn 页面中使用的相同示例:

http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx

我的代码:

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

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

    SecureZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    SecureZeroMemory( &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)
        "C:\\Users\\user\\Desktop\\FreeCell.lnk",        // 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 );
}

如您所见,我只更改了目录部分。当我运行它不会给出任何错误,但它也无法正常工作。

4

1 回答 1

10

CreateProcess 只能启动可执行程序。对于 .lnk 文件,您需要 shell 的帮助。使用带有“open”动词的ShellExecuteEx() 。

于 2012-08-15T07:21:09.643 回答