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



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

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

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

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

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

转到Visual Studio,创建一个新的Win32 c++项目(不是控制台),并在源文件中粘贴以下代码将出现:

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

我自己贡献了以下可以在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);
}

最新更新