2013-02-10 101 views
1

如何从C中打开外部EXE文件?我正在尝试编写一个打开记事本的C程序,以及其他一些应用程序,并且我被卡住了。 感谢您支持我的noob级别的C; p如何在C中打开外部EXE文件?

+0

您可能想要查看使_exec或类似的win32 API调用。 – forTruce 2013-02-10 03:29:49

+0

http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html – technosaurus 2013-02-10 04:17:04

回答

2

请尝试system("notepad");这将打开记事本可执行文件。请注意,可执行文件的路径应该是PATH变量的一部分,或者需要将全路径提供给system调用。

+0

这似乎不工作..我错过了一个库或什么? – Leetfail 2013-02-10 03:38:10

+1

你可以请尝试包括这个头文件'#include '? – Ganesh 2013-02-10 03:40:25

+0

它默认在那里。 – Leetfail 2013-02-10 03:41:33

0

CreateProcess或ShellExecute是Windows启动另一个进程的方式。您需要#include才能看到它们的定义

#include <windows.h> 

int main() 
{ 
    STARTUPINFOW siStartupInfo; 
    PROCESS_INFORMATION piProcessInfo; 
    memset(&siStartupInfo, 0, sizeof(siStartupInfo)); 
    memset(&piProcessInfo, 0, sizeof(piProcessInfo)); 
    siStartupInfo.cb = sizeof(siStartupInfo); 

    if (CreateProcessW(L"C:\\Windows\\system32\\notepad.exe"), 
         NULL, NULL, NULL, FALSE, 
         0, NULL, NULL, 
         &siStartupInfo, &piProcessInfo)) 
    { 
     /* This line waits for the process to finish. */ 
     /* You can omit it to keep going whilst the other process runs */ 
     dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000)); 
    } 
    else 
    { 
     /* CreateProcess failed */ 
     iReturnVal = GetLastError(); 
    } 
    return 0; 
}