导出功能,投进去的指针通过DLL [英] Exporting a function, cast into a pointer through a DLL

查看:109
本文介绍了导出功能,投进去的指针通过DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个阐述前面一个问题:<一href=\"http://stackoverflow.com/questions/33519285/how-to-reset-state-machines-when-unit-testing-c\">How复位状态机时,单元测试C 结果
还有一个类似的问题,但我不回答符合我的问题,我有一些例子我想present:的从DLL导出函数指针

我有两套code的,我希望也应该这样做,但后者的崩溃。
使用的mingw32和Win7。

要导出的函数。这被认为是传统和unmutable。结果
addxy.c

  INT addXY(INT X,int y)对
    {
      返回X + Y;
    }

addxy.h

  INT addXY(INT X,int y)对;

的工作示例

的main.c

 的#include&LT;&stdio.h中GT;
    #包括addxy.h    的typedef INT(__cdecl * addXYWrap_t)(int类型的,INT B);    addXYWrap_t addXYWrap =(addXYWrap_t)addXY;    无效的主要()
    {
      输出(结果:%d个,addXYWrap(3,4));
    }

屈服

 结果:7

的崩溃例子

addxydll.c

 的#include&LT;&stdio.h中GT;
    #包括addxy.h    的typedef INT(__cdecl * addXYWrap_t)(int类型的,INT B);    __declspec(dllexport)的addXYWrap_t addXYWrap =(addXYWrap_t)addXY;

的main.c

 的#include&LT;&WINDOWS.H GT;
    #包括LT&;&stdio.h中GT;    的typedef INT(__cdecl * FUNC)(int类型的,INT B);    无效的主要()
    {
      HINSTANCE loadedDLL =调用LoadLibrary(addxy.dll);      如果(!loadedDLL){
        输出(DLL未加载);
        返回;
      }其他{
        输出(DLL加载\\ n);
      }      FUNC另外=(FUNC)GetProcAddress的(loadedDLLaddXYWrap);      如果(!加法){
        的printf(FUNC未加载);
        返回;
      }其他{
        的printf(FUNC装\\ n);
      }      输出(结果:%d个,除了(3,4));
    }

屈服

 加载DLL
FUNC装

之前chrashes。

崩溃,没有给出相关原因还是什么。结果
它是一个语法错误或者概念性的?


解决方案

  FUNC另外=(FUNC)GetProcAddress的(loadedDLLaddXYWrap);


此调用 GetProcAddress的返回 addXYWrap 的,而不是它的价值的地址。由于 addXYWrap 是(FUNC同一类型为)的函数指针,这意味着它返回一个指针的函数指针,或 FUNC *

试着改变该行这样的:

  FUNC此外= *(FUNC *)GetProcAddress的(loadedDLLaddXYWrap);

&NBSP;

或者,或者:

  FUNC *另外=(FUNC *)GetProcAddress的(loadedDLLaddXYWrap);

然后(*补充)(3,4)

This is an elaboration to an earlier question: How to reset state machines when unit testing C
There is also a similar question but i don't the answer match my problem and i have some examples i wish to present: Exporting a function pointer from dll

I have two sets of code that i expect should do the same but the latter crashes. Using mingw32 and Win7.

The function to be exported. This is to be considered legacy and unmutable.
addxy.c

    int addXY(int x, int y)
    {
      return x + y;
    }    

addxy.h

    int addXY(int x, int y);

The working example

main.c

    #include <stdio.h>
    #include "addxy.h"

    typedef int (__cdecl *addXYWrap_t)(int a, int b);

    addXYWrap_t addXYWrap = (addXYWrap_t)addXY;

    void main()
    {
      printf("result: %d", addXYWrap(3, 4));
    }

Yielding

result: 7

The crashing example

addxydll.c

    #include <stdio.h>
    #include "addxy.h"

    typedef int (__cdecl *addXYWrap_t)(int a, int b);

    __declspec(dllexport) addXYWrap_t addXYWrap = (addXYWrap_t)addXY;

main.c

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

    typedef int (__cdecl *func)(int a, int b);

    void main()
    {
      HINSTANCE loadedDLL = LoadLibrary("addxy.dll");

      if(!loadedDLL) {
        printf("DLL not loaded");
        return;
      } else {
        printf("DLL loaded\n");
      }

      func addition = (func)GetProcAddress(loadedDLL, "addXYWrap");

      if(!addition) {
        printf("Func not loaded");
        return;
      } else {
        printf("Func loaded\n");
      }

      printf("result: %d", addition(3, 4));
    }

Yielding

DLL loaded
Func loaded

before it chrashes.

The crash gives no information as to why or what.
Is it a syntactical error or conceptional one?

解决方案

func addition = (func)GetProcAddress(loadedDLL, "addXYWrap");

This call to GetProcAddress returns the address of addXYWrap, not its value. Since addXYWrap is a function pointer (of the same type as func), that means it returns a pointer to a function pointer, or a func*.

Try changing that line to this:

func addition = *(func*)GetProcAddress(loadedDLL, "addXYWrap");

 

Or, alternatively:

func* addition = (func*)GetProcAddress(loadedDLL, "addXYWrap");

and then (*addition)(3, 4)

这篇关于导出功能,投进去的指针通过DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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