c++ - 如何在 _T 包装器中使用变量?

标签 c++ visual-c++ mfc

我想让这个字符串的主机名部分可变.. 目前,它只修复了这个 URL:

_T(" --url=http://www.myurl.com/ --out=c:\\current.png");

我想做这样的东西,所以URL是可变的..

_T(" --url=http://www." + myurl +  "/ --out=c:\\current.png");

更新。以下是我最近的尝试:

      CString one   = _T(" --url=http://www.");
      CString two(url->bstrVal);
      CString three = _T("/ --out=c:\\current.png");
      CString full = one + two + three;

      ShellExecute(0,                           
               _T("open"),        // Operation to perform
               _T("c:\\IECapt"),  // Application name
               _T(full),// Additional parameters
               0,                           // Default directory
               SW_HIDE);

错误是:错误 1 ​​error C2065: 'Lfull' : undeclared identifier c:\test.cpp

最佳答案

它不起作用,因为 _T() 宏仅适用于常量 字符串文字。 _T() 的定义如下所示:

#ifdef UNICODE
#define _T(str) L##str
#else
#define _T(str) str

由于您显然是在 Unicode 模式下编译,_T(full) 扩展为 Lfull,这显然不是您想要的。

在您的情况下,只需传入 full 而无需 _T() 宏,因为 CString 将转换运算符定义为 const Unicode 模式下为 wchar_t*,非 Unicode 模式下为 const char*

ShellExecute(0, _T("open"), _T("c:\\IECapt"), full, 0, SW_HIDE);

请注意,标准 C++ 还提供了一个 std::string 类型和一个 std::wstring 类型,它们的功能几乎与 CString 的功能相同,因此字符串操作实际上不需要 MFC。 std::string 提供转换运算符,但通过c_str() 提供对底层 C 风格字符串的访问。

关于c++ - 如何在 _T 包装器中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515310/

相关文章:

mfc - 对话使用分析/热图工具

c++ - PostThreadMessage & 发送消息

c++ - 缺少 WinUSB 模板

c++ - 为什么我在 C 中使用 openMP 时无法在 for 循环中定义数据类型?

visual-studio-2010 - Visual Studio 2010 - C++ 项目 - 删除 *.sdf 文件

c++ - 为什么在 iterator_facade 中会出现编译错误,具体取决于我放置 Boost header 的位置?

c++ - 如何将 RGB 颜色代码分配给 WORD?

c++ - 二叉搜索树 C++(父级)

c++ - 以 headless 模式运行 WinDbg

c++ - MFC CScrollView 滚动不工作