加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何为mingw32指定dll onload函数?

发布时间:2020-12-14 05:46:20 所属栏目:Windows 来源:网络整理
导读:我可以使用mingw正确编译DLL并执行导出/导入操作.我正在寻找的是正确定义dll onload功能,就像在MS VC产品中一样.谷歌没有发现任何事情.任何人有任何想法或教程的链接? 解决方法 好吧,经过一些摆弄……所以它正在发挥作用.对于其他任何有问题的人来说,这是.
我可以使用mingw正确编译DLL并执行导出/导入操作.我正在寻找的是正确定义dll onload功能,就像在MS VC产品中一样.谷歌没有发现任何事情.任何人有任何想法或教程的链接?

解决方法

好吧,经过一些摆弄……所以它正在发挥作用.对于其他任何有问题的人来说,这是.我的问题与编译无关,而不是动态加载.这是几个教程/问题/方法的混搭让我达到了这一点.

dll.c

#include <stdio.h>
#include <windows.h>
#include "dll.h"

//extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD Reason,LPVOID LPV) {
//This one was only necessary if you were using a C++ compiler

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved) {

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // Code to run when the DLL is loaded
        printf ("Load working...n");
            break;

        case DLL_PROCESS_DETACH:
            // Code to run when the DLL is freed
        printf ("Unload working...n");
            break;

        case DLL_THREAD_ATTACH:
            // Code to run when a thread is created during the DLL's lifetime
        printf ("ThreadLoad working...n");
            break;

        case DLL_THREAD_DETACH:
            // Code to run when a thread ends normally.
        printf ("ThreadUnload working...n");
            break;
    }

    return TRUE;
} 

EXPORT void hello(void) {
    printf ("Hellon");
}

dll.h

#ifndef DLL_H_
#define DLL_H_

#ifdef BUILD_DLL
/* DLL export */
#define EXPORT __declspec(dllexport)
#else
/* EXE import */
#define EXPORT __declspec(dllimport)
#endif

EXPORT void hello(void);

#endif /* DLL_H_ */

你好?

#include <windows.h>
#include <stdio.h>

int main () {

    /*Typedef the hello function*/
    typedef void (*pfunc)();

    /*Windows handle*/
    HANDLE hdll;

    /*A pointer to a function*/
    pfunc hello;

    /*LoadLibrary*/
    hdll = LoadLibrary("message.dll");

    /*GetProcAddress*/
    hello = (pfunc)GetProcAddress(hdll,"hello");

    /*Call the function*/
    hello();
    return 0;
}

编译时

gcc -c -DBUILD_DLL dll.c
gcc -shared -o message.dll dll.o -Wl,--out-implib,libmessage.a
gcc -c hello.c
gcc -o hello.exe hello.o message.dll

产生预期的产量

Load working...
Hello
Unload working...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读