简体   繁体   English

如何为mingw32指定dll onload函数?

[英]How to specify dll onload function for mingw32?

I can compile DLLs properly using mingw and do the exports/imports stuff. 我可以使用mingw正确编译DLL并执行导出/导入操作。 What I am looking for is defining the dll onload function properly as you would in MS VC products. 我正在寻找的是正确定义dll onload功能,就像在MS VC产品中一样。 Google didn't turn up anything. 谷歌没有发现任何事情。 Anyone have any ideas or a link to a tutorial? 任何人有任何想法或教程的链接?

Okay, so after some fiddling...it's working. 好吧,经过一些摆弄......所以它正在发挥作用。 For anyone else that is having issues here it is. 对于其他任何有问题的人来说,这是。 My issues weren't related to compiling in instead of loading dynamically. 我的问题与编译无关,而不是动态加载。 It was a mash-up of a couple of tutorial/question/how-tos that got me to this point. 这是几个教程/问题/方法的混搭让我达到了这一点。

dll.c 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 ("Hello\n");
}

dll.h 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_ */

hello.c 你好ç


#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;
}

when compiled with 编译时


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

produces the expected output of 产生预期的产量


Load working...
Hello
Unload working...

Since mingw is just a windows port of GCC and associated tools, you can use GCC constructor and destructor attributes . 由于mingw只是GCC的Windows端口和相关工具,因此您可以使用GCC构造函数和析构函数属性 These work for both shared and static libraries, and execute code before and after main is run, respectively. 这些工作分别用于共享库和静态库,并分别在main运行之前和之后执行代码。 Additionally, you can specify multiple constructor and destructor functions per library. 此外,您可以为每个库指定多个构造函数和析构函数。

static void __attribute__((constructor))
your_lib_init(void)
{
    fprintf(stderr, "library init\n");
}

static void __attribute__((destructor))
your_lib_destroy(void)
{
    fprintf(stderr, "library destroy\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM