Open external program

别等时光非礼了梦想. 提交于 2019-12-25 18:44:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!