Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c++ - How to use a variable inside a _T wrapper?

I want to make the hostname part of this string to be variable.. Currently, it is only fix to this URL:

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

I want to make something like this, so the URL is changeable..

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

update. Below is my latest attempt:

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

The error is : Error 1 error C2065: 'Lfull' : undeclared identifier c:est.cpp

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It doesn't work because the _T() macro works only with constant string literals. The definition for _T() looks something like this:

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

Since you're apparently compiling in Unicode mode, _T(full) expands to Lfull, which is obviously not what you want.

In your case, just pass in full without the _T() macro since CString defines a conversion operator to a const wchar_t* in Unicode mode, and const char* in non-Unicode mode.

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

Note that standard C++ also provides a std::string type and a std::wstring type which does pretty much what CString does, so MFC isn't actually required for string manipulation. std::string does not provide a conversion operator, but does provide access to the underlying C-style string via c_str().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...