assigning to type PWSTR

New to C++, so i thought i'd post this in here. Apologies if I should have put this in the Windows section. I'm trying to write some code to search Active Directory using winldap.h. According to the documentation, the ldap_search_s function requires the filter and attribute parameters to be passed as PWSTR, but i cant figure out how to successfully initialise or assign a PWSTR variable. I've tried...

 
  PWSTR filter = L"filters";


But this gives the error.
a value of type "const wchar_t *" cannot be used to initialize an entity of type "PWSTR"

Weirdly, this works fine on Visual Studio 2015, but not on Visual Studio 2017...

Tried messing with UNICODE, c_str(), c_str, _T but just cannot get this to work.

Any help appreciated.
did you try the barbarian approach?

PWSTR filter = new wchar_t[size];

or, possibly

wchar_t blah[] = "moo"; //or something like this.

PWSTR filter = blah; // no longer a constant, its the const that is making it grumpy I think



Last edited on
Maybe you just need const:
 
const PWSTR fileter = L"filters";

Last edited on
The problem is:
PWSTR is a typedef to wchar_t*
L"filters" is const wchar_t*

This works as well:
PCWSTR str = L"Hello";
Thanks for the help everyone, the following from jonnin worked...

1
2
wchar_t wcharFilter[7] = L"filter";
PWSTR pwstrFilter = wcharFilter;


Has to be a PWSTR and not a PCWSTR as ldap_search_s expects a PWSTR.

Was enjoying learning C++ until i encountred Windows.h haha.
what I gave you was seriously the crudest thing that would work, though.
there is probably a more elegant answer, if you really want to dig into it.

The M$ renaming of everything to poorly named cryptic gibberish has always turned me off. Wait until you encounter all the functions that expect a 'handle' type. /wrist.

Last edited on
Topic archived. No new replies allowed.