delphi XE7 编写DLL与调用

1、DLL部分 代码

library HelloDll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
System.SysUtils,
System.Classes;

{$R *.res}
function SayHello(sInPar:WideString;var sOutPar :WideString) :Integer; stdcall;
begin
try
Result:=0;
sOutPar:='Hello,'+sInPar;
Result:=1;
except
on e:exception do
begin
sOutPar:='异常:'+e.Message;
end;
end;
end;
exports
SayHello;
begin
end.

2、‘exe 部分代码

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
//TMyfun = function(iJYLX: Integer; pInpar:PChar;pOutPar: PChar): Integer; stdcall;
TMyfun=function(sInPar:WideString;var sOutPar :WideString) :Integer; stdcall;
var
MyHandle: THandle;
Myfunc:TMyfun;
iRet:Integer;
sInPar,sOutPar:WideString;
//pInPar,pOutPar:PChar;
begin
try

sInPar:='bill';
//StrPCopy(pInpar,sInPar);
//MyHandle:=LoadLibRaRy('hy.intf.ybjkfw.dll');

MyHandle:=LoadLibRaRy('HelloDll.dll');
if MyHandle=0 then
begin
ShowMessage('动态连接库加载失败,其原因(错误代码)是:'+IntToStr(GetLastError));
end;
@MyFunc:=GetProcAddress(MyHandle,'SayHello');
if @Myfunc <> nil then
begin
// iRet := Myfunc(2,pInpar,pOutPar);
// sOutPar :=IntToStr(iRet)+'|'+StrPas(pOutPar);
iRet:= Myfunc(sInPar,sOutPar);
ShowMessage(sOutPar);
end;

except
on e:Exception do
begin
ShowMessage('异常:'+e.Message);
end;
end;
end;

end.

3、设置宿主进程与调试

如果遇到无法单步的时候 可以看下 下图部分的设置

 

posted on   金宝的博客  阅读(446)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2020-05-08 中检测到有潜在危险的 Request.Form 值

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示