Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...