PChar與String類型轉換問題(100分)

  • 主题发起人 followindy
  • 开始时间
对了,你分配内存不要用GetMem<br>最好用HeapAlloc或者干脆用静态变量<br>array[0..4095]of ansiChar试试!<br>由于在调用DLL的时候,分配内存如果用GetMem类的函数,需要使用<br>Borlandmm.dll,所以我猜测是这个原因。。。
 
估计你的程序不方便邮寄?<br>你看看在执行<br>xcom_recv(52, BMsg, 4096, BID, ....);<br>前后,BMsg的内存地址有没有变化?
 
无忌兄,你的说法不正确,pchar是通用的,string不通用,才需要那个什么dll。
 
不是,我看到的一本(好象是徐新华的书)书里提到不光是string类型需要borlandMM.dll<br>如果是使用getmem分配的内存,在dll里使用,仍然需要 borlandMM.dll,<br>现在出了问题,应该多试试,
 
不对吧?没有道理的。那你说什么函数分配的内存不需要borlandmm.dll呢?
 
这是dll wizard里的一段话,很清楚了。<br>{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }
 
比如HeapAlloc就不要BorlandMM.dll,还有静态变量都不要这个
 
var<br>&nbsp; BMsg, BID: PChar;<br>&nbsp; sMsg, sID: string;<br>begin<br>&nbsp; GetMem(BMsg, 4096);<br>&nbsp; GetMem(BID, 32);<br><br>&nbsp; { 加上这两句试试 }<br>&nbsp; FillChar(BMsg^, 4096, #0);<br>&nbsp; FillChar(BID^, 32, #0);<br><br>&nbsp; xcom_recv(52, BMsg, 4096, BID, ....);<br>&nbsp; sID := StrPas(BID); // 此处编译异常!!!!<br>&nbsp; sID := upperCase(trim(sID));<br>&nbsp; sMsg := StrPas(BMsg);<br>&nbsp; sMsg := upperCase(trim(sMsg));<br>&nbsp; &nbsp;.....<br>&nbsp; FreeMem(BMsg);<br>&nbsp; FreeMem(BID);<br>end;<br><br>如果还是不行,我也认为你的VB函数在Delphi中的声明有问题了(我不懂VB),你最好到CSDN<br>上的BV论坛上去找一个既懂VB又懂C++的人问问,让他翻译成C++代码,就好办了(当然,如果<br>既懂VB又懂Delphi,就再好不过了)。
 
遠帆兄:兩個PChar在xcom_recv調用前後地阯都沒有變化,<br>BMsg:pChar $12FBDC<br>BID: PChar $12FBDB
 
&nbsp;xcom_recv(52, BMsg, 4096, BID, ....);<br>&nbsp; sID := StrPas(BID); // 虽然此处出错,但是显示是上一行调用错误引起堆栈破坏引起的<br>我看了一下你的两个贴,应该是声明的错误,我现在也不知道你用了那个声明<br>vb的 byVal long 对应delphi的是longint,而不是var x:longint,(byRef才是)<br>vb的 byVal string,对应delphi的是PCHAR,而不是var x:pChar或者string 之类<br>调用方式是stdcall
 
不知你查看的方法是否正确:<br>pchar是一个指针,所以只看BMsg的地址是不能表明它的实际内存没有被破坏。<br>如果BMsg的地址是$12FBDC,那么你就应该查看$12FBDC开始的4个字节的内存是否有变化。
 
To Pipi,那么,楼主的申明就没有什么不正确了。
 
同意远帆的观点,楼主在你说的那几个地方几种方式应该都试了,还是出错。。。
 
實在不行的話就用Array[0..50]of char看看,還不行就用指針指向這樣聲明的變量
 
vb中没有byval的long,对应delphi的 var x:longint
 
顶部