简体   繁体   English

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

[英]How to execute another program from a Visual C++ Application

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.在我制作的表格中,我想放一个链接来打开 Microsoft Paint。 How can I execute another application (MSPaint) from mine?如何从我的应用程序执行另一个应用程序 (MSPaint)?

Call ShellExecute() passing open as the verb and mspaint.exe as the filename.调用ShellExecute()传递open作为动词和mspaint.exe作为文件名。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM