我有以下代码,它从函数获取返回值char*
cDestDrive = ReadFromRegistry(HKEY_CURRENT_USER,NDSPATH,szDestPathRoot);
我能够读取里面的值,cDestDrive
直到我分配它。我分配它的那一刻:
CString strServerAddress = cDestDrive;
cDestDrive 的值被更改(损坏),我无法在CString strServerAddres
任何想法中获得值,为什么会发生这种情况。
编辑: 从注册表读取的代码
char* CNDSShellExtender::ReadFromRegistry(HKEY hKey,LPCTSTR lpNDS,LPSTR lpRegKey)
{
HKEY hRegKey=NULL;
if(hKey==NULL || lpNDS==""||lpNDS==NULL||lpRegKey==""||lpRegKey==NULL)
MessageBox(NULL,"Reading from Registry Failed!Invalid Path",
_T("Network Drive Solution"),
MB_ICONERROR);
LONG lOpenRes=RegOpenKey(hKey,lpNDS,&hRegKey);
if (lOpenRes!=ERROR_SUCCESS ||lpNDS==NULL)
MessageBox ( NULL, "Can not Find Any Server to Connect",
_T("NDSShellExtension"),
MB_ICONERROR );
if(lOpenRes==ERROR_SUCCESS && lpNDS!=NULL)
{
TCHAR tSZValue[MAX_PATH] = {0};
DWORD dwBufSize=MAX_PATH;
LONG lCloseOut;
LPBYTE lpStorage = reinterpret_cast<LPBYTE>(tSZValue);
char* cpRegKeyVal=tSZValue;
if (ERROR_SUCCESS == RegQueryValueEx(hRegKey,lpRegKey , 0, 0, (BYTE*)tSZValue, &dwBufSize))
{
lCloseOut= RegCloseKey(hRegKey);
if (lCloseOut != ERROR_SUCCESS)
MessageBox (NULL, "Registry Not Closed",
_T("NDSShellExtension"),
MB_ICONERROR );
return cpRegKeyVal;
}
else
{
lCloseOut= RegCloseKey(hRegKey);
if (lCloseOut != ERROR_SUCCESS)
MessageBox (NULL, "Registry Not Closed",
_T("NDSShellExtension"),
MB_ICONERROR );
return "";
}
}
return "";
}