怎样得到注册表项的路径

siammy 2010-01-08 12:51:08
能不能像CFileDialog那样,用鼠标选择注册表项,和得到他的路径?
...全文
79 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinhualuo 2010-01-08
  • 打赏
  • 举报
回复
void COfficePathDlg::OnPath()
{
LPOLESTR szwApp;
char szLocation[255], szApp[50];

// Get ProgID from EditBox
m_ProgID.GetWindowText(szApp, 50);
if (strcmp(szApp,"") == 0)
{
MessageBox("Type ProgID in EditBox", "Error");
return;
}
// Find number ofcharacters to be allocated
int len = strlen(szApp) + 1;

// Use OLE Allocator to allocate memory
szwApp = (LPOLESTR) CoTaskMemAlloc(len*2);
if (szwApp == NULL)
{
MessageBox("Out of Memory", "Error");
return;
}

// AnsiToUnicode conversion
if (0 == MultiByteToWideChar(CP_ACP, 0, szApp, len,
szwApp, len))
{
// Free Memory allocated to szwApp if conversion failed
CoTaskMemFree(szwApp);
szwApp = NULL;
MessageBox("Error in Conversion", "Error");
return;
}

// Get Path to Application and display it
if (!GetPath(szwApp, szLocation, 255))
MessageBox("Error Getting Path", "Error");
else
MessageBox(szLocation, "Path");
}

void COfficePathDlg::OnVersion()
{
CHAR szVersion[255], szApp[50];
// Get Version of Application
m_ProgID.GetWindowText(szApp, 50);

if (strcmp(szApp,"") == 0)
{
MessageBox("Type ProgID in EditBox", "Error");
return;
}

// Get Version and display it
if (!GetVersion(szApp, szVersion, 255))
MessageBox("Error Getting Version", "Error");

else
MessageBox(szVersion, "Version");
}

BOOL COfficePathDlg::GetPath(LPOLESTR szApp, LPSTR szPath, ULONG cSize)
{
CLSID clsid;
LPOLESTR pwszClsid;
CHAR szKey[128];
CHAR szCLSID[60];
HKEY hKey;

// szPath must be at least 255 char in size
if (cSize < 255)
return FALSE;

// Get the CLSID using ProgID
HRESULT hr = CLSIDFromProgID(szApp, &clsid);
if (FAILED(hr))
{
AfxMessageBox("Could not get CLSID from ProgID, Make sure ProgID is correct", MB_OK, 0);
return FALSE;
}

// Convert CLSID to String
hr = StringFromCLSID(clsid, &pwszClsid);
if (FAILED(hr))
{
AfxMessageBox("Could not convert CLSID to String", MB_OK, 0);
return FALSE;
}

// Convert result to ANSI
WideCharToMultiByte(CP_ACP, 0, pwszClsid, -1, szCLSID, 60, NULL, NULL);

// Free memory used by StringFromCLSID
CoTaskMemFree(pwszClsid);

// Format Registry Key string
wsprintf(szKey, "CLSID\\%s\\LocalServer32", szCLSID);

// Open key to find path of application
LONG lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, szKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS)
{
// If LocalServer32 does not work, try with LocalServer
wsprintf(szKey, "CLSID\\%s\\LocalServer", szCLSID);
lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, szKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS)
{
AfxMessageBox("No LocalServer Key found!!", MB_OK, 0);
return FALSE;
}
}

// Query value of key to get Path and close the key
lRet = RegQueryValueEx(hKey, NULL, NULL, NULL, (BYTE*)szPath, &cSize);
RegCloseKey(hKey);
if (lRet != ERROR_SUCCESS)
{
AfxMessageBox("Error trying to query for path", MB_OK, 0);
return FALSE;
}

// Strip off the '/Automation' switch from the path
char *x = strrchr(szPath, '/');
if(0!= x) // If no /Automation switch on the path
{
int result = x - szPath;
szPath[result] = '\0'; // If switch there, strip it
}
return TRUE;
}

BOOL COfficePathDlg::GetVersion(LPCTSTR szApp, LPSTR szVersion, ULONG cSize)
{
CHAR szKey[128], szValueName[128];
HKEY hKey, hKey1;

wsprintf(szKey, "%s", szApp);
LONG lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, szKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS) {
// Word is registered, but no local server can be found!!!
AfxMessageBox("Could not get CLSID from ProgID, Make sure ProgID is correct", MB_OK, 0);
return FALSE;
}

wsprintf(szValueName, "%s", "CurVer");
lRet = RegOpenKeyEx(hKey, szValueName, 0, KEY_ALL_ACCESS, &hKey1);
if (lRet != ERROR_SUCCESS) {
// Excel is registered, but no local server can be found!!!
return FALSE;
}

// Get the Version information
lRet = RegQueryValueEx(hKey1, NULL, NULL, NULL, (BYTE*)szVersion, &cSize);

// Close the registry keys

RegCloseKey(hKey1);
RegCloseKey(hKey);

// Error while querying for value
if (lRet != ERROR_SUCCESS)
return FALSE;

// At this point szVersion contains the ProgID followed by a number.
// For example, Word 97 will return Word.Application.8 and Word 2000 will return Word.Application.9


// Store the version number
char *x = strrchr(szVersion, '.');
szVersion[0] = *(x + 1);
szVersion[1] = '\0';

if (strcmp(szVersion, "6") == 0)
strcpy(szVersion, "Version 95");
else if (strcmp(szVersion, "8") == 0)
strcpy(szVersion, "Version 97");
else if (strcmp(szVersion, "9") == 0)
strcpy(szVersion, "Version 2000");
else if (strcmp(szVersion,”1”) == 0)
strcpy(szVersion, “Version 2002”);

else
strcpy(szVersion, "Version unknown");
return TRUE;

}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧