Tôi tạo ra một c dự án DLL mới ++ trong VS2010 đó cho thấy nhiều chức năng 1chức năng Gọi từ một C++ DLL trong Delphi
#include "stdafx.h"
#define DllImport extern "C" __declspec(dllimport)
#define DllExport extern "C" __declspec(dllexport)
DllExport int DoMath(int a, int b) {
return a + b ;
}
sau đó tôi đã tạo ra một ứng dụng C++ với VS2010 để kiểm tra DLL này. Việc xây dựng ứng dụng thử nghiệm trong VS2010 có thể gọi C++ DLL và nhận được kết quả mong đợi.
#include "stdafx.h"
#include <windows.h>
typedef int (*DoMath)(int, int) ;
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hMod = LoadLibrary ("exampleDLL.dll");
if (NULL != hMod) {
DoMath mf1 = (DoMath) GetProcAddress(hMod,"DoMath");
if(mf1 != NULL) {
printf ("DoMath(8,7)==%d \n", mf1(8,7));
} else {
printf ("GetProcAddress Failed \n");
}
FreeLibrary(hMod);
} else {
printf ("LoadLibrary failed\n");
return 1;
}
return 0;
}
Tiếp theo tôi đã cố tạo một dự án mới trong Delphi 7 để gọi hàm DLL C++ này. Tôi đã sử dụng this tutorial để giúp tôi tạo dự án mới.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TmyFunction = function(X,Y: Integer):Integer;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
hDll: THandle;
end;
var
Form1: TForm1;
fDoMath : TmyFunction;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
hDll := LoadLibrary('exampleDLL.dll');
if HDll >= 32 then { success }
begin
fDoMath := GetProcAddress(hDll, 'DoMath');
end
else
MessageDlg('Error: could not find exampleDLL.DLL', mtError, [mbOk], 0)
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
i := fDoMath(2,3);
edit1.Text := IntToStr(i);
end;
end.
Kết quả từ dự án Delphi 7 là 6.155.731 Khi tôi mong đợi . Tôi đã kiểm tra nhị phân của kết quả nghĩ rằng nó có thể có một cái gì đó để làm với một kiểu dữ liệu nhưng nó trông ngẫu nhiên với tôi. Khi tôi biên dịch lại/chạy lại ứng dụng, nó nhận được kết quả tương tự mỗi lần.
Tôi không biết nhiều về Delphi đây là lần đầu tiên tôi đối phó với nó và tôi thấy nó khó hiểu.
Bất kỳ đề xuất nào về những gì cần kiểm tra tiếp theo?
Đánh bại tôi sau 5 giây +1 –
+1 Quy ước cuộc gọi luôn là điều cần quan tâm – Juliano