不调用 CFileDialog ,阅读注册表似乎有效。
但是在调用 CFileDialog 之后(即使在没有调用任何文件的情况下打开和关闭之后),从注册表中读取也会失败。
以下是 CFileDialog 代码:
void CprojectTHD_2015_09_28Doc::OnImportFromTextfile()
{
if(!SaveFile()) return;
TCHAR szFilter[] = _T("INP Files (*.INP)|*.inp|OUT Files (*.OUT)|*.out||");
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, szFilter);
CString strPathName;
if(IDOK == dlg.DoModal()){
strPathName = dlg.GetPathName();
AdPinData *pInputDataTem = new AdPinData(); //to open new input data
if(pInputDataTem->OpenFile(strPathName)){ //to get Infos from text file
//to delete the last file Data when it needs a new file
if(pInputData){ delete pInputData; pInputData=NULL; }
if(pOutputData){ delete pOutputData; pOutputData=NULL; }
if(pBusData){ delete pBusData; pBusData=NULL; }
pInputData = pInputDataTem;
pInputDataTem = NULL;
//to open new output data
pOutputData = new AdPinData();
pOutputData->initAdPinData(false); //to just initialize, it doesn't need text file
//to open new bus data
pBusData = new AdBusData();
pBusData->initAdBusData();
for(int i=1;i<6;i++) MyEnableMenu(true, i); //on all the menu above
endofTick = 0; //set basic value 0
SignalView::isOverwritten = false;
UpdateAllViews(NULL); //to execute Ondraw funcion agian
}else{
delete pInputDataTem; pInputDataTem=NULL;
if(!pInputData) AdPinData::setFlag(false);
}
}
}
而且,这是从注册表中读取的代码:
CString CSerialPort::GetPortName()
{
DWORD dwcSubKeys, dwcValues, dwcMaxSubKeyName, dwcMaxValueName, dwcMaxValueData;
TCHAR szBuffer[255] = {0};
DWORD dwSubKeyIndex = 0;
DWORD dwValueIndex = 0;
HKEY hKey = 0;
LONG lStatus = ERROR_SUCCESS;
TCHAR szValueName[255] = {0};
TCHAR szValueData[255] = {0};
lStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"),0,KEY_READ/*KEY_ALL_ACCESS*/,&hKey);
if (lStatus == ERROR_SUCCESS) {
RegQueryInfoKey(hKey, szBuffer, NULL, 0,
&dwcSubKeys, &dwcMaxSubKeyName, NULL,
&dwcValues, &dwcMaxValueName, &dwcMaxValueData,
NULL, NULL);
if (dwcValues != 0) {
do {
DWORD dwType;
DWORD dwcValueName = sizeof(szValueName);
DWORD dwcValueData = sizeof(szValueData);
lStatus = RegEnumValue(hKey, dwValueIndex,
szValueName, &dwcValueName,
NULL, &dwType,
(BYTE*)szValueData,
&dwcValueData);
dwValueIndex++;
} while (lStatus == ERROR_SUCCESS);
}
RegCloseKey(hKey);
}
return szValueData;
}
P.S。 我将代码更改为:
CString CSerialPort::GetPortName()
{
DWORD dwcSubKeys,dwcValues,dwcMaxSubKeyName,dwcMaxValueName,dwcMaxValueData;
dwcSubKeys=dwcValues=dwcMaxSubKeyName=dwcMaxValueName=dwcMaxValueData=0; ///
DWORD dwSubKeyIndex = 0;
DWORD dwValueIndex = 0;
HKEY hKey = 0;
LONG lStatus = ERROR_SUCCESS;
TCHAR szValueName[255] = {0};
TCHAR szValueData[255] = {0};
lStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE,/*TEXT*/_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),0,KEY_READ,&hKey);
if (lStatus == ERROR_SUCCESS) {
RegQueryInfoKey(hKey, NULL/*szBuffer*/, NULL, 0,
&dwcSubKeys, &dwcMaxSubKeyName, NULL,
&dwcValues, &dwcMaxValueName, &dwcMaxValueData,
NULL, NULL);
if (dwcValues != 0) {
do {
DWORD dwcValueName = sizeof(szValueName);
DWORD dwcValueData = sizeof(szValueData);
lStatus = RegEnumValue(hKey, dwValueIndex,
szValueName, &dwcValueName,
NULL, NULL, ///
(BYTE*)szValueData,
&dwcValueData);
dwValueIndex++;
} while (lStatus == ERROR_SUCCESS);
}
RegCloseKey(hKey);
}
return szValueData;
}
我不使用我不需要的值,似乎比以前更好。至少它在任何情况下都有效,但是当它处于拔出状态(USB)时,它仍然会得到dwcValues = 1,dwcMaxValueName = 17,&amp; dwcMaxValueData = 10。