WinCE RegQueryValueEx 返回值的问题

来源:互联网 发布:拼图软件fun 编辑:程序博客网 时间:2024/05/11 02:39

魅族MyMobile SDK下,一段获取注册表值的代码如下:

  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, sActive+achKey, 0, 0, &hKey_tmp) == ERROR_SUCCESS)
        {  
        wsprintf(comname.C_Str(),L"key open sucesss!");
        MzMessageBox(NULL,comname,L"",MZ_OK,0,0); 

        DWORD dwSize=sizeof(dwValue);
        if (ERROR_SUCCESS==::RegOpenKeyEx(hKeyName,lpSubKey, 0, NULL,&hKey))
        {
             LONG lQueryRe=::RegQueryValueEx(hKey, lpValueName, NULL,NULL,(BYTE *)&dwValue,&dwSize); 
  ::RegCloseKey(hKey);*/

      //读取指定键的的name值
      long t=RegQueryValueEx(hKey_tmp, L"DisplayName", NULL, &dwType, (BYTE *)&comNum, &dwSize);
            if(t == ERROR_SUCCESS)
            {
        
              wsprintf(comname.C_Str(),L"DisplayName=%s,SUCCESS=%ld",comNum,t);
              MzMessageBox(NULL,comname,L"",MZ_OK,0,0); 
            }
            else
            {  
              wsprintf(comname.C_Str(),L"DisplayName Get Error!Error is %ld",t);
              MzMessageBox(NULL,comname,L"",MZ_OK,0,0); 
            }

              RegCloseKey(hKey_tmp);
           }

程序运行到if (ERROR_SUCCESS==::RegOpenKeyEx(hKeyName,lpSubKey, 0, NULL,&hKey))是完美的,但到了long t=RegQueryValueEx(hKey_tmp, L"DisplayName", NULL, &dwType, (BYTE *)&comNum, &dwSize);则部分成功。

 

后来发现 RegQueryValueEx的返回值除了SUCCESS之外,还有一个ERROR_MORE_DATA。ERROR_MORE_DATA故名思议是没有足够的空间导致的错误。在这里,是由于comNum没有足够的空间储存结果。故将if(t == ERROR_SUCCESS)
改为if((t == ERROR_SUCCESS)||(t == ERROR_MORE_DATA)),程序通过。

 

WinError.h中对ERROR_MORE_DATA的定义如下:

 

//
// MessageId: ERROR_MORE_DATA
//
// MessageText:
//
//  More data is available.
//
#define ERROR_MORE_DATA                  234L    // dderror

其中,234L中的L为长整型。
 

RegQueryValueEx中lpcbData 的有如下解释:

If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData.

意思是说当lpData 没有足够的空间储存结果时,返回ERROR_MORE_DATA,并且重置lpcbData的空间。

原创粉丝点击