Link to home
Start Free TrialLog in
Avatar of skel
skel

asked on

How to call a function in a DLL in Delphi 3?

Hi all... I have made a DLL in C language with the following function:

UINT WINAPI Desasm( UINT current, LPSTR target, LPSTR start );

(UINT = Unsigned int, LPSTR = Long pointer String)

The question is.. how can I declare such function in Delphi? I have:

function Desasm( Address: Integer; Target : string; Start : PChar): Integer; external 'Desasm.dll';

just after starting implementation part of the unit.

Later in other function I tried to call it this way:

var
  Texto : string;
  Buf   : array[0..19] of Char;

begin
  Buf[0] := #18;
  Buf[1] := #00;
  Buf[2] := #38;
  Desasm( 0, Texto, Buf );
  ShowMessage( Texto );
end;

The fact is that Texto must contain a string, but on return, it contains nothing. If I try to use stdcall in declaration of the function, application crashes.

Note: the DLL does work!

Who can help me?
Thanks in advance

Jaime
Avatar of viktornet
viktornet
Flag of United States of America image

What's the function doing....which is the return value.....???
ASKER CERTIFIED SOLUTION
Avatar of kashif063098
kashif063098

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skel
skel

ASKER

It's a dissasembler.

Address is the address of the instruccion
Target is the string receiving the mnemonic
Start is the buffer containing de opcode and operand

It's return the number of operands required for the instruction in ordder to know where the next instruction starts.

But I think it doesn't care about what the function actually does, because of encasulation... the problem is communication de delphi program with the library.
kashif I was just wondering why do you answer a question w/o having the answer...If you somehow don't supply any answers then the person cannot delete the file if he/she wants to

Regards,
Viktor Ivanov
var
  I : UINT;//you can make a decalration like this one since there is UINT type in Delphi.///
----------------
function Desasm( Address: UINT; Target : PChar; Start : PChar): UINT; external 'Desasm.dll';

Why don't you try this.....??? and see if itworks

Regards,
Viktor Ivanov
Sorry ,i am having some problems .....

Avatar of skel

ASKER

I have finally solved the problem declaring the function in the DLL as:

function Desasm( Address: UINT; Target : pointer; Start : pointer): UINT; stdcall;
         external 'Desasm.dll';

and then, calling it with

var
  Texto : array[0..255] of char;
  Buf   : array[0..19] of Byte;

begin
  Buf[0] := $12;
  Buf[1] := 00;
  Buf[2] := $26;
  Desasm( 0, @Texto, @Buf );
  .
  .
  .
end;

is there a more elegant solution? any suggestion?

Jaime
I'm not sure if you need to do this..... @Texto @Buf...what you could try doing which is almost the same is this........
var
   Texto, Buf : String;
     begin
       Buf[1] := $12;
       Buf[2] := 00;
       Buf[3] := $26;
       Desasm( 0, PChar(Texto), PChar(Buf) );
//In the first example here is what's wrong..
you wrote..Desasm( 0, Texto, Buf ); and it is suppose to be
Desasm( 0, PChar(Texto), Buf );//That's what was wrong with you function....You need to typecast the String to a PChar...which is same as declaring an array of char :+)

But still it's the same idea and maybe more writing...so you better stick to your way ;-)
Otherwise..is everything working properly now?

Regards,
Viktor Ivanov
Avatar of skel

ASKER

I tried that but it didn't work... the fact is that the string definition is different between C and Pascal. In C it starts from 0, while in Pascal starts from 1, with the 0 reserved for the string length. I think that is the problem.

Regarding with buf variable.. it must be an array of bytes.... I made a mistake in my first post.

Thanks for your posts
Jaime
That's what I meant kashif... I really think that your answer was best...Keep the good work...

vik
What I meant is at least I tried to help you and kashif's answer was "Answer is coming...". That doesn't look like an answer to me. Maybe I didn't give you the right answer but at least tried, and I think that's important. Anyway, I think it's (*GrEaT*) that you got your function working!
Talk to you later! Bye

{The example I gave you should work. I might have missed something small since I wrote it right on E-E, and haven't checked it with Delphi, but I'm sure it will work if something small is corrected =)

Regards,
Viktor Ivanov