Leetfail
Leetfail

Reputation: 133

How do I open an external EXE file inside of C?

How do I open an external EXE file from inside C? I'm trying to write a C program that opens Notepad, and some other applications and I am stuck. Thanks for putting up with my noob level of C ;p

Upvotes: 1

Views: 3036

Answers (2)

SecurityMatt
SecurityMatt

Reputation: 6753

CreateProcess or ShellExecute is the Windows way of starting another process. You will need to #include to see their definitions

#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;
}

Upvotes: 0

Ganesh
Ganesh

Reputation: 5990

Please try system("notepad"); which will open the notepad executable. Please note that the path to the executable should be part of PATH variable or the full path needs to be given to the system call.

Upvotes: 2

Related Questions