簡體   English   中英

C ++-如何使其他類可以使用顯式導入的DLL函數

[英]C++ - How do you make Explicitly Imported DLL functions available to other classes

我有一個名為mydll.dll的dll,該dll中有一個名為testFunc()的函數。 我想在它是GetProcAddress()的作用域之外的其他作用域中使用testFunc()

例如:

main.cpp中

#include <Windows.h>
typedef void(*f_testFunc)();
int main(){
    // load dll into hDll
    f_testFunc testFunc = (f_testFunc)GetProcAddress(hDll, "testFunc");

    for (int i = 0; i < NUM; i++){
        A a = A();
    }
}

A.cpp

class A{
    public:
    A(){
        testFunc();
    }
}

我只是想要一種在代碼中的任何地方使用testFunc()的方法,而不必從dll重新獲取它。

創建一個頭文件(myheader.h)。 在此處聲明函數變量,作為外部變量。 在所有源文件中包含此標頭。 明確定義變量並將其設置在main中。

myheader.h

typedef void(*f_testFunc)();
extern f_testFunc testFunc;

main.cpp中

#include "myheader.h"
f_testfunc testFunc;
int main () {
    testFunc = (f_testFunc)GetProcAddress(hDll, "testFunc");
    for (int i ...

A.cpp

#include "myheader.h"
class A {
    public:
    A () {
        testFunc();
    }
}

我試圖為提到的 DLL包裝器類制作示例

 typedef void(*f_testFunc)();

 class DllWrapper {

      DllWrapper(HDLL hDll) {
          testFunc_ = (f_testFunc)GetProcAddress(hDll, "testFunc");
      }
      void testFunc() {
          (*testFunc_)();
      }

 private:
      f_testFunc testFunc_;
 };

 class A {
 public:
     A(DllWrapper& dll) dll_(dll) {
          dll_.testFunc();
     }

 private:
     DllWrapper& dll_;
 };

int main(){
    // load dll into hDll
    DllWrapper dll(hDll);

    for (int i = 0; i < NUM; i++){
        A a = A(dll);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM