Solved

Using external DLL's from Delphi

Posted on 1998-12-14
9
882 Views
Last Modified: 2010-04-04
A vendor of mine has a Delphi-based system to which I must integrate a simple application. My application is developed in C, so the simplest way would be to modify the application so that it becomes a DLL, which would export a function that could be used from Delphi.

My application needs one string as input, and it outputs another string. I need to know the following:

1. How to build the DLL (in C) so that it can pass strings to/from Delphi? Can I use normal NULL-terminated strings as follows:

void _cdecl ExternalFunction(char *InputString, char *OutputString);

2. Given this DLL, how can I call it from Delphi? Please provide specific lines of code (including declaration and the actual call), because I don't know Delphi at all.
0
Comment
Question by:mikkon
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Accepted Solution

by:
dwwang earned 200 total points
ID: 1350987
procedure ExternalFunction(InputString,OutputString:PChar) ;cdecl;external 'Mydll.dll' name 'ExternalFunction';

then Call the procedure:

var
Input,output:pchar;
.
CallDLL(input,output);

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1350988
do you want that a delphi program uses your dll, or that Delphi itself uses your component?
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1350989
Sorry, the call statement shold be:

ExternalFunction(input,output);

and also if you need the inputstring to be initialized, just write like:

input:='My Input String Here...';
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1350990
just wanted to refer to this dwwang :-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:dwwang
ID: 1350991
Thanks, Zif :-)
0
 
LVL 1

Author Comment

by:mikkon
ID: 1350992
Thanks, dwwang! Just let me know if I got this straight:

In C, I declare my function as follows (changed from the original case to include a return status and use __stdcall; any problems with __stdcall?):

int _stdcall ExternalFunction(char *input,char *output)

And in Delphi as follows:

function ExternalFunction(Input,Output:PChar):WORDBOOL; stdcall; external 'mydll.dll' name 'ExternalFunction';

Then I call it as follows:

var Input,Output:PChar;


Input:="...";
Output:="1234567890123456";    { Needed to inialize the output string to a certain length??? }
retval:=ExternalFunction(Input,Output);

Is this right? Does Delphi handle NULL-terminated strings just like C? I tried this kind of external function with Visual Basic, but it didn't recognize NULLs correctly (for example, Output&"test" didn't produce the expected output).

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1350993
Hi mikkon,

PChar = null-terminated string in Delphi!

here information :

A string literal is assignment-compatible with the PChar type. This means that a string literal can be assigned to a variable of type PChar. For example,

var
 P: PChar;
 ...
begin
 P := 'Hello world...';
end;

The effect of such an assignment is that the pointer points to an area of memory that contains a null-terminated copy of the string literal. This example accomplishes the same thing as the previous example:

const
 TempString: array[0..14] of Char = 'Hello world...'#0;
var
 P: PChar;
 ...
begin
 P := @TempString;
end;

You can use string literals as actual parameters in procedure and function calls when the corresponding formal parameter is of type PChar. For example, given a procedure with the declaration

procedure PrintStr(Str: PChar);

the following procedure calls are valid:

PrintStr('This is a test');
PrintStr(#10#13);

Just as it does with an assignment, the compiler generates a null-terminated copy of the string literal. The compiler passes a pointer to that memory area in the Str parameter of the PrintStr procedure.
Finally, you can initialize a typed constant of type PChar with a string constant. You can do this with structured types as well, such as arrays of PChar and records and objects with PChar fields.

const
 Message: PChar = 'Program terminated';
 Prompt: PChar = 'Enter values: ';
 Digits: array[0..9] of PChar = (
   'Zero', 'One', 'Two', 'Three', 'Four',
   'Five', 'Six', 'Seven', 'Eight', 'Nine');
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1350994
Hi,  mikkon

Firstly, Delphi can handle Null terminated string just as in C/C++, Delphi is definitely better than VB in such kind of issues.

The second, you need to allocate memory to the outputstring, either as you said(to assign a fixed length string to it), or as Zif suggested, to define a array of characters and assign the address to it.

By the way, I think you should use strcopy or some thing like that(in C) in your DLL to pass the value pack to Delphi, right?
0
 
LVL 1

Author Comment

by:mikkon
ID: 1350995
Thanks to both dwwang and ZifNab! And yes, I am using strcpy() to copy the output string in C. The idea is to use pointers to Delphi's memory in the C code.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

21 Experts available now in Live!

Get 1:1 Help Now