Solved

C DLL in Delphi 2

Posted on 1998-11-14
4
287 Views
Last Modified: 2010-04-04
I wrote a DLL in MS Visual C++, and I want to call it from a Delphi 2 app. How do I do this? Here is my declaration of the function in C++:

BOOL UnpackFile(const char* Resource, const char* InFile)
0
Comment
Question by:mmarvi
  • 2
4 Comments
 

Accepted Solution

by:
kashif063098 earned 50 total points
Comment Utility
first you need to declare this function in delphi in following way
function UnpackFile(Resource,InFile:PChar):boolean;cdecl;
external 'C:\YourCdll.dll' name 'UnpackFile';

then simply call UnpackFile() with appropriate parameters.

Any question please ask!

regards

0
 

Author Comment

by:mmarvi
Comment Utility
Dear kashif:

I put your code into a test program, and at build time, I encountered an error "field defintion not allowed after methods or procedures." I included the unit's code below, please find my error and correct it.

Thanx!

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure OnExtract(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function UnpackFile(Resource,InFile:PChar):boolean; cdecl; external 'WPLANE.DLL';
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OnExtract(Sender: TObject);
var
     h: boolean;
begin
     h := UnpackFile('WINGS.WAD', 'JEX.EXE');
     if h = true then ShowMessage('Success!');
     else ShowMessage('Fail!');
end;

end.
0
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
Don't place UnpackFile in the form class.  Just place it by itself in the module.
0
 

Expert Comment

by:kashif063098
Comment Utility
scrapdog is right put the following declaration after the uses clause
>>uses
>>  Windows, Messages, SysUtils, Classes, Graphics, Controls, >>  Forms, Dialogs,StdCtrls;

function UnpackFile(Resource,InFile:PChar):boolean;cdecl;
external 'C:\YourCdll.dll' name 'UnpackFile';
After this change your exmaple should look like following

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

/*Remember to include the full path to your dll*/
function UnpackFile(Resource,InFile:PChar):boolean; cdecl; external 'WPLANE.DLL' name 'UnpackFile';


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure OnExtract(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OnExtract(Sender: TObject);
var
     h: boolean;
begin
     h := UnpackFile('WINGS.WAD', 'JEX.EXE');
     if h = true then ShowMessage('Success!');
     else ShowMessage('Fail!');
end;

end.


0

Featured Post

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.

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now