开发者

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

开发者 https://www.devze.com 2023-03-23 19:22 出处:网络
I am doing a software project with 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 applicat

I am doing a software project with 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?


Call ShellExecute() passing open as the verb and mspaint.exe as the filename.

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:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号