windows 如何从 Visual C++ 应用程序执行另一个程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6861254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:38:42  来源:igfitidea点击:

How to execute another program from a Visual C++ Application

windowsvisual-studio-2010visual-c++

提问by Cin Sb Sangpi

I am doing a software project with Visual Studio Professional 2010.

我正在使用 Visual Studio Professional 2010 做一个软件项目。

In the form that I am making, I would like to put a link to open Microsoft Paint. How can I execute another application (MSPaint) from mine?

在我制作的表格中,我想放一个链接来打开 Microsoft Paint。如何从我的应用程序中执行另一个应用程序 (MSPaint)?

回答by David Heffernan

Call ShellExecute()passing openas the verb and mspaint.exeas the filename.

调用ShellExecute()传递open作为动词和mspaint.exe文件名。

ShellExecute(
    MainFormWindowHandle,
    "open",
    "mspaint.exe",
    NULL,
    NULL,
    SW_SHOW
);

回答by Carlos Rafael Ramirez

My contribution a complete example:

我的贡献一个完整的例子:

Go to Visual Studio, create a new Win32 C++ Project (not console), and paste the following code in the source file will appear:

进入Visual Studio,新建一个Win32 C++ Project(不是console),在源文件中粘贴如下代码会出现:

// Win32Project1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project1.h"
#include "shellapi.h"


int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);



    ShellExecuteA ( NULL, "open", 
        "your.exe", 
        "your params", 
        "working dir", SW_SHOW);


    return TRUE;
}

回答by Jaya madhu

Myself I contribute the following code which can be used in Windows

我自己贡献了以下可以在 Windows 中使用的代码

#include <iostream>
#include<Windows.h>
using namespace std;
int main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    DWORD dwProcessId = 0;
    DWORD dwThreadId = 0;

    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));

    bool bCreateProcess = NULL;

    bCreateProcess = CreateProcess((Location of Paint path),0,0,0,0,0,0,0,&si, pi); 
    //Instead of 0 NULL can also be used
    if (bCreateProcess == FALSE)
        cout << " Creation  Process Failed ";
    else
        cout << " Process Executedd ";

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}