8

I'm sure this is sooo simple but I've come from a c# background where strings are easy and now I am making a small trip into the unmanaged world I am very confused.

Essentially I am using EnumDisplayDevices to list the available devices, I want to target a particular adapter so I need to compare DeviceString and DeviceName against some know values to see whether or not I have the right adapter to work on.

But I am stumped, I defined the known value as such...

wchar_t devName[] = L"Intel(R) HD Graphics Family";

but direct comparison doesn't work - if(devName == theDisplay.DeviceName)

strcmp doesnt seem to work with wide chars so I have no idea what to do, anyone know how to do this please?

Thanks

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
john bowring
  • 178
  • 2
  • 2
  • 11

2 Answers2

13

Use a std::wstring, it has an operator==.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • This worked for me by using 2 std::wstrings thusly: if (devName.compare(dn) == 0) – john bowring Oct 05 '11 at 13:41
  • 1
    @john: You should just use `if (devName == dn)` – Puppy Oct 05 '11 at 16:46
  • I'm not sure whether to laugh or to cry, when the OP says that "in C# strings are simple", and then you have to write all this member function nonsense for a simple comparison. As far as I can tell, C++ is one of the few C-like languages where you can compare strings like a sane person with `s1 == s2`. – Kerrek SB Oct 05 '11 at 20:10
  • to what member function nonsense do you refer? Typically in c# if I want to compare 2 string I make 2 strings and use strcmp, simple. As I said in my original post s1 == s2 did not work with wchar_t arrays. – john bowring Oct 06 '11 at 11:53
  • 2
    @johnbowring: It's a `std::wstring`. As I said earlier. You can just compare a pair of `std::wstring` by using `if (s1 == s2)`, where `s1` and `s2` are `std::wstring`. You're confused because you're so busy trying to make C++ look like C#, you haven't learned a scrap about C++. C++ 101: Use the Standard string classes for all string handling needs. – Puppy Oct 06 '11 at 12:36
13

If you check Visual Studio help for strcmp, you'll find it lists 3 functions to compare strings: strcmp, wcscmp and _mbscmp. The one you're looking for is wcscmp.

Joel Rondeau
  • 7,486
  • 2
  • 42
  • 54