2015-07-13 80 views
-1

我一直在試圖打開一個外部程序,如編輯器,例如C. 我已經搜索了幾個小時,但還沒有找到打開外部可執行文件的方法,例如,從控制檯應用程序打開Skype等。打開外部程序

這是我到目前爲止的代碼:

/* fopen1.c */ 
#include <Windows.h> 
#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(int) 
{ 
    FILE *fp; 
    fp = fopen("C://Users/Jonte/Desktop/Skype.exe", "r"); 
} 

我怎樣才能打開外部文件? 謝謝 真誠, 貝林

+1

你有什麼期望你程序要做到嗎?你發佈的代碼打開「C://Users/Jonte/Desktop/Skype.exe」文件,然後退出。 –

+1

打開文件是一回事,試圖將文件作爲程序運行是另一回事。 – alk

回答

4

一種可能的方式 -

system("C:\\Windows\\notepad.exe");

ShellExecute(NULL, "open", "C:\\Windows\\notepad.exe", NULL, NULL, SW_SHOWDEFAULT); 

或使用CreateProcess

VOID startup(LPCTSTR lpApplicationName) 
{ 
    // additional information 
    STARTUPINFO si;  
    PROCESS_INFORMATION pi; 

    // set the size of the structures 
    ZeroMemory(&si, sizeof(si)); 
    si.cb = sizeof(si); 
    ZeroMemory(&pi, sizeof(pi)); 

    // start the program up 
    CreateProcess(lpApplicationName, // the path 
    argv[1],  // 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 
) 
    // Close process and thread handles. 
    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 
} 
+1

還有'_exec *()'和'_spawn *()'函數系列。 – alk

+0

yes .. http://www.tenouk.com/cpluscodesnippet/crtexecfamilyexample.html – udit043

+0

僅供參考:exec https://msdn.microsoft.com/en-us/library/431x4c1w.aspx and and spawn https:// msdn.microsoft.com/en-us/library/20y988d2.aspx – alk