oursgris
oursgris

Reputation: 2882

Compare 2 strings of type string and LPWSTR

I'd like to compare a string for example "Tedt2.csv" to &value which is LPWSTR in the declaration of the function. This is my base code which works:

PWSTR   value = nullptr;
HRESULT hr    = properties->GetStringValue(key, &value);

I tested many approches like

if (wcsstr(&value, L"Tedt2.csv") == 0)
{
   wprintf(L"%ws: %ws\n", keyName, value);
}

but it always enters the if.

Can someone help me please? It should be trivial but I'm a bit lost with C++. I've to make a proof of concept.

Upvotes: 1

Views: 3611

Answers (2)

qPCR4vir
qPCR4vir

Reputation: 3571

and without & ?

if (wcsstr(value, L"Tedt2.csv") == 0)

 ... make something when "Tedt2.csv" not appear in value

your value is a pointer. You are passing a pointer to a pointer, but you need a pointer

wchar_t *wcsstr(   wchar_t *str,   const wchar_t *strSearch ); // C++ only

Upvotes: 3

hmjd
hmjd

Reputation: 122001

wcsstr() searches for a string within a string, it does not perform a string comparison. It returns NULL if the rhs is not found in the lhs. The 0 in this context will be converted to a null pointer and the if branch will entered when "Tedt2.csv" is not found in value. Use wcscmp() instead:

if (wcscmp(value,  L"Tedt2.csv") == 0)
{      //  ^ remove & also
}

Or use std::wstring that provides operator==():

#include <string>

const std::wstring search_string(L"Tedt2.csv");
if (search_string == value)
{
}

Upvotes: 7

Related Questions