Solved

Using external DLL's from Delphi

Posted on 1998-12-14
9
891 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
[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
  • 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
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 8

Expert Comment

by:ZifNab
ID: 1350990
just wanted to refer to this dwwang :-)
0
 
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

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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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