如题,对注册表进行写操作,RegSetValueEx返回非零,GetLastError返回零是什么原因。
代码如下: HKEY hk;
DWORD dwData;
TCHAR szBuf[80];
BOOL succ; //create or open the reg.
if (RegCreateKey(HKEY_CURRENT_USER,
TEXT("Software\\MySoft\\1.0"),
&hk))
{
// cannot create the reg.
return false;
} dwData = 0;
if (0 != RegSetValueEx(hk,
TEXT("value"),
0,
REG_DWORD,
(LPBYTE) dwData,
sizeof(DWORD)))
{
// close the reg and return false.
// RegCloseKey(hk);
// return false;
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
} RegCloseKey(hk); return true;
代码如下: HKEY hk;
DWORD dwData;
TCHAR szBuf[80];
BOOL succ; //create or open the reg.
if (RegCreateKey(HKEY_CURRENT_USER,
TEXT("Software\\MySoft\\1.0"),
&hk))
{
// cannot create the reg.
return false;
} dwData = 0;
if (0 != RegSetValueEx(hk,
TEXT("value"),
0,
REG_DWORD,
(LPBYTE) dwData,
sizeof(DWORD)))
{
// close the reg and return false.
// RegCloseKey(hk);
// return false;
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
} RegCloseKey(hk); return true;
if( lr != ERROR_SUCCESS )
{
...
FormatMessage(...,lr,...);
}
The RegCreateKey function creates the specified registry key. If the key already exists in the registry, the function opens it. Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegCreateKeyEx function.
不知对你是否有用