使用DllMain函数(MinGW + CMake)创建Windows共享库 [英] Create a Windows shared library with DllMain function (MinGW + CMake)

查看:407
本文介绍了使用DllMain函数(MinGW + CMake)创建Windows共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉是否已经提出此要求,但是我已经尝试寻找答案已有很多星期了.

I apologise if this has already been asked, but I have been trying to find an answer for many weeks now.

对于共享库,我并不特别称职,但是,将CLion RC 1.0(及其集成的CMake)与MinGW结合使用时,我无法创建具有DllMain功能的功能性Windows DLL .我确实创建了一个.dll文件,但很快就发现该符号未正确导出.

I am not particularly competent when it comes to shared libraries, however, using CLion RC 1.0 (and its integrated CMake) with MinGW, I have been unable to create a functional Windows DLL with a DllMain function. I did manage to create a .dll file, but it quickly became apparent that the symbol did not export corrrectly.

简而言之,我希望看到与Visual Studio默认Win32 DLL模板所获得的行为相同或相似的行为.

Simply put, I would like to see identical or similar behaviour to what is obtained with Visual Studio's default Win32 DLL template.

CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(DllMainTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_library(DllMainTest SHARED ${SOURCE_FILES})

main.cpp

#include <windows.h>

extern "C" BOOL WINAPI DllMain(
    HINSTANCE hinstDLL,
    DWORD fdwReason,
    LPVOID lpvReserved
) {
switch(fdwReason) {
    case DLL_PROCESS_ATTACH:
        MessageBox(NULL, "It works!", "Status", MB_OK);
    case DLL_PROCESS_DETACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    default:
        break;
    }
return TRUE;
}

编辑: 此DLL的目的是将其注入到32位进程中,并使其在加载时显示MessageBox.

EDIT: The intention for this DLL is to have it injected into a 32-bit process and for it to display a MessageBox on load.

推荐答案

查看add_library文档:

check out the add_library documentation: http://www.cmake.org/cmake/help/v3.0/command/add_library.html

"STATIC库是链接其他目标时使用的目标文件的归档.SHARED库是动态链接的,并在运行时加载.MODULE库是未链接到其他目标的插件,但可以在运行时使用类似dlopen的方式动态加载.功能"

"STATIC libraries are archives of object files for use when linking other targets. SHARED libraries are linked dynamically and loaded at runtime. MODULE libraries are plugins that are not linked into other targets but may be loaded dynamically at runtime using dlopen-like functionality"

add_library(DllMainTest MODULE ${SOURCE_FILES})

应按预期导出DllMain.

should export the DllMain as intended.

这篇关于使用DllMain函数(MinGW + CMake)创建Windows共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆