首页
社区
课程
招聘
[求助]Run-Time check failure #2 stack around the variable 'regData' was corrupted该如何解决?
发表于: 2012-9-6 11:49 14897

[求助]Run-Time check failure #2 stack around the variable 'regData' was corrupted该如何解决?

2012-9-6 11:49
14897
一段代码在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期)

上传的附件:
收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
2
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);
}
}

知道问题是在这块,但是真心找不出来。。。。悲哀。
2012-9-6 12:06
0
雪    币: 165
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
瞎改了一下,貌似调试没问题了
上传的附件:
2012-9-6 12:38
0
雪    币: 165
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
另外应对这种越界,貌似你下那么多断点没啥意义...不懂调试的菜鸟撸过
2012-9-6 12:39
0
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
5
问题已经解决,感谢Y兄
2012-9-6 13:23
0
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
6
YZ兄,我顺便罗嗦一句吧:本身是读取注册表中的信息来得到图标的获取方法,读取的注册表项为
SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Management\\ARPCache"里面的每个SlowInfoCache子项,这个子项的内容有些特殊,前面28个byte表示的貌似是大小之类的信息,从第29个byte开始是程序的路径了(见图片),因此你上面的代码我进行了修改,使得可以正确显示出来图标。
            if (lResult == ERROR_SUCCESS) {
				WCHAR *szData=(WCHAR*)malloc(dwSize*sizeof(WCHAR)+2);
				ZeroMemory(szData,dwSize*sizeof(WCHAR));
                lResult = RegQueryValueEx(hKey, szValue2, NULL, &dwType2, (LPBYTE)szData, &dwSize);
                if (lResult == ERROR_SUCCESS) {
					if(szData!=NULL){
                        ExtractIconExW(szData+14, 0, &hIcon, NULL, 1);
                    }
                }
            }
            RegCloseKey(hKey);

由于你用的WCHAR,因此这里偏移是28/2=14,这样可以取得正确图标了。。

2012-9-6 13:44
0
游客
登录 | 注册 方可回帖
返回
//