第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從DLL動(dòng)態(tài)加載函數(shù)

從DLL動(dòng)態(tài)加載函數(shù)

C++
慕森王 2019-08-30 17:43:54
我正在查看.dll文件,我了解它們的用法,我正在嘗試了解如何使用它們。我創(chuàng)建了一個(gè).dll文件,其中包含一個(gè)返回名為funci()的整數(shù)的函數(shù)使用此代碼,我(想)我已將.dll文件導(dǎo)入項(xiàng)目(沒有投訴):#include <windows.h>#include <iostream>int main() {  HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop  \\fgfdg\\dgdg\\test.dll");  if (hGetProcIDDLL == NULL) {    std::cout << "cannot locate the .dll file" << std::endl;  } else {    std::cout << "it has been called" << std::endl;    return -1;  }  int a = funci();  return a;}# funci function int funci() {  return 40;}但是,當(dāng)我嘗試編譯我認(rèn)為已導(dǎo)入.dll的.cpp文件時(shí),我遇到以下錯(cuò)誤:C:\Documents and Settings\User\Desktop\fgfdg\onemore.cpp||In function 'int main()':|C:\Documents and Settings\User\Desktop\fgfdg\onemore.cpp|16|error: 'funci' was not     declared in this scope|||=== Build finished: 1 errors, 0 warnings ===|我知道.dll與頭文件有所不同,所以我知道我可以;導(dǎo)入這樣的函數(shù),但這是我能想到的最好的表明我已經(jīng)嘗試過的。我的問題是,如何使用“hGetProcIDDLL”指針訪問.dll中的函數(shù)。我希望這個(gè)問題有道理,我再也不會(huì)咆哮一些錯(cuò)誤的樹了。
查看完整描述

2 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

LoadLibrary不會(huì)做你認(rèn)為它做的事情。它加載DLL到當(dāng)前進(jìn)程的內(nèi)存,但它并不會(huì)神奇地導(dǎo)入它定義的功能!這是不可能的,因?yàn)長oadLibrary在運(yùn)行時(shí)調(diào)用時(shí),鏈接器會(huì)在編譯時(shí)解析函數(shù)調(diào)用(請(qǐng)記住,C ++是一種靜態(tài)類型語言)。


您需要一個(gè)單獨(dú)的WinAPI函數(shù)來獲取動(dòng)態(tài)加載函數(shù)的地址:GetProcAddress。



#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.

 *

 * Make sure to use matching calling convention (__cdecl, __stdcall, ...)

 * with the exported function. __stdcall is the convention used by the WinAPI

 */

typedef int (__stdcall *f_funci)();


int main()

{

  HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop\\test.dll");


  if (!hGetProcIDDLL) {

    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;

}

此外,您應(yīng)該正確地從DLL 導(dǎo)出您的函數(shù)。這可以這樣做:


int __declspec(dllexport) __stdcall funci() {

   // ...

}

正如Lundin指出的那樣,如果您不再需要它,那么將句柄釋放到庫中是一種很好的做法。如果沒有其他進(jìn)程仍然擁有同一DLL的句柄,這將導(dǎo)致它被卸載。


查看完整回答
反對(duì) 回復(fù) 2019-08-30
  • 2 回答
  • 0 關(guān)注
  • 727 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)