c - createprocess 函数的困难时期

标签 c winapi

首先,我已经在网上阅读了几天与此功能相关的所有内容,但仍然无法正确理解。我在这里使用 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 );
}

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

最佳答案

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

关于c - createprocess 函数的困难时期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11965413/

相关文章:

c - 寻找中型代码块的来源

c - C标准库中的函数线程安全吗?

c++ - 线程中的堆栈大小在 C++ 中定义了什么?

c++ - 向 CFileDialog 添加控件

c++ - 如何判断虚拟内存页是否已被锁定?

c - C语言从内存中读取地址

c++ - 在程序中添加空间计数器

C - 在 C 8051 中递增 18 位

windows - 如何获取已安装的 Windows 通用应用程序列表?

c++ - 如何通过 MessageBox 输出一个指针?