C++连续LPCTSTR

发布于 2024-10-20 21:31:44 字数 761 浏览 5 评论 0 原文

我正在为 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?

I am implementing a custom action for a WindowsCE CAB file, and I need to concat a LPCTSTR to get a proper path to an exe.

My custom action receives a LPCTSTR as an argument.

So (pseudocode):

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

This is using the imaginary "+" operator, that I would use in my standard language, C#.

I have relatively little experience in C++. What is the proper way to append a LPCTSTR for my purposes? The LaunchApp method uses this type as an argument.

Also if I want to display the resulting path (for debugging purposes) in a MessageBox, is there a quick way to convert to a LPCWSTR?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿哥们儿 2024-10-27 21:31:45

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

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 的返回值来查找组合生成的字符串的末尾并取消下一个字符来完成此操作。)

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

You need to allocate a new buffer to assemble the combined string in and then copy both parts into it. You can either pick a fixed, large buffer size

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

or allocate it dynamically to fit:

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;

If you're in Unicode mode then LPCTSTR == LPCWSTR (in MBCS mode == LPCSTR instead). Either way the MessageBox macro should work for you - it'll choose between MessageBoxA or MessageBoxW as appropriate.


As ctacke points out below, this in on Windows CE and I can't assume you're going to have the _s functions. I think in the second case it's OK to use the non _s variants since we know the buffer is big enough, but in the first _sntprintf does not guarantee a trailing null on the output string (as the _s version does) and so we need to initialise the buffer ourselves first:

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

(It might also be possible to do this by omitting the memset and using _sntprintf's return value to find the end of the combined generated string and nul the next character.)

AFAICR Windows CE is Unicode only and so LPCTSTR == LPCWSTR always.

醉生梦死 2024-10-27 21:31:45

您可以使用字符串进行连接,然后使用 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;

You can use string to be concatenated and then cast the result to LPCTSTR using ATL helpers like CA2T:

std::string filePath = "\\\\user\\Home\\";
std::string fileName = "file.ex";
std::string fullPath = filePath + fileName;
CA2T t(fullPath.c_str());
LPCTSTR lpctsrFullPath = t;
深爱成瘾 2024-10-27 21:31:44

对于串联,请使用 StringCchCat

TCHAR pszDest[260] = _T("");
StringCchCat(pszDest, 260, pszInstallDir); 
StringCchCat(pszDest, 260, _T("\\MyApp.exe"));
LaunchApp(pszDest);

For concatenation use StringCchCat

TCHAR pszDest[260] = _T("");
StringCchCat(pszDest, 260, pszInstallDir); 
StringCchCat(pszDest, 260, _T("\\MyApp.exe"));
LaunchApp(pszDest);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文