从功能返回结构

时间:2012-06-28 10:44:56

标签: c++

你好我的程序中有两段代码:

PIMAGE_IMPORT_DESCRIPTOR PE::GetImportedLibInfo(LPSTR libName )
{
    PIMAGE_DOS_HEADER doshdr = (PIMAGE_DOS_HEADER)EntryPoint;
    PIMAGE_NT_HEADERS nthdr = (PIMAGE_NT_HEADERS)((DWORD)doshdr + doshdr->e_lfanew);
    DWORD tmp =nthdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
    DWORD Rva = RvaToOffset((DWORD)tmp);
    if(Rva != -1)
    {
        Rva += EntryPoint; 
        PIMAGE_IMPORT_DESCRIPTOR iid =(PIMAGE_IMPORT_DESCRIPTOR)(DWORD)Rva;

        while(iid->Characteristics)
        {
            char* dll = (char*)((DWORD_PTR)RvaToOffset(iid->Name)+ EntryPoint);
            DWORD res=lstrcmp((LPCSTR)dll,(LPCSTR)libName);
            if(res == 0)
            {
                return iid;
            }
            iid ++;
        }
    }
    return NULL;
}


VOID FillLibInfo(PIMAGE_IMPORT_DESCRIPTOR iiD)
{
    if(iiD != NULL)
    {
        char* buff[20];
        wsprintf((LPSTR)&buff,"%08lX",(DWORD)iiD->OriginalFirstThunk);
        SetDlgItemText(hImpDlg,IDC_EDIT1,(LPCSTR)&buff);
        wsprintf((LPSTR)&buff,"%08lX",(DWORD)iiD->TimeDateStamp);
        SetDlgItemText(hImpDlg,IDC_EDIT2,(LPCSTR)&buff);
        wsprintf((LPSTR)&buff,"%08lX",(DWORD)iiD->ForwarderChain);
        SetDlgItemText(hImpDlg,IDC_EDIT3,(LPCSTR)&buff);
        wsprintf((LPSTR)&buff,"%08lX",(DWORD)iiD->FirstThunk);
        SetDlgItemText(hImpDlg,IDC_EDIT4,(LPCSTR)&buff);
    }
}

然后我使用它:

FillLibInfo(GetImportedLibInfo("MyLibName"));

附加内容是我的文本框在我通过鼠标悬停它们之前不会实现文本

并且在几次调用GetImportedLibInfo()程序崩溃之后。 我认为这是堆栈损坏的东西......

有人可以给我一个提示吗?

@Edit:

Class PE defenition:

class PE
{
private:
DWORD ptrImgDosHeader;
DWORD RvaToOffset(DWORD Rva);
DWORD RvaToMemory(DWORD Rva);

public:
DWORD EntryPoint;
PE(DWORD ptrMemory);
~PE();
VOID EnumSections(BOOL (*ptrCallBack)(PIMAGE_SECTION_HEADER));
VOID EnumImports(BOOL (*ptrCallBack)(LPSTR,DWORD),DWORD);
VOID EnumImportedFunctionsFromLib(LPSTR,BOOL (*ptrCallBack)(LPSTR,LPSTR));
VOID EnumExportedFunctions(BOOL (*ptrCallBack)(LPSTR,LPSTR,LPSTR));
WORD GetPeType();
DWORD ValidatePE();
DWORD ValidateNtHeader();
PIMAGE_IMPORT_DESCRIPTOR GetImportedLibInfo(LPSTR lib);
};

1 个答案:

答案 0 :(得分:2)

你正在写一个字符串到一个char指针数组,而不是一个char数组,所以你要写一些随机指针(无论未初始化数组的第一个元素指向哪个,这将是未分配的内存)。

尝试使用char buff[20]代替char* buff[20],然后使用wsprintf( buff, ... )SetDlgItemText( ..., buff )