DLL回调出现错误
高手求救!!!!!!!
//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;
interface
uses
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 错误提示 没分了。。请教。。。
[解决办法]
@vSock:=GetProcAddress(GetModuleHandle(nil),'SockCon'); //这里就出错了,SockCon没有这个导出函数
if @vSock =nil then ///地址为空的时候,调用下面的程序肯定报错,应改为<>
vSock(DM.SOCKETCONNECTION);
end;
[解决办法]
@vSock:=GetProcAddress(GetModuleHandle(nil),'VSOCK'); //这里改成这个'VSOCK'
这个也不行,你的主程序(EXE)没有导出函数的,你是不是想在dll中调用主程序的函数?
直接将函数地址做为参数传给DLL就可以了