?
Solved

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

Posted on 1998-09-01
11
Medium Priority
?
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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