RegOpenKeyEx后报错,FormatMessage取出来的信息是乱码。怎么解决。下面是代码。
#define BUFSIZE 80
HKEY hKey;
char szProductType[BUFSIZE];
DWORD WordType = REG_SZ;
DWORD dwBufLen = BUFSIZE;
LONG lRet;
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,NULL,0,KEY_QUERY_VALUE,&hKey);
if(lRet != ERROR_SUCCESS)
{
AfxMessageBox(LPCTSTR(_T("RegOpenKeyEx() Error!")),MB_OK);
Show_FormatMessage(lRet,0);
}
void Show_FormatMessage(DWORD dwErrorMsgId,int ShowType)
{
/*利用FormatMessage函数得到错误代码的文本显示。而错误代号则可由GetLastError()获得。(_WINDOWS)*/
//DWORD nErrorNo = GetLastError ( ); // 得到错误代码
DWORD ret;
LPSTR lpBuffer;
WCHAR strErrorCause[256];
char n_lpBuffer[256];
lpBuffer = n_lpBuffer;
ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErrorMsgId, // 此乃错误代码,通常在程序中可由 GetLastError()得之
LANG_NEUTRAL,
(LPTSTR) & lpBuffer,
0,
NULL);
if(ret != 0)
{
;
}
else
{
sprintf(lpBuffer,"Error Message ID is:%ld",dwErrorMsgId);
}
mbstowcs(strErrorCause,lpBuffer,strlen(lpBuffer));
//CString strErrorCause = lpBuffer ? _T(lpBuffer) : _T( " Sorry, cannot find this error info. " );
switch(ShowType)
{
case 0://用消息对话框显示消息
MessageBox(strErrorCause,TEXT("Error Message"),MB_OK);
break;
default:
break;
}
// Free the buffer.
LocalFree(lpBuffer);
}