如何使用Microsoft Linker / Export参数 [英] How to use the Microsoft Linker /Export parameter

查看:823
本文介绍了如何使用Microsoft Linker / Export参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Microsoft Linker手动导出我的对象文件中的函数。
当我为每个这样的函数使用参数时,它工作正常:

  / Export:ExportedFunction1 $ qqsv / Export :ExportedFunction2 $ qqsv等等... 

链接器会自动正确分配ords。但是在导出表中,实际出口名称为 ExportedFunction1 $ qqsv / ExportedFunction2 $ qqsv / etc ..
我尝试过这样的参数: p>

  / Export:ExportedFunction1 $ qqsv,1,ExportedFunction1 / Export:ExportedFunction2 $ qqsv,2,ExportedFunction2 

但我认为我使用的参数错误?如何正确使用/导出参数为导出分配自己的名称?



PS:
我正在使用Microsoft(R)增量链接器版本7.00.9210

解决方案

这是一个具有DEF文件的示例解决方案。



DLL-project:



CppLib.h:

  #ifdef CPPLIB_EXPORTS 
#define CPPLIB_API __declspec(dllexport)
#else
#define CPPLIB_API __declspec(dllimport)
#endif

CPPLIB_API double MyFunction1(double);

CppLib.cpp:

  CPPLIB_API double MyFunction1(double dd)
{
return dd;
}

CppLib.def:

  LIBRARY 

EXPORTS
MySuperFunction = MyFunction1 @ 1
/ pre>

构建DLL。



如果我们在CppLib.DLL上运行dumpbin,获取:

  ... 
ordinal提示RVA名称

2 0 0001101E?MyFunction1 @@ YANN @ Z = @ ILT + 25(?MyFunction1 @@ YANN @ Z)
1 1 0001101E MySuperFunction = @ ILT + 25(?MyFunction1 @@ YANN @ Z)






使用CppLib的控制台应用程序.dll:

  #includeCppLib.h

#include< Windows.h>
#include< iostream>

int main()
{
typedef double(* MY_SUPER_FUNC)(double);

HMODULE hm = LoadLibraryW(LCppLib.dll);
MY_SUPER_FUNC fn1 =(MY_SUPER_FUNC)GetProcAddress(hm,MySuperFunction); //按名称查找
MY_SUPER_FUNC fn2 =(MY_SUPER_FUNC)GetProcAddress(hm,MAKEINTRESOURCEA(1)); // find by ordinal

std :: cout<<<< fn1(34.5)<的std :: ENDL; // prints 34.5
std :: cout<<<< fn2(12.3)<的std :: ENDL; //打印12.3

返回0;
}


I would like to export functions from my object files manually with the Microsoft Linker. It works fine when I use the parameter for every function like this:

/Export:ExportedFunction1$qqsv /Export:ExportedFunction2$qqsv and so on...

The linker then automatically assigns the ords properly. However in the export table the actuall export name is "ExportedFunction1$qqsv/ExportedFunction2$qqsv/etc.." I tried the parameter doing like this:

/Export:ExportedFunction1$qqsv,1,ExportedFunction1 /Export:ExportedFunction2$qqsv,2,ExportedFunction2

But I think I'm using the parameters wrong?! How do I use the /Export parameter properly to assign my own names for the exports?

PS.: I'm using Microsoft (R) Incremental Linker Version 7.00.9210

解决方案

Here is a sample solution with a DEF file.

DLL-project:

CppLib.h:

#ifdef CPPLIB_EXPORTS
#define CPPLIB_API __declspec(dllexport)
#else
#define CPPLIB_API __declspec(dllimport)
#endif

CPPLIB_API double MyFunction1(double);

CppLib.cpp:

CPPLIB_API double MyFunction1(double dd)
{
    return dd;
}

CppLib.def:

LIBRARY

EXPORTS
MySuperFunction=MyFunction1 @1

Build DLL.

If we run dumpbin on CppLib.DLL, we'll get:

...
    ordinal hint RVA      name

          2    0 0001101E ?MyFunction1@@YANN@Z = @ILT+25(?MyFunction1@@YANN@Z)
          1    1 0001101E MySuperFunction = @ILT+25(?MyFunction1@@YANN@Z)
...


Console Application that uses CppLib.dll:

#include "CppLib.h"

#include <Windows.h>
#include <iostream>

int main()
{
    typedef double(*MY_SUPER_FUNC)(double);

    HMODULE hm = LoadLibraryW(L"CppLib.dll");
    MY_SUPER_FUNC fn1 = (MY_SUPER_FUNC)GetProcAddress(hm, "MySuperFunction"); // find by name
    MY_SUPER_FUNC fn2 = (MY_SUPER_FUNC)GetProcAddress(hm, MAKEINTRESOURCEA(1)); // find by ordinal

    std::cout << fn1(34.5) << std::endl; // prints 34.5
    std::cout << fn2(12.3) << std::endl; // prints 12.3

    return 0;
}

这篇关于如何使用Microsoft Linker / Export参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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