请问用LoadLibrary , GetProcAddress 如何引用API函数?(10分)

  • 主题发起人 主题发起人 icysword
  • 开始时间 开始时间
I

icysword

Unregistered / Unconfirmed
GUEST, unregistred user!
请问用LoadLibrary , GetProcAddress 如何引用API函数? 能举个例子吗?
 
搜一下已答问题可得:<br>-----------------------------------------------------<br>&nbsp; &nbsp; 静态调用:function testfunc;far;external 'c:/run/dll/testdll.dll';<br>&nbsp; &nbsp; 动态调用:temphd:=loadlibrary('c:/run/dll/testdll.dll');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getprocess(...);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; freelibrary(...);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>-----------------------------------------------------<br>var<br>&nbsp; h : THandle;<br>&nbsp; fn : function(PARAMETER_LIST_HERE) : RETURN_TYPE;<br>begin<br>&nbsp; h := LoadLibraryEx(DLL_FILE_PATH,0,0);<br>&nbsp; if h=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; showMessage('load library error!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; @fn := GetProcAddress(h,PROC_NAME); //case sensitive<br>&nbsp; if @fn=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; showMessage('get proc address error!');<br>&nbsp; &nbsp; FreeLibrary(h);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; fn(PARAMETER_LIST_HERE); // call proc or function<br>&nbsp; ...<br>&nbsp; FreeLibrary(h);<br>end;<br><br><br><br>
 
多谢答复, 但在使用中发现问题, 能否再赐教.<br>代码如下(程序产生非法操作):<br>var<br>&nbsp; buffer:pchar;<br>&nbsp; h : THandle;<br>&nbsp; fn : function (lpBuffer: PChar; uSize: UINT): UINT;<br>begin<br>h:=LoadLibrary('kernel32.dll');<br>if h=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; showMessage('load library error!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>@fn := GetProcAddress(h,'GetWindowsDirectoryA');<br>if @fn=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; showMessage('get proc address error!');<br>&nbsp; &nbsp; freelibrary(h);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>getmem(buffer,260);<br>fn(buffer,260);<br>freelibrary(h);<br>end;<br><br>为何会产生非法操作??
 
做两处改动:<br>fn : function (lpBuffer: PChar; uSize: UINT): UINT; stdcall;<br>....<br>@fn := GetProcAddress(h,Pchar('GetWindowsDirectoryA'));
 
呵呵, 成功了. 谢谢了, ^-^
 
后退
顶部