如何在DELPHI中动态调用DLL文件!加急!!! ( 积分: 30 )

  • 主题发起人 主题发起人 飘落的枫叶
  • 开始时间 开始时间

飘落的枫叶

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DELPHI中动态调用DLL文件,可否请哪位大侠指点一二,最好是有个例子。
 
如何在DELPHI中动态调用DLL文件,可否请哪位大侠指点一二,最好是有个例子。
 
以下是动态调用Lindo线性规划dll链接库的范例,请参考!<br>var<br> &nbsp;LindoDllHandle :THandle;<br><br> &nbsp;ILINDO :Procedure; Stdcall;<br> &nbsp;INIT &nbsp; :Procedure; Stdcall;<br> &nbsp;LUNOPN :Procedure(Lunit: PLongint; Lfname: PLongint; ByValKfname: Pchar; Inrout: PLongint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Notfmt: PLongint; Lutrmi: PLongint; Lutrmo: PLongint); Stdcall;<br> &nbsp;CAPOUT :Procedure(Dunit: PLongint); Stdcall;<br> &nbsp;QUIET &nbsp;:Procedure(j: PLongint); &nbsp; &nbsp; Stdcall;<br> &nbsp;DEFROW :Procedure(Idir: PLongint; Rhs: PSingle; Idrow: PLongint; Trouble: PLongint); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Stdcall;<br> &nbsp;APPCOL :Procedure(ByValKname: Pchar; Nonz: PLongint; Value: PSingle; Idrow: PLongint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trouble: PLongint); Stdcall;<br> &nbsp;GO &nbsp; &nbsp; :Procedure(Limgo: PLongint; Istat: PLongint); &nbsp;Stdcall;<br> &nbsp;REPROW :Procedure(i: PLongint; Primal: PSingle; Dual: PSingle); Stdcall;<br> &nbsp;REPVAR :Procedure(i: PLongint; Primal: PSingle; Dual: PSingle); Stdcall;<br> &nbsp;LSEXIT :Procedure; Stdcall;<br> &nbsp;MAKINT :Procedure(i: PLongint); &nbsp;Stdcall;<br>begin<br> &nbsp; //动态载入 &quot; LNDDLL32.DLL &quot; 文件<br> &nbsp; str := GetCurrentDir;<br> &nbsp; str := str + '/LNDDLL32.DLL';<br> &nbsp; LindoDllHandle := loadlibrary(Pchar(str));<br> &nbsp; if &nbsp;LindoDllHandle&amp;lt;32 then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;Application.MessageBox('代码结构载入失败!',PChar('系统提示'),MB_ICONWARNING+MB_OK);<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; end;<br> &nbsp; @ILINDO := GetProcAddress(LindoDllHandle,'ILINDO_STD');<br> &nbsp; @INIT &nbsp; := GetProcAddress(LindoDllHandle,'INIT_STD');<br> &nbsp; @LUNOPN := GetProcAddress(LindoDllHandle,'LUNOPNX_STD');<br> &nbsp; @CAPOUT := GetProcAddress(LindoDllHandle,'CAPOUT_STD');<br> &nbsp; @QUIET &nbsp;:= GetProcAddress(LindoDllHandle,'QUIET_STD');<br> &nbsp; @DEFROW := GetProcAddress(LindoDllHandle,'DEFROW_STD');<br> &nbsp; @APPCOL := GetProcAddress(LindoDllHandle,'APPCOLX_STD');<br> &nbsp; @GO &nbsp; &nbsp; := GetProcAddress(LindoDllHandle,'GO_STD');<br> &nbsp; @REPROW := GetProcAddress(LindoDllHandle,'REPROW_STD');<br> &nbsp; @REPVAR := GetProcAddress(LindoDllHandle,'REPVAR_STD');<br> &nbsp; @LSEXIT := GetProcAddress(LindoDllHandle,'LSEXIT_STD');<br> &nbsp; @MAKINT := GetProcAddress(LindoDllHandle,'MAKINT_STD');<br><br> &nbsp; FreeLibrary(LindoDllHandle);<br>end;
 
怎么在工程里添加DLL文件呢
 
小谷朋友,能否把你的那些代码适当地加些注释。我是新手,所以看起来有点费劲。谢谢!!!
 
注释如下:[:)]<br>var<br> &nbsp;LindoDllHandle :THandle;<br><br> &nbsp;ILINDO :Procedure; Stdcall; &nbsp; &nbsp; &nbsp; &nbsp;//定义过程变量<br> &nbsp;INIT &nbsp; :Procedure; Stdcall;<br> &nbsp;LUNOPN :Procedure(Lunit: PLongint; Lfname: PLongint; ByValKfname: Pchar; Inrout: PLongint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Notfmt: PLongint; Lutrmi: PLongint; Lutrmo: PLongint); Stdcall;<br> &nbsp;CAPOUT :Procedure(Dunit: PLongint); Stdcall;<br> &nbsp;QUIET &nbsp;:Procedure(j: PLongint); &nbsp; &nbsp; Stdcall;<br> &nbsp;DEFROW :Procedure(Idir: PLongint; Rhs: PSingle; Idrow: PLongint; Trouble: PLongint); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Stdcall;<br> &nbsp;APPCOL :Procedure(ByValKname: Pchar; Nonz: PLongint; Value: PSingle; Idrow: PLongint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trouble: PLongint); Stdcall;<br> &nbsp;GO &nbsp; &nbsp; :Procedure(Limgo: PLongint; Istat: PLongint); &nbsp;Stdcall;<br> &nbsp;REPROW :Procedure(i: PLongint; Primal: PSingle; Dual: PSingle); Stdcall;<br> &nbsp;REPVAR :Procedure(i: PLongint; Primal: PSingle; Dual: PSingle); Stdcall;<br> &nbsp;LSEXIT :Procedure; Stdcall;<br> &nbsp;MAKINT :Procedure(i: PLongint); &nbsp;Stdcall;<br>begin<br> &nbsp; //动态载入 &quot; LNDDLL32.DLL &quot; 文件<br> &nbsp; str := GetCurrentDir; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//获得当前路径<br> &nbsp; str := str + '/LNDDLL32.DLL'; &nbsp; &nbsp; &nbsp; //指定库文件<br> &nbsp; LindoDllHandle := loadlibrary(Pchar(str)); &nbsp; //引导库文件<br> &nbsp; if &nbsp;LindoDllHandle&amp;lt;32 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//判断是否引导成功,出错小于则32<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;Application.MessageBox('代码结构载入失败!',PChar('系统提示'),MB_ICONWARNING+MB_OK);<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; end;<br> &nbsp; @ILINDO := GetProcAddress(LindoDllHandle,'ILINDO_STD'); &nbsp;<br> &nbsp; //打开库文件,获得入口地址<br> &nbsp; @INIT &nbsp; := GetProcAddress(LindoDllHandle,'INIT_STD');<br> &nbsp; @LUNOPN := GetProcAddress(LindoDllHandle,'LUNOPNX_STD');<br> &nbsp; @CAPOUT := GetProcAddress(LindoDllHandle,'CAPOUT_STD');<br> &nbsp; @QUIET &nbsp;:= GetProcAddress(LindoDllHandle,'QUIET_STD');<br> &nbsp; @DEFROW := GetProcAddress(LindoDllHandle,'DEFROW_STD');<br> &nbsp; @APPCOL := GetProcAddress(LindoDllHandle,'APPCOLX_STD');<br> &nbsp; @GO &nbsp; &nbsp; := GetProcAddress(LindoDllHandle,'GO_STD');<br> &nbsp; @REPROW := GetProcAddress(LindoDllHandle,'REPROW_STD');<br> &nbsp; @REPVAR := GetProcAddress(LindoDllHandle,'REPVAR_STD');<br> &nbsp; @LSEXIT := GetProcAddress(LindoDllHandle,'LSEXIT_STD');<br> &nbsp; @MAKINT := GetProcAddress(LindoDllHandle,'MAKINT_STD');<br><br> &nbsp; FreeLibrary(LindoDllHandle);//释放库文件<br>end; &nbsp;
 
后退
顶部