调用vc的dll问题,着急,请帮忙!!! ( 积分: 30 )

  • 主题发起人 主题发起人 emule
  • 开始时间 开始时间
E

emule

Unregistered / Unconfirmed
GUEST, unregistred user!
要调用一vc的dll,他的函数是参数是:<br>char *filename,char *myname<br>其中参数myname是要传出来的<br><br>我这样写对吗?<br>//1.定义<br>TReadMyName = function(filename : pchar; var myname : pchar): integer; stdcall;<br>g_TReadMyName : TReadMyName;<br>//2.<br>var<br> &nbsp; &nbsp;l_filename : array[0..500] of char;<br> &nbsp; &nbsp;l_myname : pchar;<br>if g_TReadMyName(l_filename, l_myname) then<br> &nbsp; &nbsp;showmessage(l_myname);
 
要调用一vc的dll,他的函数是参数是:<br>char *filename,char *myname<br>其中参数myname是要传出来的<br><br>我这样写对吗?<br>//1.定义<br>TReadMyName = function(filename : pchar; var myname : pchar): integer; stdcall;<br>g_TReadMyName : TReadMyName;<br>//2.<br>var<br> &nbsp; &nbsp;l_filename : array[0..500] of char;<br> &nbsp; &nbsp;l_myname : pchar;<br>if g_TReadMyName(l_filename, l_myname) then<br> &nbsp; &nbsp;showmessage(l_myname);
 
老是出现内存错误,要如何写,请帮忙,谢谢!
 
LoadLibrary()
 
首先确定DLL的导出函数是什么方式调用的,stdcall当然是最好的了<br><br>静态调用的话这样就行<br>function FnName( lpszFileName, lpszMyName : PAnsiChar ):Integer;stdcall;external 'DllName.Dll' name the-export-api-namedll;
 
因为你的函数定义形式只适合动态调用,所以在调用函数前必须用LoadLibrary函数载入DLL,否则当然会出错。试着帮你改了一下,希望能对你有所启发:<br>//1.定义<br>TReadMyName = function(filename : pchar;myname : pchar): integer; stdcall;<br>// myname 虽然是用来接收返回字符串的,但由于它是PChar类型的,所以没有必要定义成变参<br>g_TReadMyName : TReadMyName;<br>//2.<br>var<br> &nbsp; &nbsp;Handle:THandle;<br> &nbsp; &nbsp;l_filename : array[0..500] of char;<br> &nbsp; &nbsp;l_myname : pchar;<br>...<br> &nbsp; &nbsp;Handle:=LoadLibrary('这里写上你的DLL文件名');<br> &nbsp; &nbsp;if Handle&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;@g_TReadMyName:=GetProcAddress('这里写上你要调用的DLL函数的名字');<br> &nbsp; &nbsp; &nbsp;if @g_TReadMyName&lt;&gt;nil then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;GetMem(l_myname,g_TReadMyName(l_filename,nil)+1);<br> &nbsp; &nbsp; &nbsp; &nbsp;if g_TReadMyName(l_filename, l_myname)&gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage(l_myname);<br> &nbsp; &nbsp; &nbsp; &nbsp;FreeMem(l_myname)<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FreeLibrary(Handle)<br> &nbsp; end;<br>...
 
VC默认的是cdecl的调用方式,你的l_myname只有定义没有分配内存,用GetMem分配一下,返回值Integer也不是标准的,最好改为Int64。
 
后退
顶部