我怎么能写调用Win32函数一个通用的C函数? [英] How can I write a generic C function for calling a Win32 function?

查看:137
本文介绍了我怎么能写调用Win32函数一个通用的C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要允许一个脚本语言(C语言编写的)访问Win32 API的,我想编写一个函数,如以下内容:

To allow access to the Win32 API from a scripting language (written in C), I would like to write a function such as the following:

void Call(LPCSTR DllName, LPCSTR FunctionName, 
  LPSTR ReturnValue, USHORT ArgumentCount, LPSTR Arguments[])

这将调用,一般,任何Win32 API函数。

which will call, generically, any Win32 API function.

(所述LPSTR参数基本上被用作字节数组 - 假设它们已正确尺寸以采取外部的功能正确的数据类型也相信一些额外的复杂性是必需的指针和非指针之间进行区分。论点,但我忽略了这个问题的目的)。

(the LPSTR parameters are essentially being used as byte arrays - assume that they have been correctly sized to take the correct data type external to the function. Also I believe that some additional complexity is required to distinguish between pointer and non-pointer arguments but I'm ignoring that for the purposes of this question).

我的问题是传递参数到Win32 API函数。因为这些都是我STDCALL不能使用可变参数所以通话的实施必须了解提前参数的数目,因此它不能通用...

The problem I have is passing the arguments into the Win32 API functions. Because these are stdcall I can't use varargs so the implementation of 'Call' must know about the number of arguments in advance and hence it cannot be generic...

我想我可以(通过循环过争论,互推到堆栈)汇编code做到这一点,但是这可能纯C?

I think I can do this with assembly code (by looping over the arguments, pushing each to the stack) but is this possible in pure C?

更新:我已标记为接受,现在的'不,这是不可能的的答案。当然,我会改变这一点,如果一个基于C语言的解决方案,涉及到光。

Update: I've marked the 'No it is not possible' answer as accepted for now. I will of course change this if a C-based solution comes to light.

更新: 红宝石/ DL 样子它可以使用适当的机构来实现。在这个任何细节将AP preciated。

Update: ruby/dl looks like it may be implemented using a suitable mechanism. Any details on this would be appreciated.

推荐答案

没有,我不认为它可能没有写一些组件做。原因是你需要对什么是堆栈上调用目标函数之前,而且也做,在纯C.它是没有真正的方法,当然,简单的在大会上做虽然precise控制。

No, I don't think its possible to do with without writing some assembly. The reason is you need precise control over what is on the stack before you call the target function, and there's no real way to do that in pure C. It is, of course, simple to do in Assembly though.

另外,你使用PCSTR所有这些争论,实际上就是为const char * 。但是,由于所有这些args来不是字符串,你真的想使用的返回值和参数[]是无效* LPVOID 。这是当你不知道的真实类型的参数,你应该使用,而不是将它们转换为的char *

Also, you're using PCSTR for all of these arguments, which is really just const char *. But since all of these args aren't strings, what you actually want to use for return value and for Arguments[] is void * or LPVOID. This is the type you should use when you don't know the true type of the arguments, rather than casting them to char *.

这篇关于我怎么能写调用Win32函数一个通用的C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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