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

12,039

Solution 1

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

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

Solution 2

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

Solution 3

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;
}
Share:
12,039

Related videos on Youtube

Cin Sb Sangpi
Author by

Cin Sb Sangpi

About me:http://en.wikipedia.org/wiki/User:Sbsangpi Visit www.sbsangpi.com (only in Myanmar Language)

Updated on June 04, 2022

Comments

  • Cin Sb Sangpi
    Cin Sb Sangpi almost 2 years

    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?

    • Aren
      Aren over 12 years
      It's so amazing what a small change to a questions structure can do :)
  • Cin Sb Sangpi
    Cin Sb Sangpi over 12 years
    thx alot 4 ur answer! but unfortunately it didn't work in visual studio windows form application! any more suggest! :)
  • David Heffernan
    David Heffernan over 12 years
    I suggest you learn to tell us what language you are coding in and tell us what the error message is when you encounter failure. "It doesn't work" is hopeless. Please try much harder.
  • Cody Gray
    Cody Gray over 12 years
    @Ajay: I'm betting you're using C++/CLI. That's the only way to create a Windows Forms application in C++. In that case, you're actually programming to the .NET Framework and you do everything very differently than if you were using unmanaged C++ code (as this answer assumes). As David says, you need to make sure that you include all relevant information in your question. If you created a C++/CLI project, then you need to tell people that because it does change the answers. In this case, you need Process.Start.
  • Cin Sb Sangpi
    Cin Sb Sangpi over 12 years
    ok! I'll inform you back! thx alot for u all patient answer! ;)