Skip to content

Commit

Permalink
fix: 修复Windows下打开动态库路径错误
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 30, 2023
1 parent 5fc0b80 commit 18576ab
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions source/LibraryHolder/ControlUnit/ControlUnit.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
#include "LibraryHolder/ControlUnit.h"

#include <filesystem>

#include "ControlUnit/AdbControlUnitAPI.h"
#include "ControlUnit/DbgControlUnitAPI.h"
#include "ControlUnit/ThriftControlUnitAPI.h"
#include "ControlUnit/Win32ControlUnitAPI.h"
#include "Utils/Logger.h"

static std::filesystem::path library_dir;

#ifdef _WIN32
#include "Utils/SafeWindows.hpp"

void init_library_path(HINSTANCE hinstDLL)
{
char buffer[MAX_PATH + 1] = { 0 };
GetModuleFileName(hinstDLL, buffer, MAX_PATH);
library_dir = MAA_NS::path(buffer).parent_path();
}

// https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain
BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved) // reserved
{
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
init_library_path(hinstDLL);
break;

case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;

case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;

case DLL_PROCESS_DETACH:

if (lpvReserved != nullptr) {
break; // do not do cleanup if process termination scenario
}

// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
#endif

MAA_NS_BEGIN

template <typename ControlUnitT, typename GetVersionT>
Expand All @@ -30,8 +78,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> AdbControlUnitLibraryHolder::c
MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config,
MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg)
{
if (!load_library(libname_)) {
LogError << "Failed to load library" << VAR(libname_);
if (!load_library(library_dir / libname_)) {
LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -66,10 +114,11 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> AdbControlUnitLibraryHolder::c
}

std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> Win32ControlUnitLibraryHolder::create_control_unit(
MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg)
MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback,
MaaCallbackTransparentArg callback_arg)
{
if (!load_library(libname_)) {
LogError << "Failed to load library" << VAR(libname_);
if (!load_library(library_dir / libname_)) {
LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -105,8 +154,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> Win32ControlUnitLibraryHolder:
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::create_control_unit(
MaaDbgControllerType type, MaaStringView read_path)
{
if (!load_library(libname_)) {
LogError << "Failed to load library" << VAR(libname_);
if (!load_library(library_dir / libname_)) {
LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -142,8 +191,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::c
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> ThriftControlUnitLibraryHolder::create_control_unit(
MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config)
{
if (!load_library(libname_)) {
LogError << "Failed to load library" << VAR(libname_);
if (!load_library(library_dir / libname_)) {
LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_);
return nullptr;
}

Expand Down

0 comments on commit 18576ab

Please sign in to comment.