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

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

问题描述

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



主要的问题是C ++是一个强类型语言,需要你知道编译时函数调用的参数的数量和类型。在我的例子中,我根本不知道这些参数是什么直到用户在运行时选择它们。



我想出的唯一解决方案是使用assembly手动地在调用栈上推送参数。



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



所以(finally :)这里是我的问题:我可以推断调用约定programmaticaly?依赖性Walker不会帮助我,我不知道如何手动读取PE格式。

解决方案



如果函数名称是C ++修饰,那么你可以从名称修饰中确定参数计数和类型,这是你最好的情况



如果导出的函数是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天全站免登陆