Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am doing code under UNICODE environment in vc++.

i am trying to read the registry values

DWORD aType = 0;
DWORD aSize = 0;
LPTSTR aBuffer = NULL;
RegQueryValueEx(hTestKey, L"DisplayName" , NULL, &aType, NULL, &aSize);
aBuffer = (char *)malloc(sizeof(char)*aSize);

I got the following error..

CSS
error C2440: '=' : cannot convert from 'char *' to 'unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



how to solve it.
Posted

aimdharma wrote:
LPTSTR aBuffer = NULL;
//...
aBuffer = (char *)malloc(sizeof(char)*aSize);


You should use types consistently: namely a LPTSTR is a pointer to TCHAR, hence:
C++
LPTSTR aBuffer = NULL;
//...
aBuffer = (TCHAR *) malloc(sizeof(TCHAR) * aSize);



[update]

As correctly noted by nv3, you need a buffer of exactly aSize bytes in order to call RegQueryValueEx, hence the correct allocation is:
C++
aBuffer = (TCHAR *) malloc(aSize);


[/update]
 
Share this answer
 
v2
Comments
@BangIndia 2-Apr-12 6:03am    
Thanks a lot..
CPallini 2-Apr-12 6:42am    
You are welcome.
nv3 2-Apr-12 13:59pm    
Actually, this solution is not fully correct. The malloc allocates twice as much storage as is really needed. RegQueryValueEx explicitly asks for a BYTE buffer and returns always the number of bytes that it needs, not the number of characters! So

malloc (aSize)

is what is needed. I therefore would recommend to stick with the data type that RegQueryValueEx asks for. Then, after testing the aType return value, you would want to cast the buffer pointer into whatever type the system call has returned -- in case of a string into a PTSTR.
CPallini 2-Apr-12 15:48pm    
Yes, you are right.
Espen Harlinn 2-Apr-12 16:24pm    
5'ed!
The auxiliary buffer is expected to be an array of bytes. So that is what you should provide. Your definition of a Buffer as LPTSTR translates in a Unicode build to a wchar_t* and your char* cannot be cast into that. So do it this way:

BYTE* aBuffer = NULL;
...
aBuffer = (BYTE*) malloc (aSize);
 
Share this answer
 
Comments
nv3 2-Apr-12 14:05pm    
Hello aimdharam!

In a message message that meanwhile obviously has been removed you wrote that this solution was not correct and you would still get a compile error

*** cannot convert from 'unsigned char *' to 'unsigned short *'

Could it be that you have not copied the first line of the above code, in which aBuffer is being declared as BYTE* ? You have probably used your original declaration of

LPTSTR aBuffer = NULL;

didn't you? I would always prefer to declare the buffer as BYTE*, because that is what the system call asks for. Using any other type might easily get you confused about the number of bytes to allocated, as you can see in the solution 1 example.
Espen Harlinn 2-Apr-12 16:24pm    
5'ed!
//LPTSTR aBuffer = NULL;

Declaring the aBuffer as a char* will solve the issue.

Try char* aBuffer = NULL;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900