Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Barry Kelly inspired me to seeking a solution without replacing the stack... Here is the solution (probably could also use the Invoke from the rtti unit, instead RealCall_CDecl).</p> <pre><code>// This function is copied from PascalScript function RealCall_CDecl(p: Pointer; StackData: Pointer; StackDataLen: Longint; // stack length are in 4 bytes. (so 1 = 4 bytes) ResultLength: Longint; ResEDX: Pointer): Longint; Stdcall; // make sure all things are on stack var r: Longint; begin asm mov ecx, stackdatalen jecxz @@2 mov eax, stackdata @@1: mov edx, [eax] push edx sub eax, 4 dec ecx or ecx, ecx jnz @@1 @@2: call p mov ecx, resultlength cmp ecx, 0 je @@5 cmp ecx, 1 je @@3 cmp ecx, 2 je @@4 mov r, eax jmp @@5 @@3: xor ecx, ecx mov cl, al mov r, ecx jmp @@5 @@4: xor ecx, ecx mov cx, ax mov r, ecx @@5: mov ecx, stackdatalen jecxz @@7 @@6: pop eax dec ecx or ecx, ecx jnz @@6 mov ecx, resedx jecxz @@7 mov [ecx], edx @@7: end; Result := r; end; // personally created function :) function CallManually3(Code: Pointer; const Args: array of const): Pointer; var i: Integer; tmp: AnsiString; data: AnsiString; begin for i := Low(Args) to High(Args) do begin case Args[i].VType of vtInteger, vtPChar, vtAnsiString, vtWideString, vtUnicodeString: begin tmp := #0#0#0#0; Pointer((@tmp[1])^) := TVarRec(Args[i]).VPointer; end; vtExtended: begin tmp := #0#0#0#0#0#0#0#0; Double((@tmp[1])^) := TVarRec(Args[i]).VExtended^; end; // fill as needed else raise Exception.Create('Unknown type'); end; data := data + tmp; end; Result := pointer(RealCall_CDecl(Code, @data[Length(data) - 3], Length(data) div 4, 4, nil)); end; function printf(fmt: PAnsiChar): Integer; cdecl; varargs; external 'msvcrt.dll' name 'printf'; begin CallManually3(@printf, [AnsiString('test of printf %s %d %.4g'#10), PAnsiChar('hello'), 42, 4.123]); end.</code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload