Solved

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

Posted on 1998-09-01
11
166 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:skel
  • 6
  • 3
  • 2
11 Comments
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
What's the function doing....which is the return value.....???
0
 

Accepted Solution

by:
kashif063098 earned 50 total points
Comment Utility
answer is coming
0
 

Author Comment

by:skel
Comment Utility
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.
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:kashif063098
Comment Utility
Sorry ,i am having some problems .....

0
 

Author Comment

by:skel
Comment Utility
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
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
0
 

Author Comment

by:skel
Comment Utility
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
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
That's what I meant kashif... I really think that your answer was best...Keep the good work...

vik
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now