我可以编程推导出C ++ DLL使用的调用约定吗? [英] Can I programatically deduce the calling convention used by a C++ dll?

查看:227
本文介绍了我可以编程推导出C ++ DLL使用的调用约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想你想编写一个程序来测试c ++ dll文件中的函数。
您应该允许用户选择一个dll(我们假设我们在谈论c ++ dll)。
他应该能够获取dll导出的所有函数的列表。
然后,用户应该能够从列表中选择一个函数名,手动输入一个参数列表(这些参数都是基本类型,如int,double,bool或char数组(例如c型字符串) )并尝试使用指定的参数运行所选函数。
他想知道函数是否与指定的参数一起运行,或者导致它们崩溃(因为它们与签名不匹配)。



主要的问题是,C ++是强类型语言,需要您在编译时知道函数调用的参数的数量和类型。在我的情况下,我根本不知道这些参数直到用户在运行时选择它们。



我想出的唯一解决方案是使用程序集来手动推送调用堆栈上的参数。 >

但是,我已经明白,如果我想搞乱程序集,我最好确定我知道哪个调用约定是使用dll中的函数。



所以(终于:)这里是我的问题:我可以推断出调用会议程序语言吗? Dependency Walker不会帮助我,我不知道如何手动阅读PE格式。

解决方案

答案是可能



如果函数名称是C ++装饰的,那么您可以从名称装饰中确定参数计数和类型,这是您最好的案例如果MSVC被用来首先编写代码,那么这个方案很可能。



如果导出的函数是stdcall调用约定(Windows api的默认值),那么你可以确定要推送的字节数,而不是参数的类型。



坏消息是,对于C调用约定,没有办法通过查看符号名称来告诉。您需要访问源代码或调试信息。



http://en.wikipedia.org/wiki/X86_calling_conventions



函数作为导出给出的名称不是需要与链接器看到的名称有任何关系,但大多数情况下,链接器看到的导出的名称和符号名称是相同的。


Imagine you'd like to write a program that tests functions in a c++ dll file. You should enable the user to select a dll (we assume we are talking about c++ dlls). He should be able to obtain a list of all functions exported by the dll. Then, the user should be able to select a function name from the list, manually input a list of arguments ( the arguments are all basic types, like int, double, bool or char arrays (e.g. c-type strings) ) and attempt to run the selected function with the specified arguments. He'd like to know if the function runs with the specified arguments, or do they cause it to crash ( because they don't match the signature for example ).

The main problem is that C++, being a strongly typed language, requires you to know the number and type of the arguments for a function call at compile time.And in my case, I simply don't know what these arguments are, until the user selects them at runtime.

The only solution I came up with, was to use assembly to manually push the arguments on the call stack.

However, I've come to understand that if I want to mess with assembly, I'd better make damn sure that I know which calling convention are the functions in the dll using.

So (finally:) here's my question: can I deduce the calling convention programmaticaly? Dependency Walker won't help me, and I've no idea how to manually read PE format.

解决方案

The answer is maybe.

If the functions names are C++ decorated, then you can determine the argument count and types from the name decoration, this is your best case scenario, and fairly likely if MSVC was used to write the code in the first place.

If the exported functions are stdcall calling convention (the default for windows api), you can determine the number of bytes to be pushed, but not the types of the arguments.

The bad news is that for C calling convention, there isn't any way to tell by looking at the symbol names. You would need to have access to the source code or the debug info.

http://en.wikipedia.org/wiki/X86_calling_conventions

The name that a function is given as an export is not required to have any relationship with the name that the linker sees, but most of the time, the exported name and the symbol name that the linker sees are the same.

这篇关于我可以编程推导出C ++ DLL使用的调用约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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