【UE4】調用外部鏈接庫:dll動態庫


加載

  • 方式一
    // dll內的函數引用聲明
    typedef void(*_Initial)(
    char* thisPCID,
    char* thisConnectioninfo,
    char* thisExpVersion,
    char* thisSessionlD
    );
    
    void *DLLHandle;
    _Initial DLL_Initial;
      //加載
    bool UDLLTest::DLL_Init() {
        FString filePath = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("DLL/"), TEXT("test.dll"));
        if (FPaths::FileExists(filePath)) {
            UE_LOG(LogTemp, Log, TEXT("%s"), *filePath);
            // 綁定dll
            DLLHandle = FPlatformProcess::GetDllHandle(*filePath);
            if (DLLHandle != NULL) {
                DLL_Initial=NULL;
               
                //綁定函數
                FString procName = "Initial"; // dll里的函數名
                DLL_Initial = (_Initial)FPlatformProcess::GetDllExport(DLLHandle, *procName); 
                if(DLL_Initial != NULL){
                    UE_LOG(LogTemp, Log, TEXT("DLL_Initial import succ"));
                    return true;
                }     
            }
        }
        return false;
    }
    
    //釋放
    void UDLLTest::DLL_Free() {
        if(DLLHandle !=NULL){
            DLL_Initial = NULL;
            FPlatformProcess::FreeDllHandle(DLLHandle );
            DLLHandle = NULL;
        }
    }
    
  • 方式二 (當使用方法一引用不到 dll 的時候,可以嘗試使用該方法)
    SetDllDirectory(*filePath);
    DllHandle = LoadLibrary(TEXT("test.dll"));
    ...
    

運行時依賴性

  • 打包游戲時為在可執行文件旁暫存第三方DLL,可在 build.cs中聲明其為運行時依賴性。
    RuntimeDependencies.Add(Path.Combine(PluginDirectory, "Binaries/Win64/Foo.dll"));
    
  • 此操作假設DLL已存在於給定目錄中,插件將在該位置手動進行加載。若希望在編譯時將DLL復制到可執行文件使用的相同輸出目錄,可通過重載 RuntimeDependencies.Add 方法執行。
    RuntimeDependencies.Add("$(TargetOutputDir)/Foo.dll", Path.Combine(PluginDirectory, "Source/ThirdParty/bin/Foo.dll"));
    

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM