#include <QtCore>#include <windows.h>int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); LONG retVal=0; DWORD Index=0; DWORD dwTypeCode; char KeyParam[256]; char KeyValue[256]; DWORD dwKeyParamLength = sizeof(KeyParam); DWORD dwKeyValueLength = sizeof(KeyValue); LONG lnResult; HKEY hKey; retVal=RegOpenKeyExA(HKEY_LOCAL_MACHINE,"Hardware\\Devicemap\\Serialcomm", 0, KEY_READ, &hKey); qDebug() << "RegOpenKeyExA =" << retVal; while (RegEnumValueA(hKey, Index, KeyParam, &dwKeyParamLength, NULL, &dwTypeCode, (BYTE *)KeyValue, &dwKeyValueLength) == ERROR_SUCCESS) { if(dwTypeCode == REG_SZ) { qDebug() << "Index=" << Index; qDebug() << "KeyParam=" << KeyParam; qDebug() << "dwKeyParamLength=" << dwKeyParamLength; qDebug() << "dwTypeCode=" << dwTypeCode; qDebug() << "KeyValue=" << KeyValue; qDebug() << "dwKeyValueLength=" << dwKeyValueLength; } ++Index; dwKeyParamLength = sizeof(KeyParam); dwKeyValueLength = sizeof(KeyValue); } retVal=RegCloseKey(hKey); qDebug() << "RegCloseKey=" << retVal; return app.exec();}
C++ (Qt)BOOL GetComputerName_G1(LPTSTR lpBuffer,ULONG* lpnSize){ BOOL result = FALSE; if( lpBuffer && lpnSize ) { char kname[MAX_PATH]= "SYSTEM\\ControlSet001\\Control\\ComputerName\\ComputerName"; char cComputerName[MAX_PATH]="ComputerName"; CRegKey regKey; if( regKey.Open(HKEY_LOCAL_MACHINE,kname) == ERROR_SUCCESS ) { result = (regKey.QueryStringValue(cComputerName,lpBuffer,lpnSize) == ERROR_SUCCESS) ; regKey.Close(); } } return(result);} //--------------------------------------------------------using: char cn[MAX_PATH]={'\0'}; ULONG sz = MAX_PATH; BOOL b1 = GetComputerName_G1(cn,&sz);//-------------------------------------------------------- BOOL GetComputerName_G2(LPTSTR lpBuffer,DWORD* lpnSize){ BOOL result = FALSE; if( lpBuffer && lpnSize ) { char kname[MAX_PATH]= "SYSTEM\\ControlSet001\\Control\\ComputerName\\ComputerName"; char cComputerName[MAX_PATH]="ComputerName"; HKEY hKey; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, kname, 0, KEY_READ, &hKey) == ERROR_SUCCESS) { result = (RegQueryValueEx( hKey, cComputerName, NULL, NULL, (LPBYTE)lpBuffer, lpnSize) == ERROR_SUCCESS) RegCloseKey(hKey); } } return(result);}//--------------------------------------------------------using: char cn[MAX_PATH]={'\0'}; DWORD sz = MAX_PATH; BOOL b2 = GetComputerName_G2(cn,&sz);//--------------------------------------------------------