I’m currently developing a program that reads strings from a text file in 8-bit ASCII mode, and I make a function to assign that string into a wchar_t*
Here I’m using atlconv.h and USES_CONVERSION macro to convert the string into wstring. So here is the code:
void CSampleProvider::getCopy(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR *a, const string s) {
USES_CONVERSION;
wstring temp(A2W (s.c_str ()));
a->pszLabel = new WCHAR(temp.length()+1);
if (!a->pszLabel)
return;
wcscpy_s(a->pszLabel, temp.size()+1, (LPWSTR)temp.c_str());
::MessageBox(NULL,s.c_str(),"getCopy",0);
return;
}
I used a debugger to watch line by line. It works well (i.e. the content of a->pszLabel is as I expected, the same as the content of s) until it reaches return. As it returned, an error popped up:
First-chance exception at 0x770f3067 in CPTest.exe: 0xC0000005: Access violation reading location 0x00200074.
Unhandled exception at 0x770f3067 in CPTest.exe: 0xC0000005: Access violation reading location 0x00200074.
Does anyone know how to fix this? Please tell me. Your answers are highly appreciated 🙂
Thanks,
Reinardus
EDIT: Oh yeah, the type CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR is a struct, and one of its member is pszLabel, which is a wchar_t*
a->pszLabel = new WCHAR(temp.length()+1);returns a pointer to a newWCHARwhose value is the length of your string plus one. You meanta->pszLabel = new WCHAR[temp.length()+1];which returns a pointer to a new array ofWCHARwith the number of elements being the length of your string plus one.