问题
I've been trying to open an external program like Editor for example in C. I've searched for hours but haven't found a way to open external executables, e.g. open Skype or so from the Console Application.
This is my code so far:
/* 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");
}
How can I open external files? Thank you, Sincerely, Behring
回答1:
One possible way -
system("C:\\Windows\\notepad.exe");
or
ShellExecute(NULL, "open", "C:\\Windows\\notepad.exe", NULL, NULL, SW_SHOWDEFAULT);
or use 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 );
}
来源:https://stackoverflow.com/questions/31379141/open-external-program