请帮助我解决问题

请帮助我解决问题

本文介绍了请帮助我解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在vc ++中的UNICODE环境下执行代码.

我正在尝试读取注册表值

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

我收到以下错误..

Hi,

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

i am trying to read the registry values

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

I got the following error..

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.

推荐答案

aimdharma写道:
aimdharma wrote:

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

LPTSTRaBuffer = NULL;
//...
aBuffer = (char *)malloc(sizeof(char)*aSize);



您应该一致地使用类型 :即LPTSTR是指向TCHAR的指针,因此:



You should use types consistently: namely a LPTSTR is a pointer to TCHAR, hence:

LPTSTR aBuffer = NULL;
//...
aBuffer = (TCHAR *) malloc(sizeof(TCHAR) * aSize);




[更新]

正如 nv3 正确指出的那样,您需要精确地aSize bytes 的缓冲区才能调用RegQueryValueEx,因此正确的分配是:




[update]

As correctly noted by nv3, you need a buffer of exactly aSize bytes in order to call RegQueryValueEx, hence the correct allocation is:

aBuffer = (TCHAR *) malloc(aSize);



[/update]



[/update]


BYTE* aBuffer = NULL;
...
aBuffer = (BYTE*) malloc (aSize);



这篇关于请帮助我解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:03