高手求救!!!!!!!
//DLL
library PDM;{ 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
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
oleCtrls,
Dialogs,
Activex,
sconnect,
DB,
DBClient,
form_dm in 'form_dm.pas' {DM: TDataModule};type
TSockCon=procedure(Asock:TSocketConnection);stdcall;
procedure SetConnected(Ahandle:Thandle); export;stdcall;
var
vSock:TSockCon;
begin
Application.Handle:=Ahandle;
DM:=TDM.Create(application);
@vSock:=GetProcAddress(GetModuleHandle(nil),'SockCon');
if @vSock =nil then
vSock(DM.SOCKETCONNECTION);
end;
{$R *.res}
exports
SetConnected name 'SetConnected';
begin
end;//EXE
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,sconnect, StdCtrls, Grids, DBGrids, DB, DBClient;type
TForm1 = class(TForm)
Button1: TButton;
ClientDataSet1: TClientDataSet;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSD=procedure(Ahandle:Thandle);stdcall;
var
Form1: TForm1;
PROCEDURE VSOCK(ASOCK:TSocketConnection);STDCALL;
implementation{$R *.dfm}PROCEDURE VSOCK(ASOCK:TSocketConnection);
BEGIN
Form1.ClientDataSet1.RemoteServer:=ASOCK;
END;procedure TForm1.Button1Click(Sender: TObject);
var
theHandle:THandle;
MyProc:TSD;
begin
theHandle:=LoadLibrary('PDM.dll');
if theHandle <> 0 then
begin
@MyProc := GetProcAddress(theHandle,'SetConnected');
if @MyProc <> nil then
begin
SHOWMESSAGE('PDM.DLL调用成功');
FreeLibrary(theHandle);
end
else MessageBox(0,'exe 在载入 PDM 。dll 时失败','信息',MB_OK);
end;
end;end.
出现 Access violation at address 00000000.read of address 00000000 错误提示 没分了。。请教
//DLL
library PDM;{ 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
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
oleCtrls,
Dialogs,
Activex,
sconnect,
DB,
DBClient,
form_dm in 'form_dm.pas' {DM: TDataModule};type
TSockCon=procedure(Asock:TSocketConnection);stdcall;
procedure SetConnected(Ahandle:Thandle); export;stdcall;
var
vSock:TSockCon;
begin
Application.Handle:=Ahandle;
DM:=TDM.Create(application);
@vSock:=GetProcAddress(GetModuleHandle(nil),'SockCon');
if @vSock =nil then
vSock(DM.SOCKETCONNECTION);
end;
{$R *.res}
exports
SetConnected name 'SetConnected';
begin
end;//EXE
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,sconnect, StdCtrls, Grids, DBGrids, DB, DBClient;type
TForm1 = class(TForm)
Button1: TButton;
ClientDataSet1: TClientDataSet;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSD=procedure(Ahandle:Thandle);stdcall;
var
Form1: TForm1;
PROCEDURE VSOCK(ASOCK:TSocketConnection);STDCALL;
implementation{$R *.dfm}PROCEDURE VSOCK(ASOCK:TSocketConnection);
BEGIN
Form1.ClientDataSet1.RemoteServer:=ASOCK;
END;procedure TForm1.Button1Click(Sender: TObject);
var
theHandle:THandle;
MyProc:TSD;
begin
theHandle:=LoadLibrary('PDM.dll');
if theHandle <> 0 then
begin
@MyProc := GetProcAddress(theHandle,'SetConnected');
if @MyProc <> nil then
begin
SHOWMESSAGE('PDM.DLL调用成功');
FreeLibrary(theHandle);
end
else MessageBox(0,'exe 在载入 PDM 。dll 时失败','信息',MB_OK);
end;
end;end.
出现 Access violation at address 00000000.read of address 00000000 错误提示 没分了。。请教
if @vSock =nil then ///地址为空的时候,调用下面的程序肯定报错,应改为<>
vSock(DM.SOCKETCONNECTION);
end;
keiy
@vSock:=GetProcAddress(GetModuleHandle(nil),'VSOCK'); //这里改成这个'VSOCK'
If @vSock <>nil then //这里由=改为<>,但总出现'回调失败信息'如果是=,就又会出现上面的地址错误
vSock(DM.SOCKETCONNECTION)ELSE
SHOWMESSAGE('回调失败');
end;
theHandle:THandle;
MyProc:TSD;
begin
theHandle:=LoadLibrary('PDM.dll');
if theHandle <> 0 then
begin
@MyProc := GetProcAddress(theHandle,'SetConnected');
if @MyProc <> nil then
begin
MYPROC(APPLICATION.HANDLE);//上面的程序加入这句.否则不会运行回调;
SHOWMESSAGE('PDM.DLL调用成功');
FreeLibrary(theHandle);
end
else MessageBox(0,'exe 在载入 PDM 。dll 时失败','信息',MB_OK);
end;
end;
这个也不行,你的主程序(EXE)没有导出函数的,你是不是想在dll中调用主程序的函数?
直接将函数地址做为参数传给DLL就可以了
EXPORTS
VSOCK NAME 'VSOCK'
加入这句情况依旧.
keiy
我讲一下上面的情况.上面的DLL是一个窗体DLL,这个窗体是个DATA MODULE,里面放了个SOCKETCONNECTION
现在我要实现的是:将DLL里的SOCKETCONNECTION调入进EXE,请高手帮帮忙.
dll部分:
@vSock:=GetProcAddress(GetModuleHandle(nil),'SockCon');
if @vSock =nil then //改成<>
vSock(DM.SOCKETCONNECTION);
end;
//EXE
缺少
exports
VSOCK name 'SockCon';