Solved

Using external DLL's from Delphi

Posted on 1998-12-14
9
878 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

14 Experts available now in Live!

Get 1:1 Help Now