读书人

CopyMemory无法复制常量指针?解决方案

发布时间: 2012-04-11 17:42:33 作者: rapoo

CopyMemory无法复制常量指针?

const
CONST_NAME = 'TEST';

CopyMemory(@SendBuf, @CONST_NAME, strLen(CONST_NAME));

//出错求解释

[解决办法]
CONST_NAME应该是一个字符串
应该这样
@CONST_NAME[1]
[解决办法]
可以复制的,请看下面的示例,@CONST_NAME[1]下标从1开始

Delphi(Pascal) code
const  CONST_NAME = 'TEST';var  SendBuf: array of Byte;  i: Integer;begin  SetLength(SendBuf,4);  CopyMemory(@SendBuf[0], @CONST_NAME[1], strLen(CONST_NAME));  for i := Low(SendBuf) to High(SendBuf) do    ShowMessage(Char(SendBuf[i]));end;
[解决办法]
const
CONST_NAME = 'TEST';

CopyMemory(@SendBuf, PChar(CONST_NAME), SizeOf(char)*strLen(CONST_NAME));

读书人网 >.NET

热点推荐