DllData.INC的内容
type
PGlobalData=^TGlobalDllData;
TGlobalDLLData=String[255];
library GetConnStr;
{ 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
ShareMem,Windows,SysUtils,Classes;
const
CMMFileName
char='SharedMapData';
{$I DllData.INC}
var
GlobalData
GlobalData;
MapHandle:THandle;
procedure GetDllData(var ADllData:string);stdcall;
begin
ADllData:=GlobalData^;
end;
procedure SetDllData(AGlobalData:string);stdcall;
begin
GlobalData^:=AGlobalData;
end;
procedure OpenSharedData();
var
Size:integer;
begin
Size:=sizeof(TGlobalDllData);
MapHandle:=CreateFileMapping(Dword(-1),nil,PAGE_READWRITE,0,Size,CMMFileName);
if MapHandle=0 then
RaiseLastWIN32Error;
GlobalData:=MapViewOfFile(MapHandle,FILE_MAP_ALL_ACCESS,0,0,SIZE);
//GlobalData^:='SharedLib';
//GlobalData^.I:=1;
if GlobalData=nil then
begin
CloseHandle(MapHandle);
RaiseLastWin32Error;
end;
end;
procedure CloseSharedData();
begin
UnMapViewOfFile(GlobalData);
CloseHandle(MapHandle);
end;
Procedure DllEntryPoint(DWReason
word);
begin
case dwReason of
DLL_PROCESS_ATTACH : OpenSharedData;
DLL_PROCESS_DETACH : CloseSharedData;
end;
end;
exports
GetDllData,SetDllData;
begin
DllProc:=@DllEntryPoint;
DllEntryPoint(DLL_PROCESS_ATTACH);
end.
data.inc是一个独立的文件,你写在一个文件也一样的。
编译成DLl文件后就可以调用了。我在VB中测试过是没有问题的。如果是用Delphi调用
传递的类型可以更多一些。这个是我现在的项目用过的,应该没有什么问题的。