请帮助我解决问题 [英] pls help me to fix the issue

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

问题描述



我正在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

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..

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);

LPTSTR aBuffer = 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]


辅助缓冲区应为字节数组.这就是您应该提供的.您将LPTSTR定义为Buffer时,会将Unicode构建转换为wchar_t *,而char *无法转换为wchar_t *.因此,请按照以下方式进行操作:

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);


//LPTSTR aBuffer = NULL;

将aBuffer声明为char *将解决此问题.

尝试char * aBuffer = NULL;
//LPTSTR aBuffer = NULL;

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

Try char* aBuffer = NULL;


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆