1

我调用GetModuleFileName函数,以检索指定模块的完全限定路径,以便通过Process::Start方法调用同一文件中的另一个 .exe。

但是,当路径包含非拉丁字符(在我的情况下为希腊字符)时,无法调用 .exe。

有什么办法可以解决这个问题吗?

代码:

    TCHAR path[1000];
    GetModuleFileName(NULL, path, 1000) ; // Retrieves the fully qualified path for the file that
                                          // contains the specified module.

    PathRemoveFileSpec(path); // Removes the trailing file name and backslash from a path (TCHAR).

    CHAR mypath[1000];
    // Convert TCHAR to CHAR.
    wcstombs(mypath, path, wcslen(path) + 1);

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

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

    Process::Start(result);
4

2 回答 2

1

.NET 中的字符串以 UTF-16 编码。您正在调用的事实wcstombs()意味着您的应用程序已针对 Unicode 编译并TCHAR映射到WCHAR,这是 Windows 用于 UTF-16 的内容。所以根本不需要打电话wcstombs()。检索路径并将其格式化为 UTF-16,然后将其编组为 UTF-16。完全停止使用TCHAR(除非您需要为 Windows 9x/ME 编译):

WCHAR path[1000];
GetModuleFileNameW(NULL, path, 1000);

PathRemoveFileSpecW(path);

WCHAR mypath[1000];
swprintf_s(mypath, 1000, L"%s\\Client_JoypadCodesApplication.exe", path);

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

Process::Start(result);

更好的选择是使用原生 .NET 解决方案(未经测试):

String^ path = Path::DirectoryName(Application->StartupPath); // uses GetModuleFileName() internally
// or:
//String^ path = Path::DirectoryName(Process::GetCurrentProcess()->MainModule->FileName);

Process::Start(path + L"\\Client_JoypadCodesApplication.exe");
于 2014-05-16T15:19:24.467 回答
0

您必须使用GetModuleFileNameW并将结果存储在wchar_t字符串中。

大多数 Win32 API 函数都有一个“Unicode”变体,它接受/给出 UTF-16 字符串。强烈建议不要使用 ANSI 版本。

于 2014-05-16T12:54:02.667 回答