关于LoadLibrary的奇怪问题!!!!!!!(100分)

  • 主题发起人 mycreatedream
  • 开始时间
M

mycreatedream

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个用vc6写的dll,用VB测试LoadLibrary和LoadLibraryEx都成功,<br>但是在Delphi6中LoadLibrary竟然返回0,而且在程序退出时(调试状态)还<br>出现非法访问内存的错误,真是百思不得其解,望各位高手指教一二。<br><br>procedure TForm1.btnRegDllClick(Sender: TObject);<br>var<br>&nbsp; sFileName:string;<br>&nbsp; lHandle:HMODULE;<br>begin<br>&nbsp; sFileName:=Edit1.Text;<br>&nbsp; lHandle:=LoadLibrary(@sFileName[1]);<br>&nbsp; Caption:=IntToStr(lHandle);<br>&nbsp; if lHandle&lt;&gt;0 then FreeLibrary(lHandle);<br>end;
 
我试了一下load系统的dll,没有问题,这样看来好像是我用vc6座的dll出问题了,<br>但是我用vb6来LoadLibrary又可以成功,真是搞不懂
 
sFileName和lHandle都有问题,给你个个例子,参考一下:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; //buffer: pchar;<br>&nbsp; data, result: array[0..512]of char;<br>&nbsp; h : HInst;<br>&nbsp; fn, fn2 : function (lpBuffer: PChar; uSize: UINT; Result: PChar): UINT; stdcall;<br>&nbsp; pn: procedure;stdcall;<br>&nbsp; //fs:LPCSTR;<br>&nbsp; c_len, d_len, i: integer;<br>&nbsp; s, s2: string;<br>begin<br><br>&nbsp; &nbsp; h:=LoadLibrary('code_dll.dll');<br>&nbsp; &nbsp; if h=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showMessage('load library error!');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; @fn := GetProcAddress(h,Pchar('golay_code'));<br>&nbsp; &nbsp; @fn2 := GetProcAddress(h,Pchar('golay_decode'));<br>&nbsp; &nbsp; @pn := GetProcAddress(h,Pchar('generate_golay_table'));<br>&nbsp; &nbsp; if (@fn=nil)or(@pn=nil) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showMessage('get proc address error!');<br>&nbsp; &nbsp; &nbsp; freelibrary(h);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; data := '12345';<br>&nbsp; &nbsp; //getmem(buffer,512);<br>&nbsp; &nbsp; pn;<br>&nbsp; &nbsp; c_len := fn(data,5,result);<br>&nbsp; &nbsp; d_len := fn2(result,c_len,data);<br><br>&nbsp; &nbsp; s := '';<br>&nbsp; &nbsp; for i:=0 to c_len-1 do<br>&nbsp; &nbsp; s := s+ format('%x/',[ord(result)]);<br>&nbsp; &nbsp; //caption := s;<br><br>&nbsp; &nbsp; s2 := '';<br>&nbsp; &nbsp; for i:=0 to d_len-1 do<br>&nbsp; &nbsp; s2 := s2+ format('%x/',[ord(data)]);<br>&nbsp; &nbsp; ShowMessage('golay_code:'+s+#13+'golay_decode:'+s2);<br><br>&nbsp; &nbsp; freelibrary(h);<br><br>end;<br>
 
to zw84611: <br>&nbsp; &nbsp; 我认为我的类型是没有错的,而且我试过load系统的dll就可以,<br>只是我那个vc6做的ATL Object dll就不行(在d6中不行,在vb中load就可以)
 
是路径问题?<br>procedure TForm1.btnRegDllClick(Sender: TObject);<br>var<br>&nbsp; sFileName:string;<br>&nbsp; lHandle:HMODULE;<br>begin<br>&nbsp; sFileName:=Edit1.Text;<br>&nbsp; if FileExists(sFileName) then ShowMessage('FileExists');<br>&nbsp; lHandle:=LoadLibrary(PChar(sFileName));<br>&nbsp; Caption:=IntToStr(lHandle);<br>&nbsp; if lHandle&lt;&gt;0 then FreeLibrary(lHandle);<br>end;
 
var lHandle: function: integer;stdcall;<br><br>lHandle:=LoadLibrary(pchar(sFileName));
 
1. 为什么是HModule而不是THandle? 有什么区别?<br>2. 为什么是@sFileName[1] 而不是PChar(sFileName)?有什么区别?<br><br>也就是说, 为什么看起来那么复杂?呵呵
 
顶部