2013-11-15 122 views

回答

1

您可以使用此代碼來加載DLL和調用DLL導出的函數:

#include <windows.h> 
#include <iostream> 

/* Define a function pointer for our imported 
* function. 
* This reads as "introduce the new type f_funci as the type: 
*    pointer to a function returning an int and 
*    taking no arguments. 
*/ 
typedef int (*f_funci)(); 

int main() 
{ 
    HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop \\fgfdg\\dgdg\\test.dll"); 

    if (hGetProcIDDLL == NULL) { 
    std::cout << "could not load the dynamic library" << std::endl; 
    return EXIT_FAILURE; 
    } 

    # resolve function address here 
    f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "funci"); 
    if (!funci) { 
    std::cout << "could not locate the function" << std::endl; 
    return EXIT_FAILURE; 
    } 

    std::cout << "funci() returned " << funci() << std::endl; 

    return EXIT_SUCCESS; 
} 
1

從一個exe相對目錄加載DLL所有你需要做的就是在格式指定路徑的"\\mydlldir\\dllnamehere.dll",而不是完全合格的路徑"driveletter:\\dir\\dir2\\dirwithexeinit\\mydlldir\\dllnamehere.dll"

第一種方法將始終在從exe文件存在位置指定的目錄中查找,其中第二個方法將始終查找指定的確切目錄。