一段代码在VC6中写的demo没有错误,移到VS08中编译OK运行出现了上面的错误,找了很久也没有找到解决办法,希望看雪的大虾们指点指点。
完整代码:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <shlobj.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib")
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#include <tchar.h>
#include "resource.h"
/*//// 调试用 ///////////////////////////////////////////////////////*/
/*#define DEBUG(h, f, ...) \
{ \
TCHAR buf[256]; \
wsprintf(buf, f, __VA_ARGS__); \
MessageBox(h, buf, TEXT("DEBUG"), MB_OK); \
} */ \
/*//////////////////////////////////////////////////////////////////*/
//#pragma comment(linker,"\"/manifestdependency:type='win32' \
//name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
//processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
TCHAR szUninstall[] = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
static INT_PTR CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
static int cxSmallIcon;
static int cySmallIcon;
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow)
{
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icc);
cxSmallIcon = GetSystemMetrics(SM_CXSMICON);
cySmallIcon = GetSystemMetrics(SM_CYSMICON);
return DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, (DLGPROC)MainDlgProc, (LPARAM)hInstance);
}
typedef struct {
BYTE unknown[28];
WCHAR szPath[MAX_PATH];
} REG_DATA;
BOOLEAN QueryAppInfo(LPCTSTR szName, LPTSTR szDesc, LPTSTR szUninstaller)
{
HKEY hKey;
LONG lResult;
TCHAR szSubKey[2048];
DWORD dwType = REG_SZ;
DWORD dwType2 = REG_EXPAND_SZ;
DWORD dwSize = MAX_PATH;
BOOLEAN bResult = FALSE;
TCHAR szValue1[] = TEXT("DisplayName");
TCHAR szValue2[] = TEXT("UninstallString");
TCHAR szUninstall[] = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
wsprintf(szSubKey, TEXT("%s\\%s"), szUninstall, szName);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ, &hKey);
if (lResult == ERROR_SUCCESS) {
lResult = RegQueryValueEx(hKey, szValue1, NULL, &dwType, (LPBYTE)szDesc, &dwSize);
if (lResult == ERROR_SUCCESS) {
dwSize = MAX_PATH;
lResult = RegQueryValueEx(hKey, szValue2, NULL, &dwType, (LPBYTE)szUninstaller, &dwSize);
if (lResult == ERROR_SUCCESS) {
dwSize = MAX_PATH;
lResult = RegQueryValueEx(hKey, szValue2, NULL, &dwType2, (LPBYTE)szUninstaller, &dwSize);
if (lResult == ERROR_SUCCESS) {
LPTSTR psz = (LPTSTR)GlobalAlloc(GPTR, MAX_PATH * sizeof(TCHAR));
ExpandEnvironmentStrings(szUninstaller, psz, MAX_PATH);
lstrcpy(szUninstaller, psz);
GlobalFree(psz);
bResult = TRUE;
}
}
}
RegCloseKey(hKey);
}
return bResult;
}
HICON QueryAppIcon(LPCTSTR szName)
{
HKEY hKey;
LONG lResult;
HICON hIcon = NULL;
TCHAR szSubKey[2048];
TCHAR szIconPath[MAX_PATH];
DWORD dwType1 = REG_SZ;
DWORD dwType2 = REG_BINARY;
DWORD dwSize = MAX_PATH;
TCHAR szValue1[] = TEXT("DisplayIcon");
TCHAR szValue2[] = TEXT("SlowInfoCache");
TCHAR szArpCache[] = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Management\\ARPCache");
wsprintf(szSubKey, TEXT("%s\\%s"), szUninstall, szName);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ, &hKey);
if (lResult == ERROR_SUCCESS) {
lResult = RegQueryValueEx(hKey, szValue1, NULL, &dwType1, (LPBYTE)szIconPath, &dwSize);
if (lResult == ERROR_SUCCESS) {
if (*szIconPath != 0) {
int index = 0;
index = PathParseIconLocation(szIconPath);
ExtractIconEx(szIconPath, index, NULL, &hIcon, 1);
}
}
RegCloseKey(hKey);
}
if (hIcon == NULL) {
wsprintf(szSubKey, TEXT("%s\\%s"), szArpCache, szName);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ, &hKey);
if (lResult == ERROR_SUCCESS) {
lResult = RegQueryValueEx(hKey, szValue2, NULL, &dwType2, NULL, &dwSize);
if (lResult == ERROR_SUCCESS) {
REG_DATA regData;
ZeroMemory(®Data,sizeof(REG_DATA));
lResult = RegQueryValueEx(hKey, szValue2, NULL, &dwType2, (LPBYTE)®Data, &dwSize);
if (lResult == ERROR_SUCCESS) {
if (*regData.szPath != 0) {
ExtractIconExW(regData.szPath, 0, NULL, &hIcon, 1);
}
}
}
RegCloseKey(hKey);
}
}
if (hIcon == NULL) {
TCHAR szMsiExec[MAX_PATH];
if (SHGetSpecialFolderPath(NULL, szMsiExec, CSIDL_SYSTEM, FALSE)) {
if (PathAppend(szMsiExec, TEXT("msiexec.exe"))) {
if (PathFileExists(szMsiExec)) {
ExtractIconEx(szMsiExec, 0, NULL, &hIcon, 1);
}
}
}
}
return hIcon;
}
static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HKEY hKey;
LONG lResult;
DWORD cSubKeys;
LVCOLUMN lvc;
LVITEM lvi;
int cxList;
int iItem;
RECT rect;
TCHAR szKey[MAX_PATH];
HIMAGELIST himl;
static HWND hListView;
switch (uMsg) {
case WM_INITDIALOG:
hListView = GetDlgItem(hwndDlg, IDC_LIST);
ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT);
himl = ImageList_Create(cxSmallIcon, cySmallIcon, ILC_COLORDDB, 0, 0);
ListView_SetImageList(hListView, himl, LVSIL_SMALL);
GetClientRect(hListView, &rect);
cxList = rect.right - GetSystemMetrics(SM_CXVSCROLL);
lvc.mask = LVCF_WIDTH | LVCF_TEXT;
lvc.pszText = TEXT("名称");
lvc.cx = (int)(cxList * 0.3F);
ListView_InsertColumn(hListView, 0, &lvc);
lvc.pszText = TEXT("卸载程序");
lvc.cx = (int)(cxList * 0.7F);
ListView_InsertColumn(hListView, 1, &lvc);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szUninstall, 0, KEY_READ, &hKey);
if (lResult == ERROR_SUCCESS) {
lResult = RegQueryInfoKey(hKey, NULL, NULL, NULL, &cSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
if (lResult == ERROR_SUCCESS) {
UINT i;
iItem = 0;
for (i = 0; i < cSubKeys; i++) {
lResult = RegEnumKey(hKey, i, szKey, MAX_PATH);
if (lResult == ERROR_SUCCESS) {
TCHAR szName[MAX_PATH];
TCHAR szPath[MAX_PATH];
if (QueryAppInfo(szKey, szName, szPath)) {
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.iItem = iItem;
lvi.iSubItem = 0;
lvi.iImage = ImageList_AddIcon(himl, QueryAppIcon(szKey));
lvi.pszText = szName;
ListView_InsertItem(hListView, &lvi);
lvi.iSubItem = 1;
lvi.pszText = szPath;
SendMessage(hListView, LVM_SETITEMTEXT, iItem++, (LPARAM)&lvi);
}
}
}
}
RegCloseKey(hKey);
}
return FALSE;
/*
case WM_NOTIFY:
return TRUE;
case WM_COMMAND:
switch (wParam) {
case MAKEWPARAM(IDOK, BN_CLICKED):
EndDialog(hwndDlg, TRUE);
break;
}
return FALSE;
*/
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return FALSE;
default:
return FALSE;
}
}
为了方便大家调试我会附上完整工程,此代码的作用就是得到软件卸载信息。
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)