Is there a macro or an easy way to search an LPCTSTR for a string

JonMny
JonMny used Ask the Experts™
on
I am calling a function that returns a LPCTSTR. I need to search the LPCTSTR for a value. Essentially I need a "Contains" function. Is there a marco that already exists that can do this?  If not how can I convert to std::string?

CString is not an option.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
the LPCTSTR is const char * in case of MBCS (multi-byte character set) project and it is const wchar_t * in case of a UNICODE project.

MBCS:

void f(LPCTSTR lp)
{
   std::string s = lp;   // simply assign it
};

UNICODE:

void f(LPCTSTR lp)
{
   std::wstring ws = lp;   // simply assign it
};

Commented:
if you want a solution for either MBCS or UNICODE you can do

void f(LPCTSTR lp)
{
   std::string s;
   if (sizeof(TCHAR) == sizeof(char))
   {
       s = lp;   // simply assign it if TCHAR is a char
   }
   else
   {
        size_t l = wcslen(lp);
        char * szb = new char(2 * l +1); // some extra buff
        wcstombs(szb, lp, 2*l + 1);
        s = lp;   // assign after converting multi-byte to wide strings
    }
}

Commented:
a typo and forgot delete the memory after use

        size_t l = wcslen(lp);
        char * szb = new char(2 * l +1); // some extra buff
        wcstombs(szb, lp, 2*l + 1);
        s = szb;   // assign after converting multi-byte to wide strings

        delete [] szb;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial