tags:

views:

299

answers:

3

Hi

I have to excecute a exe which is available in some drive how can i do this using c++??

I am doing like this

#include <stdio.h>
#include <conio.h>
#include <windows.h>

void main()
{
    STARTUPINFO si;

    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if(!CreateProcess(L"c:\\DOTNET.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
    }
    else
    {
        printf("Prcess Creation Success");
    }

    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread ); 

    getch();
}

But every time it is showing process creation failed with error code 2(i.e can not find the path specified) but i place the DOTNET.exe at c:\DOTNET.exe only.

what is wrong in the above code.can any one suggest me...

Any help is greately appreciated..

Thabks in advance.

A: 

Are you building a Unicode executable? Try _T("C:\\DOTNET.exe") instead.

Or does your DOTNET.exe have some dependent DLLs which are not being found?

Paul Mitchell
A: 

I think you need to call CreateProcess() differently.

Try this;

if (!CreateProcess( NULL, L"C:\\DOTNET.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
Mel Green
then it gives the exception as Unhandled exception at 0x77e4ba89 in CreateProc.exe: 0xC0000005: Access violation writing location 0x00415696.
Cute
Hmmm... I'll keep looking at it and see if I can reproduce your problem, at the moment it works in my test app.
Mel Green
+1  A: 

I've just tested your code and it's working here with :

if(!CreateProcess(L"C:\\Program Files\\Mozilla Firefox\\firefox.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )

A C++/Win32 solution for your C/Win32 code :)

void ExecuteAndWait (wstring toto)
{
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION  pi;
    vector<TCHAR> V( toto.length() + 1);
    for (int i=0;i< (int) toto.length();i++)
 V[i] = toto[i];
    CreateProcess(NULL, &V[0],0, 0, FALSE, 0, 0, 0, &si, &pi);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}
anno