德尔福" const的&QUOT阵列;到"可变参数" [英] Delphi "array of const" to "varargs"
问题描述
请帮帮忙!我需要这种转换编写包装一些C头德尔福。
Please help! I need this conversion to write wrapper for some C headers for Delphi.
作为一个例子:
function pushfstring(fmt: PAnsiChar): PAnsiChar; cdecl; varargs; external;
...
function PushString(fmt: AnsiString; const args: array of const): AnsiString;
begin
Result := AnsiString(pushfstring(PAnsiString(fmt), args)); // it's incorrect :/
end;
我如何转换常量数组到可变参数?
How can I convert "array of const" to "varargs"?
修改:功能PushString实际上是记录(我给了一个简单的例子)里面,我不必pushfstring直接访问。直接呼叫被排除在外。
edit: function PushString is actually inside the record (I gave a simplified example), and I do not have direct access to pushfstring. Direct call is excluded.
编辑2 :我写的单位LUA库德尔福的情况下是非常重要的,我
edit 2:I write the units for LUA library for Delphi and the case is quite important for me.
指定并提供这个问题的所有细节 - 我必须用C此功能:
Specifying and providing all the details of the matter - I have this function in C:
LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
在德尔福我有这样的事情:
In Delphi I have something like this:
的 LuaLibrary.pas 的
{...}
interface
{...}
function lua_pushfstring(L: lua_State; fmt: PAnsiChar): PAnsiChar; cdecl; varargs;
implementation
{...}
function lua_pushfstring; external 'lua.dll'; // or from OMF *.obj file by $L
的 dtxLua.pas 的
uses LuaLibrary;
{...}
type
TLuaState = packed record
private
FLuaState: lua_State;
public
class operator Implicit(A: TLuaState): lua_State; inline;
class operator Implicit(A: lua_State): TLuaState; inline;
{...}
// btw. PushFString can't be inline function
function PushFString(fmt: PAnsiChar; const args: array of const ): PAnsiChar;
//... and a lot of 'wrapper functions' for functions like a lua_pushfstring,
// where L: lua_State; is the first parameter
end;
implementation
{...}
function TLuaState.PushFString(fmt: PAnsiChar; const args: array of const )
: PAnsiChar;
begin
Result := lua_pushfstring(FLuaState, fmt, args); // it's incorrect :/
end;
和其他单位一样Lua.pas我只用TLuaState从dtxLua.pas(因为LuaLibrary粗大,dtxLua是我的包装),对于许多有用的,很酷的事情...
and in other units like Lua.pas i use only TLuaState from dtxLua.pas (because LuaLibrary is bulky, dtxLua is my wrapper), for many useful and cool things...
推荐答案
我猜的原型 pushfstring
有点像这样的:
I'm guessing that the prototype for pushfstring
is somewhat like this:
void pushfstring(const char *fmt, va_list args);
如果它不是,是代替
void pushfstring(const char *fmt, ...);
...然后我应该有你也包括在内。
... then I should have you covered also.
在C,如果你要传递从一个可变参数函数到另外一个电话,你应该使用的va_list
,的va_start
和 va_end用来
,并调用 v
版的功能。所以,如果你正在实施的printf
你自己,你可以使用 vsprintf中
来格式化字符串 - 你不能叫的sprintf
直接沿着一个可变的参数列表传递。您需要使用的va_list
和朋友。
In C, if you have to pass on a call from one variadic function to another, you should use va_list
, va_start
and va_end
, and call the v
version of the function. So, if you were implementing printf
yourself, you might use vsprintf
to format the string - you can't call sprintf
directly and pass along the variadic argument list. You need to use va_list
and friends.
这是pretty别扭处理C的的va_list
从Delphi和技术上不应该做的 - 的va_list的实施code>是特定于C编译器供应商的运行时间。
It's pretty awkward to handle C's va_list
from Delp and technically it shouldn't be done - the implementation of va_list
is specific to the C compiler vendor's runtime.
不过,大家可以试试。假设我们有一个小类 - 尽管我做了它的易用性的记录:
However, we can try. Suppose we have a little class - though I made it a record for ease of use:
type
TVarArgCaller = record
private
FStack: array of Byte;
FTop: PByte;
procedure LazyInit;
procedure PushData(Loc: Pointer; Size: Integer);
public
procedure PushArg(Value: Pointer); overload;
procedure PushArg(Value: Integer); overload;
procedure PushArg(Value: Double); overload;
procedure PushArgList;
function Invoke(CodeAddress: Pointer): Pointer;
end;
procedure TVarArgCaller.LazyInit;
begin
if FStack = nil then
begin
// Warning: assuming that the target of our call doesn't
// use more than 8K stack
SetLength(FStack, 8192);
FTop := @FStack[Length(FStack)];
end;
end;
procedure TVarArgCaller.PushData(Loc: Pointer; Size: Integer);
function AlignUp(Value: Integer): Integer;
begin
Result := (Value + 3) and not 3;
end;
begin
LazyInit;
// actually you want more headroom than this
Assert(FTop - Size >= PByte(@FStack[0]));
Dec(FTop, AlignUp(Size));
FillChar(FTop^, AlignUp(Size), 0);
Move(Loc^, FTop^, Size);
end;
procedure TVarArgCaller.PushArg(Value: Pointer);
begin
PushData(@Value, SizeOf(Value));
end;
procedure TVarArgCaller.PushArg(Value: Integer);
begin
PushData(@Value, SizeOf(Value));
end;
procedure TVarArgCaller.PushArg(Value: Double);
begin
PushData(@Value, SizeOf(Value));
end;
procedure TVarArgCaller.PushArgList;
var
currTop: PByte;
begin
currTop := FTop;
PushArg(currTop);
end;
function TVarArgCaller.Invoke(CodeAddress: Pointer): Pointer;
asm
PUSH EBP
MOV EBP,ESP
// Going to do something unpleasant now - swap stack out
MOV ESP, EAX.TVarArgCaller.FTop
CALL CodeAddress
// return value is in EAX
MOV ESP,EBP
POP EBP
end;
使用此记录,我们可以手动构造预期的各种C调用调用帧。在x86 C'S调用约定是由右参数传递给留在堆栈中,与主叫方清理。这里有一个通用的C调用程序的框架:
Using this record, we can manually construct the call frame expected for various C calls. C's calling convention on x86 is to pass arguments from right to left on the stack, with the caller cleaning up. Here's the skeleton of a generic C calling routine:
function CallManually(Code: Pointer; const Args: array of const): Pointer;
var
i: Integer;
caller: TVarArgCaller;
begin
for i := High(Args) downto Low(Args) do
begin
case Args[i].VType of
vtInteger: caller.PushArg(Args[i].VInteger);
vtPChar: caller.PushArg(Args[i].VPChar);
vtExtended: caller.PushArg(Args[i].VExtended^);
vtAnsiString: caller.PushArg(PAnsiChar(Args[i].VAnsiString));
vtWideString: caller.PushArg(PWideChar(Args[i].VWideString));
vtUnicodeString: caller.PushArg(PWideChar(Args[i].VUnicodeString));
// fill as needed
else
raise Exception.Create('Unknown type');
end;
end;
Result := caller.Invoke(Code);
end;
以的printf
为例:
function printf(fmt: PAnsiChar): Integer; cdecl; varargs;
external 'msvcrt.dll' name 'printf';
const
// necessary as 4.123 is Extended, and %g expects Double
C: Double = 4.123;
begin
// the old-fashioned way
printf('test of printf %s %d %.4g'#10, PAnsiChar('hello'), 42, C);
// the hard way
CallManually(@printf, [AnsiString('test of printf %s %d %.4g'#10),
PAnsiChar('hello'), 42, C]);
end.
调用的va_list
版本是稍微复杂,因为的va_list
参数的位置需要谨慎的地方放置预计:
Calling the va_list
version is slightly more involved, as the va_list
argument's location needs to be placed carefully where it is expected:
function CallManually2(Code: Pointer; Fmt: AnsiString;
const Args: array of const): Pointer;
var
i: Integer;
caller: TVarArgCaller;
begin
for i := High(Args) downto Low(Args) do
begin
case Args[i].VType of
vtInteger: caller.PushArg(Args[i].VInteger);
vtPChar: caller.PushArg(Args[i].VPChar);
vtExtended: caller.PushArg(Args[i].VExtended^);
vtAnsiString: caller.PushArg(PAnsiChar(Args[i].VAnsiString));
vtWideString: caller.PushArg(PWideChar(Args[i].VWideString));
vtUnicodeString: caller.PushArg(PWideChar(Args[i].VUnicodeString));
else
raise Exception.Create('Unknown type'); // etc.
end;
end;
caller.PushArgList;
caller.PushArg(PAnsiChar(Fmt));
Result := caller.Invoke(Code);
end;
function vprintf(fmt: PAnsiChar; va_list: Pointer): Integer; cdecl;
external 'msvcrt.dll' name 'vprintf';
begin
// the hard way, va_list
CallManually2(@vprintf, 'test of printf %s %d %.4g'#10,
[PAnsiChar('hello'), 42, C]);
end.
注:
-
以上预计在Windows 86。微软C,bcc32(Embarcadero公司C ++)和gcc以同样的方式(一个指针到堆栈上的第一个参数可变参数)全通
的va_list
,根据我的实验,所以它应该为你工作;但只要在Windows假设在x86坏了,想到这可能是打破了。
The above expects x86 on Windows. Microsoft C, bcc32 (Embarcadero C++) and gcc all pass
va_list
in the same way (a pointer to the first variadic argument on the stack), according to my experiments, so it should work for you; but as soon as the x86 on Windows assumption is broken, expect this to possibly break too.
堆栈被换缓和与施工。这可以用更多的工作来避免,但通过的va_list
也变得棘手,因为它需要的参数,好像他们是在栈上传递到另一个点。因此,在code需要做出多少堆栈被调用程序使用的假设;本例假设8K,但是这可能是太小。增加,如果必要的。
The stack is swapped to ease with its construction. This can be avoided with more work, but passing va_list
also becomes trickier, as it needs to point at the arguments as if they were passed on the stack. As a consequence, the code needs to make an assumption about how much stack the called routine uses; this example assumes 8K, but this may be too small. Increase if necessary.
这篇关于德尔福" const的&QUOT阵列;到"可变参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!