Solved

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

Posted on 1998-09-01
11
170 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
ID: 1338408
What's the function doing....which is the return value.....???
0
 

Accepted Solution

by:
kashif063098 earned 50 total points
ID: 1338409
answer is coming
0
 

Author Comment

by:skel
ID: 1338410
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:viktornet
ID: 1338411
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
ID: 1338412
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
 

Expert Comment

by:kashif063098
ID: 1338413
Sorry ,i am having some problems .....

0
 

Author Comment

by:skel
ID: 1338414
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
ID: 1338415
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
ID: 1338416
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
ID: 1338417
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
ID: 1338418
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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