大家看一下这个FUNCTION有没有错
int CAlterTableDlg::GetValueFromProfile(CString &strValue,CString &strSubValue)
{
HKEY hKey; long ret=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,strValue,0, KEY_READ,&hKey); if(ret!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行
{
MessageBox("错误: 查询无法打开有关的hKEY!");
return 0;
} unsigned char tmp[256]="\0";
DWORD type=REG_SZ;
DWORD size=80;
ret=::RegQueryValueEx(hKey,strSubValue, NULL,&type,tmp,&size);
if(ret!=ERROR_SUCCESS)
{
MessageBox("错误:无法查询有关注册表信息!");
return 0;
}
CString tmpstring=CString(tmp);
m_strDBDir =tmpstring ;
strFileName =tmpstring;
::RegCloseKey(hKey); return 1;
}

解决方案 »

  1.   

    ret=::RegQueryValueEx(hKey,strSubValue, NULL,&type,tmp,&size);

    ret=::RegQueryValueEx(hKey,strSubValue.GetBuffer(strSubValue.GetLength()), NULL,&type,tmp,&size);
    strSubValue.ReleaseBuffer();
    如果要读注册表某个值,不用费这么大劲
    在CXXXApp类里面的InitInstance里面的SetRegistryKey改成你想要的键
    然后使用
    CWinApp::GetProfileInt等几个函数就可以了
      

  2.   

    CWinApp::m_pszRegistryKey
    LPCTSTR m_pszRegistryKey;ResUsed to determine where, in the registry or INI file, application profile settings are stored. Normally, this data member is treated as read-only. Registry entries are stored as follows: In Windows NT, the value is stored to a registry key. The name for the application profile setting is appended to the following registry key: HKEY_CURRENT_USER/Software/LocalAppWizard-Generated/.
    In Windows 3.x, the value is stored in the WIN.INI file. 
    In Windows 95, the value is stored in a cached version of WIN.INI. 
    If you assign a value to m_pszRegistryKey, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example://First free the string allocated by MFC at CWinApp startup.
    //The string is allocated before InitInstance is called.
    free((void*)m_pszRegistryKey);
    //Change the name of the registry key.
    //The CWinApp destructor will free the memory.
    m_pszRegistryKey=_tcsdup(_T(“HKEY_CURRENT_USER\\Software\\mycompany\\myapp\\thissection\\thisvalue”));