Solved

C DLL in Delphi 2

Posted on 1998-11-14
4
291 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
ID: 1346804
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
ID: 1346805
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
ID: 1346806
Don't place UnpackFile in the form class.  Just place it by itself in the module.
0
 

Expert Comment

by:kashif063098
ID: 1346807
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

839 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