R
RJU
Unregistered / Unconfirmed
GUEST, unregistred user!
看了很多关于这方面的问题,仍然不得其解<br>在DLL中使用PCHAR作为参数的奇怪问题<br><br>有如下的测试程序(没有加入SHAREMEM单元),在DLL中有一个函数<br> Function GetUserName(useridchar;var vNameChar):boolean;<br> begin<br> SHOWMESSAGE(USERID);//在此跟踪,得到的USERID经常是乱码<br> end;<br><br> 在多次尝试中,得到下面的程序是能正常运行,但感觉无法保证程序得稳定性.问题如下<br> 1:能否解释 注释部分出现的现象是什么原因?<br> 2:对在DLL中传递字符串常量和返回字符串有什么好的建议<br> 3:对在DLL中传递一个自定义类型的数据有什么好的方法?<br><br> var<br> GetuserName:TMgetuserName;<br> DllPathFile,temp: String;<br> cHnd: THandle;<br> userid,vNamechar;<br> begin<br> cHnd:=0;<br> DllPathFile :=Trim('my.dll');<br> if NOT FileExists(DllPathFile) Then<br> Exit;<br> GetMem(vName,255);<br> Try<br> strPCopy(vName,'00');<br> cHnd :=LoadLibrary(PChar(DllPathFile));<br> if cHnd>0 then<br> begin<br> MgetuserName:=GetProcAddress(cHnd,'MgetuserName');<br> temp:='291';<br> UserID:=PChar(temp); // 只有这样赋值,参数才能正确传递,如果用 <br> // GetMem,setPCopy的方法,传递的<br> // USERID的值是乱码!!<br> //ShowMessage(temp); // 如果没有屏蔽这句,在DLL中跟踪会发现传递的userID的值是乱码.<br><br> if Assigned(MgetuserName) then<br> begin<br> MgetuserName(UserID,vName);<br> ShowMessage(vName);<br> end;<br> end;<br> finally<br> if cHnd>0 then<br> freelibrary(cHnd);<br> FreeMem(vName);<br> end;<br>end;<br><br>