Question

I 'm trying run an .exe (Win32) from another .exe (Windows Forms) both in visual c++ (VS 2012), using Process::Start.

For this reason, I stored Win32, where Windows Forms is located. The idea is:

  • get the fully qualified path of the module: GetModuleFileName

  • remove file name and backslash from the path: PathRemoveFileSpec

  • add the name of Win32 application: sprintf

  • pass string^ to Process::Start

Build has no errors, but when running fails with error as image below indicates. I spent a lot of time trying to resolve it, but without result. How can I fix this?

enter image description here

#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>

#include <string>
#include <cerrno>
#include <Shlwapi.h>

#include <msclr\marshal_cppstd.h>

using namespace std;
using namespace System;
using namespace msclr::interop;


//code...


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{   
    TCHAR path[1000];
    GetModuleFileName(NULL, path, 1000) ; // path: A pointer to a buffer that receives 
                                             //       the fully qualified path of the module

        PathRemoveFileSpec(path); // path: holds location only (TCHAR)

        CHAR mypath[1000];
        wcstombs(mypath, path, wcslen(path) + 1); // convert tchar to char (mypath)

        // Formatting the string: constructing a string by substituting computed values at various 
        // places in a constant string
        CHAR mypath2[1000];
        sprintf(mypath2, "%s\\JoypadCodesApplication.exe", mypath);

        String^ result;
        result = marshal_as<String^>( mypath2 );

        Process::Start(result);
}
Was it helpful?

Solution

Since you're calling a .NET API to start the process anyway, you could try using .NET APIs to build the path of the executable that you want to call.

using namespace System::Diagnostics;
using namespace System::IO;
using namespace System::Reflection;

String^ assemblyLocation = Assembly::GetExecutingAssembly()->Location;
String^ dir = Path::GetDirectoryName(assemblyLocation);

String^ childProcessPath = Path::Combine(dir, "JoypadCodesApplication.exe");
Process::Start(childProcessPath);

If that still doesn't work, have you verified that the path passed to Process::Start() is correct and the exe is at the location you think that it is?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top