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
Solved

Using external DLL's from Delphi

Posted on 1998-12-14
9
888 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

860 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