【发布时间】:2014-04-01 17:03:37
【问题描述】:
我正在尝试使用 Process::Start 在 Visual c++(VS 2012)中从另一个 .exe(Windows 窗体)运行 .exe(Win32)。
出于这个原因,我存储了 Windows 窗体所在的 Win32。这个想法是:
获取模块的全限定路径:
GetModuleFileName从路径中删除文件名和反斜杠:
PathRemoveFileSpec添加Win32应用名称:
sprintf将字符串^传递给
Process::Start
构建没有错误,但是当运行失败并出现错误时,如下图所示。我花了很多时间试图解决它,但没有结果。我该如何解决这个问题?
#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);
}
【问题讨论】:
-
您运行的是 64 位机器吗?编辑:在您回答之前,您是否验证了路径实际上是正确的?
标签: .net visual-c++ c++-cli base-class-library