2011-01-06 70 views
0

我想获得一个.dll函数的句柄。我创建了一个CreateToolHelp32Snapshot,然后枚举模块,直到找到我想要的模块,然后从那个.dll文件中找到一个特定的函数。如何正确调用GetProcAddress(),以便在'that'.dll中获取函数,而不是可能正在运行的另一个实例?使用CreateToolHelp32Snapshot找到一个加载的DLL,在DLL中找到一个函数,然后调用它,GetProcAddress

从上述问题的延续将是,好吧,所以我有一个函数的句柄,我怎么实际调用它?

编辑:正如已经指出。我已经在第三方应用地址空间。如果getprocaddress不起作用,我如何使用readprocessmemory和必要的偏移量来获取函数的入口点?

谢谢。

HANDLE h_th_32snap = CreateToolhelp32Snapshot(0x8u, pid); 
if(h_th_32snap == INVALID_HANDLE_VALUE) 
    { 
    printError(TEXT("CreateToolhelp32Snapshot (of modules)")); 
    return(FALSE); 
    } 

    // Set the size of the structure before using it. 
    me32.dwSize = sizeof(MODULEENTRY32); 

    // Retrieve information about the first module, 
    // and exit if unsuccessful 
    if(!Module32First(h_th_32snap, &me32)) 
    { 
    printError(TEXT("Module32First")); // show cause of failure 
    CloseHandle(h_th_32snap);   // clean the snapshot object 
    return(FALSE); 
    } 

    // Now walk the module list of the process, 
    // and display information about each module 

    BYTE *d_pointer_qtgui4_dll = 0x0; 
    do 
    { 
    _tprintf(TEXT("\n\n  MODULE NAME:  %s"), me32.szModule); 
    _tprintf(TEXT("\n  Executable  = %s"),  me32.szExePath); 
    _tprintf(TEXT("\n  Process ID  = 0x%08X"),   me32.th32ProcessID); 
    _tprintf(TEXT("\n  Ref count (g) = 0x%04X"),  me32.GlblcntUsage); 
    _tprintf(TEXT("\n  Ref count (p) = 0x%04X"),  me32.ProccntUsage); 
    _tprintf(TEXT("\n  Base address = 0x%08X"), (DWORD) me32.modBaseAddr); 
    _tprintf(TEXT("\n  Base size  = %d"),    me32.modBaseSize); 

    if(!wcsncmp(me32.szModule, L"QtGui4.dll", 255)) 
    { 

       FARPROC test = GetProcAddress(GetModuleHandle(L"QtGui4.dll"),"[email protected]@@[email protected]@[email protected]"); 

    } 

    } while(Module32Next(h_th_32snap, &me32)); 

    CloseHandle(h_th_32snap); 

格雷格,我想知道为什么这是错误的?它不会抛出任何错误,但它也不起作用!

函数原型:

QWidget * QWidget::find (WId id) [static]; 

我试图把它叫做:

hDLL = GetModuleHandle(L"QtGui4.dll"); 
if (hDLL != NULL) 
{ 

    func pointer_find = (func)GetProcAddress(hDLL,"[email protected]@@[email protected][email protected]@@Z"); 

    if (!pointer_find) 
    { 
     // handle the error 
     FreeLibrary(hDLL);  
     //return SOME_ERROR_CODE; 
    } 
    else 
    { 
     // call the function 
     widget = pointer_find(my_hwnd); 
    } 
} 
+0

这是在您的应用程序,还是自动化第二个应用程序?试图自动化第二个应用的 – 2011-01-06 19:47:04

+0

。 – flavour404 2011-01-06 20:09:39

回答

1

不可能的,GetProcAddress的()需要一个模块句柄。 HMODULE只在获取它的过程中有效。您必须执行GetProcAddress()所做的相同操作,迭代IAT以查找入口点。并应用基地址偏移量。由于无法直接访问内存来读取IAT,因此这对于另一个进程来说是非常痛苦的。 ReadProcessMemory是必需的。

注入目标进程中的代码是唯一合理的方法。这也需要做我认为你接下来要做的事情,调用函数。代码注入技术覆盖了codeproject.com

+0

我已经在进程地址空间(这是一个注入的DLL里面),我对readprocessmemory非常熟悉,以前没有做过的是我基本问的问题,如何获取函数的入口点。所以使用readprocessmemory,我如何找到并调用函数? – flavour404 2011-01-06 20:07:15

0

如果你正在处理你几乎在那里。

与LoadLibrary相比,GetModuleHandle将获取当前加载的模块句柄,LoadLibrary将加载模块(并增加ref编号)。只需要该功能的正确原型。

typedef void __thiscall (QListView::*rowsInserted)(class QModelIndex const &,int,int); 

rowsInserted test = (rowsInserted)GetProcAddress(GetModuleHandle(L"QtGui4.dll"),"[email protected]@@[email protected]@[email protected]"); 

//QListView *object 
if(test && object) 
    (object.*test)(my_QModelIndex, int_x, int_y);