1

我正在为 WindowsCE CAB 文件实现自定义操作,我需要连接 LPCTSTR 以获得 exe 的正确路径。

我的自定义操作接收 LPCTSTR 作为参数。

所以(伪代码):

extern "C" codeINSTALL_EXIT MYCUSTOMACTION_API Install_Exit(
    HWND    hwndParent,
    LPCTSTR pszInstallDir,
    WORD    cFailedDirs,
    WORD    cFailedFiles,
    WORD    cFailedRegKeys,
    WORD    cFailedRegVals,
    WORD    cFailedShortcuts
)
{
    if (FALSE == LaunchApp(pszInstallDir + "\\MyApp.exe"))
       ::MessageBox(hwndParent, L"Could not launch app!", L"Setup", MB_ICONINFORMATION );
    return codeINSTALL_EXIT_DONE;
}

这是使用虚构的“+”运算符,我将在我的标准语言 C# 中使用它。

我在 C++ 方面的经验相对较少。为我的目的附加 LPCTSTR 的正确方法是什么?LaunchApp 方法使用此类型作为参数。

此外,如果我想在 MessageBox 中显示结果路径(用于调试目的),是否有一种快速转换为 LPCWSTR 的方法?

4

3 回答 3

6

对于串联使用StringCchCat

TCHAR pszDest[260] = _T("");
StringCchCat(pszDest, 260, pszInstallDir); 
StringCchCat(pszDest, 260, _T("\\MyApp.exe"));
LaunchApp(pszDest);
于 2011-03-08T16:57:17.110 回答
2

您需要分配一个新缓冲区来组装组合字符串,然后将两个部分复制到其中。您可以选择一个固定的大缓冲区大小

TCHAR fullPath[MAX_PATH + 11]; // 11 = length of "\MyApp.exe" + nul in characters
_sntprintf_s(fullPath, MAX_PATH + 11, _T("%s\\MyApp.exe"), pszInstallDir);

或动态分配它以适应:

size_t installDirLen = tcslen(pszInstallDir);
size_t bufferLen = installDirLen + 11; // again 11 = len of your string
LPWSTR fullPath = new TCHAR[bufferLen];
// if you're paranoid, check allocation succeeded: fullPath != null
tcsncpy_s(fullPath, bufferLen, pszInstallDir);
tcsncat_s(fullPath, bufferLen, _T"\\MyApp.exe");
// use it
delete fullPath;

如果您处于 Unicode 模式,则 LPCTSTR == LPCWSTR(在 MBCS 模式下 == LPCSTR)。无论哪种方式,MessageBox 宏都应该适合您 - 它会根据需要在 MessageBoxA 或 MessageBoxW 之间进行选择。


正如 ctacke 在下面指出的那样,这在 Windows CE 上,我不能假设您将拥有 _s 函数。我认为在第二种情况下使用非 _s 变体是可以的,因为我们知道缓冲区足够大,但是在第一个 _sntprintf 不保证输出字符串的尾随 null (就像 _s 版本一样),所以我们需要首先我们自己初始化缓冲区:

size_t bufferLen = MAX_PATH + 11;
TCHAR fullPath[bufferLen];
// zero the buffer out first
memset(fullPath, 0, sizeof(TCHAR) * bufferLen);
// only write up to bufferLen - 1, i.e. ensure the last character is left zero
_sntprintf(fullPath, bufferLen - 1, _T("%s\\MyApp.exe"), pszInstallDir);

(也可以通过省略 memset 并使用 _sntprintf 的返回值来查找组合生成的字符串的结尾和 nul 下一个字符来做到这一点。)

AFAICR Windows CE 仅是 Unicode,因此 LPCTSTR == LPCWSTR 始终。

于 2011-03-08T17:06:02.160 回答
1

您可以使用字符串进行连接,然后使用 CA2T 等 ATL 助手将结果转换为 LPCTSTR:

std::string filePath = "\\\\user\\Home\\";
std::string fileName = "file.ex";
std::string fullPath = filePath + fileName;
CA2T t(fullPath.c_str());
LPCTSTR lpctsrFullPath = t;
于 2019-11-13T18:12:12.580 回答