所以,我们一定要在最初的参数中获取后面到底实际传递了多少个变体参数。然后才可以通过堆栈,一个个的找到,进而以函数来处理。
说了这么多来举例实验一下。比如,写一个函数,需要获得传入的数字的和
function GetSum(NumCount: integer{N1,N2: integer}): Integer;cdecl; asm { push ebp mov ebp,esp } mov ecx,[ebp + 8];//先获取第一个参数值,判断传递了几个参数 //然后获取每个参数 mov edx,12 mov eax,0@@add: add eax,[ebp].edx add edx,4 loop @@add end; |
然后调用方式:
type VA_Sum = function(NumCount: integer{N1,N2}): Integer; cdecl varargs; var m: VA_Sum; begin m := GetSum; ShowMessage(inttostr(m(4,3,4,5,6))); end; |
在比如,连接字符串函数
function ConStr(NumCount: integer{N1,N2}): string;cdecl; var st1: string; i: Integer; begin asm mov esi,16 end; for i := 0 to NumCount - 1 do begin asm mov edi,[ebp].esi cmp edi,128 jb @@char mov DWORD ptr st1,edi @@Char: add esi,4 end; Result := Result + st1; end; end; |
调用方法
type VA_ConStr = function(NumCount: integer{N1,N2}): string; cdecl varargs; var cstr: VA_ConStr; begin cstr := constr; ShowMessage(cstr(3,'Test','不得闲','测试')); end; |
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.046211004257202 seconds