【问题标题】:Open external program打开外部程序
【发布时间】:2015-10-01 11:13:47
【问题描述】:

我一直在尝试打开像编辑器这样的外部程序,例如在 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");
}

如何打开外部文件? 谢谢, 真挚地, 贝林

【问题讨论】:

  • 你希望你的程序能做什么?您发布的代码会打开“C://Users/Jonte/Desktop/Skype.exe”文件并退出。
  • 打开文件是一回事,尝试将文件作为程序运行是另一回事。

标签: c windows file console-application


【解决方案1】:

一种可能的方式——

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

【讨论】:

猜你喜欢
  • 2014-04-05
  • 1970-01-01
  • 2011-07-19
  • 2017-02-15
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多