【问题标题】:The system cannot find the file specified (Visual c++)系统找不到指定的文件(Visual c++)
【发布时间】: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


【解决方案1】:

由于无论如何您都在调用 .NET API 来启动进程,因此您可以尝试使用 .NET API 来构建您要调用的可执行文件的路径。

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

如果还是不行,您是否验证了传递给Process::Start() 的路径是正确的,并且 exe 位于您认为的位置?

【讨论】:

  • 您可能想要记录 using 指令,OP 正在努力解决基础问题。
  • @Andy .exe 不在我想的位置。有时,我们正在寻找复杂的东西,即使解决方案非常简单。谢谢你。我也会看看 .NET API。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多