library工程:
Dll 输出 GetResultfunction GetResult(AppHandle: THandle): TDataSet;
begin
Application.handle:= AppHandle;
DataModule1:= TDataModule1.Create(application);
result:= DataModule1.ADODataSet1;
end;program工程:
type
TGetResult = function (AppHandle: THandle): TDataSet; stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
DllLib: THandle;
GetResult: TGetResult;
begin
try
DllLib:= LoadLibrary('prjDll');
if DllLib <> 0 then
@GetResult:= GetProcAddress(DllLib, 'GetResult');
if @GetResult <> nil then
DataSource1.DataSet:= GetResult(Application.Handle);// ???
finally
FreeLibrary(dllLib);
end;
当运行到上面一句时,弹出prjUserDll.exe Raise Exception Class EInvalidPointer with message 'Invalid pointer operation', process stopped.
User Step or Run to continue.
请问这是怎么回事?该怎样解决?谢谢!!!
Dll 输出 GetResultfunction GetResult(AppHandle: THandle): TDataSet;
begin
Application.handle:= AppHandle;
DataModule1:= TDataModule1.Create(application);
result:= DataModule1.ADODataSet1;
end;program工程:
type
TGetResult = function (AppHandle: THandle): TDataSet; stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
DllLib: THandle;
GetResult: TGetResult;
begin
try
DllLib:= LoadLibrary('prjDll');
if DllLib <> 0 then
@GetResult:= GetProcAddress(DllLib, 'GetResult');
if @GetResult <> nil then
DataSource1.DataSet:= GetResult(Application.Handle);// ???
finally
FreeLibrary(dllLib);
end;
当运行到上面一句时,弹出prjUserDll.exe Raise Exception Class EInvalidPointer with message 'Invalid pointer operation', process stopped.
User Step or Run to continue.
请问这是怎么回事?该怎样解决?谢谢!!!
你不应该将你的创建DataModule在调用GetResult时才开始,你应该在执行LoadLibrary时就应该创建DataModule,你可以在library工程中加在以下代码procedure CreateMDL;
begin
DataModule1:= TDataModule1.Create(application);
end;
function GetResult(AppHandle: THandle): TDataSet;
begin
Application.handle:= AppHandle;
result:= DataModule1.ADODataSet1;
end;initialization
CreateMDL;
end;
我在GetResult函数所在的单元加上了
procedure CreateMDL;
begin
DataModule1:= TDataModule1.Create(application);
end;initialization
CreateMDL;
但还是出现同样的错误提示?
To daniel007(添):
我想问一下:需要引用ShareMem单元时,ShareMem单元是否只需作为program和library工程文件的第一个引用单元就行?引用和被引用的具体单元的引用单元里无需添加ShareMem单元?
type
TGetResult = function (AppHandle: THandle): TDataSet; stdcall;GetResult: TGetResult;
procedure loadproject;
var
DllLib: THandle;
begin
try
DllLib:= LoadLibrary('prjDll');
if DllLib <> 0 then
@GetResult:= GetProcAddress(DllLib, 'GetResult');
except
FreeLibrary(dllLib);
end;
end;然后再用你的project静态调用它:
procedure TForm1.Button1Click(Sender: TObject);
begin
try
if @GetResult <> nil then
DataSource1.DataSet:= GetResult(Application.Handle);
finally
FreeLibrary(dllLib);
end;
end;
type
TGetResult = function (AppHandle: THandle): TDataSet; stdcall;var
GetResult: TGetResult;
DllLib: THandle;
procedure loadproject;
begin
try
DllLib:= LoadLibrary('prjDll');
if DllLib <> 0 then
@GetResult:= GetProcAddress(DllLib, 'GetResult');
except
FreeLibrary(dllLib);
end;
end;
放到Package的单元里;然后再用你的project静态调用它:
procedure TForm1.Button1Click(Sender: TObject);
begin
try
if @GetResult <> nil then
DataSource1.DataSet:= GetResult(Application.Handle);
finally
FreeLibrary(dllLib);
end;
end;
暂时解决我调用Dll的困惑。
我另开贴,希望楼上各位参与。