barbarello wrote:
Hi !!
1 2 3
| DWORD nRet;
NETRESOURCE nr;
nr.lpLocalName = lect; //Here is my error !! |
The error compilation is:
error: invalid conversion from `const wxChar*' to `CHAR*' front of line nr.lpLocalName = lect;
Thanks a lot
If 'lpLocalName' is an input only parameter - meaning you want to pass a value to the Win32 API - , try this
nr.lpLocalName=const_cast< CHAR* >(lect.c_str());
or (old style, deprecated and dangerous, as it will fail to catch errors if c_str() returns something other than const CHAR*: this _will_ fail if you decide to build your application with Unicode for example)
nr.lpLocalName=(CHAR*) lect.c_str();
Background: The problem lies on the Win32 API side. nr.lpLocalName is a CHAR pointer which means that if you pass in a pointer to a string, the Win32 API is allowed to modify that string. (It should have been declared const CHAR*, in which case the Win32 API is allowed to read but not modify the contents of the string.)
c_str() however returns a 'const' character pointer, which is a pointer that an API may read, but not modify. Reason is that wxString handles its data on its own. If you 'reach inside' wxString and change things there directly - bypassing wxString's mechanics - and then later have wxString try to handle the data again, wxString may get confused.
const_cast() removes the 'const'ness from the pointer, bypassing the 'const' restriction and allowing you to compile your application. But beware: you pass a pointer to the Win32 API which the API must not modify, but you have to trust the API to keep the promise not to change it.
If lpLocalName is an output parameter - meaning you expect some return string from the Win32 API, you cannot use a wxString at all. You have to allocate a CHAR buffer, pass the buffer to the API call and create a wxString from that buffer afterwards.
1 2 3 4 5
|
CHAR my_buffer[1024];
nr.lpLocalName=my_buffer;
// call API
lect=my_buffer; // create wxString from buffer |
or read up on wxStringBuffer.
regards
Hajo
Partager