近来因为工作的原因,需要写一个Delphi的dll供他人调用,于是写了几个例子,供大家参考。
1 。Delphi 的dll: Barcode.dll
HisInterfaceDataModUnit.pas:
用Delphi调用的例子:
用C#调用的例子:
用VF调用的例子:
1 。Delphi 的dll: Barcode.dll
library Barcode;
uses
SysUtils,
Forms,
Windows,
Messages,
Classes,
HisInterfaceDataModUnit in ' HisInterfaceDataModUnit.pas ' {HisInterfaceDataModule: TDataModule} ;
//myRapFuncs in ' myRapFuncs.pas ' ;
{$R *.res}
procedure PrintBarcode(Barcode,PatNo,PatName,SickBed,ItemStr,Sample:PChar);stdcall;
//注意如果传入字符串类型的参数要用: PChar
begin
if not Assigned(HisInterfaceDataModule) then
HisInterfaceDataModule: = THisInterfaceDataModule.Create(nil);
FBarcode: = Barcode;
FPatNo: = PatNo;
FPatName: = PatName;
FSickBed: = SickBed;
FItemStr: = ItemStr;
FSample: = Sample;
HisInterfaceDataModule.Print;
end;
exports
PrintBarcode;
begin
end.
uses
SysUtils,
Forms,
Windows,
Messages,
Classes,
HisInterfaceDataModUnit in ' HisInterfaceDataModUnit.pas ' {HisInterfaceDataModule: TDataModule} ;
//myRapFuncs in ' myRapFuncs.pas ' ;
{$R *.res}
procedure PrintBarcode(Barcode,PatNo,PatName,SickBed,ItemStr,Sample:PChar);stdcall;
//注意如果传入字符串类型的参数要用: PChar
begin
if not Assigned(HisInterfaceDataModule) then
HisInterfaceDataModule: = THisInterfaceDataModule.Create(nil);
FBarcode: = Barcode;
FPatNo: = PatNo;
FPatName: = PatName;
FSickBed: = SickBed;
FItemStr: = ItemStr;
FSample: = Sample;
HisInterfaceDataModule.Print;
end;
exports
PrintBarcode;
begin
end.
HisInterfaceDataModUnit.pas:
unit HisInterfaceDataModUnit;
interface
uses
SysUtils, Classes, ppBarCode2D, ppPrnabl, ppClass, ppCtrls, ppBarCod, ppBands,
ppCache, ppComm, ppRelatv, ppProd, ppReport,myRapFuncs, ppModule,
raCodMod, ppStrtch, ppMemo, ppParameter, ppRichTx,Inifiles;
type
THisInterfaceDataModule = class (TDataModule)
ppReport1: TppReport;
ppParameterList1: TppParameterList;
ppHeaderBand1: TppHeaderBand;
ppDetailBand1: TppDetailBand;
ppBarCode1: TppBarCode;
PaNameLabel: TppLabel;
PatNoLabel: TppLabel;
SickbedLabel: TppLabel;
SampleLabel: TppLabel;
SampleLabel1: TppLabel;
PatNoLabel1: TppLabel;
ppBarCode2: TppBarCode;
PaNameLabel1: TppLabel;
SickbedLabel1: TppLabel;
ppMemo1: TppMemo;
ppMemo2: TppMemo;
ppFooterBand1: TppFooterBand;
raCodeModule1: TraCodeModule;
private
function AddZeroToStr(S: string ;Zero:Integer):String;
function GetIniStr(Section,Ident: string ): string ;
{ Private declarations }
public
procedure Print;
{ Public declarations }
end;
var
HisInterfaceDataModule: THisInterfaceDataModule;
FBarcode,FPatNo,FPatName,FSickBed,FItemStr,FSample: string ;
implementation
uses Variants;
{$R *.dfm}
{ THisInterfaceDataModule }
function THisInterfaceDataModule.AddZeroToStr(S: string ;Zero:Integer):String;
var
L:Integer;
tempStr:String;
begin
tempStr: = S;
L: = Length(Trim(tempStr));
while L < Zero do
begin
Insert( ' 0 ' ,tempStr, 1 );
L: = Length(Trim(tempStr));
end;
Result: = tempStr;
end;
Function THisInterfaceDataModule.GetIniStr(Section,Ident: string ): string ;
var
MyFile:TiniFile;
S:String;
begin
MyFile: = TIniFile.Create( ' c:arcode.ini ' );
S: = '' ;
if myfile.ValueExists(Pchar(Section),Pchar(Ident)) then
S: = myfile.ReadString(Pchar(Section),Pchar(Ident), '' );
MyFile.Free;
rESULT: = S;
end;
procedure THisInterfaceDataModule.Print;
var
i:Integer;
myPrinterName,myDeviceType: string ;
begin
ppBarCode1.Data: = FBarcode;
ppBarCode2.Data: = FBarcode;
PatNoLabel.Caption : = FPatNo;
PaNameLabel.Caption: = FPatName;
SickbedLabel.Caption: = FSickBed;
SampleLabel.Caption: = FSample;
ppMemo1 .Text: = FItemStr;
PatNoLabel1.Caption : = FPatNo;
PaNameLabel1.Caption: = FPatName;
SickbedLabel1.Caption: = FSickBed;
SampleLabel1.Caption: = FSample;
ppMemo2 .Text: = FItemStr;
myDeviceType: = GetIniStr( ' ReportSetup ' , ' DeviceType ' );
if myDeviceType = ' Printer ' then ppReport1.DeviceType: = ' Printer ' else ppReport1.DeviceType: = ' Screen ' ;
myPrinterName: = '' ;
i: = StrToIntDef(GetIniStr( ' PrinterSetup ' , ' PrinterIndex ' ), 0 );
if ppReport1.PrinterSetup.PrinterNames.Count >= i then
myPrinterName: = ppReport1.PrinterSetup.PrinterNames[i];
if myPrinterName <> '' then
ppReport1.PrinterSetup.PrinterName: = myPrinterName;
ppReport1.PrintToDevices;
ppReport1.PrintReport;
end;
end.
interface
uses
SysUtils, Classes, ppBarCode2D, ppPrnabl, ppClass, ppCtrls, ppBarCod, ppBands,
ppCache, ppComm, ppRelatv, ppProd, ppReport,myRapFuncs, ppModule,
raCodMod, ppStrtch, ppMemo, ppParameter, ppRichTx,Inifiles;
type
THisInterfaceDataModule = class (TDataModule)
ppReport1: TppReport;
ppParameterList1: TppParameterList;
ppHeaderBand1: TppHeaderBand;
ppDetailBand1: TppDetailBand;
ppBarCode1: TppBarCode;
PaNameLabel: TppLabel;
PatNoLabel: TppLabel;
SickbedLabel: TppLabel;
SampleLabel: TppLabel;
SampleLabel1: TppLabel;
PatNoLabel1: TppLabel;
ppBarCode2: TppBarCode;
PaNameLabel1: TppLabel;
SickbedLabel1: TppLabel;
ppMemo1: TppMemo;
ppMemo2: TppMemo;
ppFooterBand1: TppFooterBand;
raCodeModule1: TraCodeModule;
private
function AddZeroToStr(S: string ;Zero:Integer):String;
function GetIniStr(Section,Ident: string ): string ;
{ Private declarations }
public
procedure Print;
{ Public declarations }
end;
var
HisInterfaceDataModule: THisInterfaceDataModule;
FBarcode,FPatNo,FPatName,FSickBed,FItemStr,FSample: string ;
implementation
uses Variants;
{$R *.dfm}
{ THisInterfaceDataModule }
function THisInterfaceDataModule.AddZeroToStr(S: string ;Zero:Integer):String;
var
L:Integer;
tempStr:String;
begin
tempStr: = S;
L: = Length(Trim(tempStr));
while L < Zero do
begin
Insert( ' 0 ' ,tempStr, 1 );
L: = Length(Trim(tempStr));
end;
Result: = tempStr;
end;
Function THisInterfaceDataModule.GetIniStr(Section,Ident: string ): string ;
var
MyFile:TiniFile;
S:String;
begin
MyFile: = TIniFile.Create( ' c:arcode.ini ' );
S: = '' ;
if myfile.ValueExists(Pchar(Section),Pchar(Ident)) then
S: = myfile.ReadString(Pchar(Section),Pchar(Ident), '' );
MyFile.Free;
rESULT: = S;
end;
procedure THisInterfaceDataModule.Print;
var
i:Integer;
myPrinterName,myDeviceType: string ;
begin
ppBarCode1.Data: = FBarcode;
ppBarCode2.Data: = FBarcode;
PatNoLabel.Caption : = FPatNo;
PaNameLabel.Caption: = FPatName;
SickbedLabel.Caption: = FSickBed;
SampleLabel.Caption: = FSample;
ppMemo1 .Text: = FItemStr;
PatNoLabel1.Caption : = FPatNo;
PaNameLabel1.Caption: = FPatName;
SickbedLabel1.Caption: = FSickBed;
SampleLabel1.Caption: = FSample;
ppMemo2 .Text: = FItemStr;
myDeviceType: = GetIniStr( ' ReportSetup ' , ' DeviceType ' );
if myDeviceType = ' Printer ' then ppReport1.DeviceType: = ' Printer ' else ppReport1.DeviceType: = ' Screen ' ;
myPrinterName: = '' ;
i: = StrToIntDef(GetIniStr( ' PrinterSetup ' , ' PrinterIndex ' ), 0 );
if ppReport1.PrinterSetup.PrinterNames.Count >= i then
myPrinterName: = ppReport1.PrinterSetup.PrinterNames[i];
if myPrinterName <> '' then
ppReport1.PrinterSetup.PrinterName: = myPrinterName;
ppReport1.PrintToDevices;
ppReport1.PrintReport;
end;
end.
用Delphi调用的例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, Grids, DBGrids, DBClient, MConnect, SConnect,
ADODB, Provider;
type
TForm1 = class (TForm)
Button6: TButton;
Button1: TButton;
procedure Button6Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure Print2(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
///// DLL文件:Barcode.dll
///// 函数名 PrintBarcode
// 参数依次为
// 1:条码
// 2:住院号
// 3:姓名
// 4:病区病床
// 5:项目名
// 6:样本类型(如 红色管 、蓝色管、 小便)
{
现在显示打印预览 是为了调试方便,以后正式版本不显示 打印预览
}
// 静态调用
Procedure PrintBarcode(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );stdcall; External ' Barcode.dll ' ;
implementation
{$R *.dfm}
// 动态调用
procedure TForm1.Print2(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );
type
TMyProc = procedure (Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string ) ;stdcall;
var
DLLHandle: THandle;
DLLSub: TMyProc;
DllPath: string ;
begin
DllPath: = ' Barcode.dll ' ;
if DllPath = '' then Exit;
DLLHandle : = LoadLibrary(Pchar(DllPath));
if DLLHandle <> 0 then
begin
@DLLSub : = GetProcAddress(DLLHandle, ' PrintBarcode ' );
if Assigned(DLLSub) then
begin
DLLSub(Barcode,PatNo,PatName,SickBed,ItemStr,Sample);
end;
end;
end;
procedure TForm1.Button6Click(Sender: TObject); // 静态调用
begin
PrintBarcode( ' 10001 ' , ' 23333 ' , ' 李先念 ' , ' 一区45床 ' , ' 肝功能组,肾功能组,蛋白组,电解质 ' , ' 红色管 ' );
end;
procedure TForm1.Button1Click(Sender: TObject); // 动态调用
begin
Print2( ' 10001 ' , ' 23333 ' , ' 李先念 ' , ' 一区45床 ' , ' 肝功能组,肾功能组,蛋白组,电解质 ' , ' 红色管 ' );
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, Grids, DBGrids, DBClient, MConnect, SConnect,
ADODB, Provider;
type
TForm1 = class (TForm)
Button6: TButton;
Button1: TButton;
procedure Button6Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure Print2(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
///// DLL文件:Barcode.dll
///// 函数名 PrintBarcode
// 参数依次为
// 1:条码
// 2:住院号
// 3:姓名
// 4:病区病床
// 5:项目名
// 6:样本类型(如 红色管 、蓝色管、 小便)
{
现在显示打印预览 是为了调试方便,以后正式版本不显示 打印预览
}
// 静态调用
Procedure PrintBarcode(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );stdcall; External ' Barcode.dll ' ;
implementation
{$R *.dfm}
// 动态调用
procedure TForm1.Print2(Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string );
type
TMyProc = procedure (Barcode,PatNo,PatName,SickBed,ItemStr,Sample: string ) ;stdcall;
var
DLLHandle: THandle;
DLLSub: TMyProc;
DllPath: string ;
begin
DllPath: = ' Barcode.dll ' ;
if DllPath = '' then Exit;
DLLHandle : = LoadLibrary(Pchar(DllPath));
if DLLHandle <> 0 then
begin
@DLLSub : = GetProcAddress(DLLHandle, ' PrintBarcode ' );
if Assigned(DLLSub) then
begin
DLLSub(Barcode,PatNo,PatName,SickBed,ItemStr,Sample);
end;
end;
end;
procedure TForm1.Button6Click(Sender: TObject); // 静态调用
begin
PrintBarcode( ' 10001 ' , ' 23333 ' , ' 李先念 ' , ' 一区45床 ' , ' 肝功能组,肾功能组,蛋白组,电解质 ' , ' 红色管 ' );
end;
procedure TForm1.Button1Click(Sender: TObject); // 动态调用
begin
Print2( ' 10001 ' , ' 23333 ' , ' 李先念 ' , ' 一区45床 ' , ' 肝功能组,肾功能组,蛋白组,电解质 ' , ' 红色管 ' );
end;
end.
用C#调用的例子:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintBarcode("10001","23333","李先念","一区45床","肝功能组,肾功能组,蛋白组,电解质","红色管");
}
[DllImport("Barcode.dll", EntryPoint = "PrintBarcode", CharSet = CharSet.Ansi ,
CallingConvention = CallingConvention.StdCall)]
public extern static void PrintBarcode(string Barcode, string PatNo, string PatName,
string SickBed, string ItemStr, string Sample);
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintBarcode("10001","23333","李先念","一区45床","肝功能组,肾功能组,蛋白组,电解质","红色管");
}
[DllImport("Barcode.dll", EntryPoint = "PrintBarcode", CharSet = CharSet.Ansi ,
CallingConvention = CallingConvention.StdCall)]
public extern static void PrintBarcode(string Barcode, string PatNo, string PatName,
string SickBed, string ItemStr, string Sample);
}
}
用VF调用的例子:
DECLARE PrintBarcode IN Barcode.dll STRING @ , STRING @,STRING @ ,STRING @ ,STRING
@ ,STRING @
STORE ' 10001 ' TO Barcode
STORE ' 23333 ' TO PatNo
STORE ' 李先念 ' TO PatName
STORE ' 一区45床 ' TO SickBed
STORE ' 肝功能组,肾功能组,蛋白组,电解质 ' TO temStr
STORE ' 红色管 ' TO Sample1
PrintBarcode(@Barcode,@PatNo,@PatName,@SickBed,@temStr,@Sample1)
@ ,STRING @
STORE ' 10001 ' TO Barcode
STORE ' 23333 ' TO PatNo
STORE ' 李先念 ' TO PatName
STORE ' 一区45床 ' TO SickBed
STORE ' 肝功能组,肾功能组,蛋白组,电解质 ' TO temStr
STORE ' 红色管 ' TO Sample1
PrintBarcode(@Barcode,@PatNo,@PatName,@SickBed,@temStr,@Sample1)