知道字符串内存句柄,怎样返回字符串内容? ( 积分: 200 )

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

mxxsir

Unregistered / Unconfirmed
GUEST, unregistred user!
使用了一个别人的DLL里面的函数,知道它返回的是字符串的名柄,怎样返回它的内容呢?<br>比如:<br>function&nbsp;M_GetComputerName:HWND;Stdcall;external&nbsp;'SYS.DLL';<br>implementation<br>{$R&nbsp;*.dfm}<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>Var<br>&nbsp;&nbsp;Pstr:HWND;<br>begin<br>&nbsp;&nbsp;Pstr:=M_GetComputerName;<br>&nbsp;&nbsp;ShowMessage(IntToStr(pstr));//这句不知道怎么写?现在它总返回13631548<br>end;<br>求帮个忙。
 
返回指针即可。
 
楼上,我这样返回,不行(太菜了,没用过指针):<br>&nbsp;&nbsp;function&nbsp;MXX_ComputerName:Pointer;stdcall;external&nbsp;DLL;<br>implementation<br>{$R&nbsp;*.dfm}<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ShowMessage(PChar(MXX_ComputerName)^);//这里<br>end;<br>返回乱字符.<br>原来的DLL里的代码类似这样的:<br>library&nbsp;SYS;<br>uses&nbsp;&nbsp;unit2,&nbsp;Windows,&nbsp;&nbsp;Messages,&nbsp;&nbsp;SysUtils,&nbsp;&nbsp;Classes,&nbsp;&nbsp;&nbsp;ExtCtrls,winsock;<br><br>var<br>&nbsp;&nbsp;&nbsp;returnstrhandle:Thandle;//字符串的返回值句柄<br>{$R&nbsp;*.res}<br><br>function&nbsp;M_GetComputerName:Thandle;stdcall;<br>var<br>&nbsp;&nbsp;CNameBuffer,P_returnstr:&nbsp;PChar;<br>&nbsp;&nbsp;returnstr:string;<br>&nbsp;&nbsp;fl_loaded&nbsp;:&nbsp;Boolean;<br>&nbsp;&nbsp;CLen&nbsp;:&nbsp;^DWord;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;GetMem(CNameBuffer,255);<br>&nbsp;&nbsp;&nbsp;&nbsp;returnstrhandle:=globalalloc(GHND,8192);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;P_returnstr:=globallock(returnstrhandle);<br>&nbsp;&nbsp;&nbsp;&nbsp;New(CLen);<br>&nbsp;&nbsp;&nbsp;&nbsp;CLen^:=&nbsp;255;<br>&nbsp;&nbsp;&nbsp;&nbsp;fl_loaded&nbsp;:=&nbsp;GetComputerName(CNameBuffer,CLen^);<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;fl_loaded&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnstr:=StrPas(CNameBuffer)<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnstr:='I&nbsp;Do&nbsp;not&nbsp;known!';<br>&nbsp;&nbsp;&nbsp;&nbsp;FreeMem(CNameBuffer,255);<br>&nbsp;&nbsp;&nbsp;&nbsp;Dispose(CLen);<br>&nbsp;&nbsp;&nbsp;&nbsp;strpcopy(P_returnstr,returnstr);<br>&nbsp;&nbsp;&nbsp;&nbsp;globalunlock(returnstrhandle);<br>&nbsp;&nbsp;&nbsp;&nbsp;result:=returnstrhandle;//就这里返回的<br>end;<br>end.<br>当然,我实际用时,要是知道了Delphi源码,当然不会非要在DLL里返回Handle,可以返回PChar,但是现在是调用别人的其他功能的.谢谢.
 
procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;c&nbsp;:&nbsp;pchar;<br>begin<br>&nbsp;&nbsp;c&nbsp;:=&nbsp;globallock(M_GetComputerName);<br>&nbsp;&nbsp;edit1.Text&nbsp;:=&nbsp;c;<br>end;
 
多谢两位.
 
多人接受答案了。
 
后退
顶部