+ -
当前位置:首页 → 问答吧 → delphi调用C动态库问题

delphi调用C动态库问题

时间:2011-09-07

来源:互联网

C动态库中定义了一个结构体如下:
结构体
typedef struct st_itemselect_
{
  WORD wID; //编号
  VOID *pValue; //编号所对应的数据缓冲区
  WORD wValueLen; //数据缓冲区大小
}ITEMSELECT,*P_ITEMSELECT;

调用的函数声明如下:
DWORD GetItem(void *pvSelect,int nItem);
/*
  pvSelect: ST_ITEMSELECT * 数组模板
  nItem : 需要读取的ST_ITEMSELECT个数。
*/
此函数的功能是通过结构体中传入的编号参数,获取到对应的数据缓冲区值。


我现在用DELPHI去调用执行,先定义对应的结构体如下:
type
  PItemSelect = ^TItemSelect;
  TItemSelect = packed record
  wID: WORD;
  pValue: pointer;
  wValueLen: WORD;
  end;
定义的调用函数如下:
function GetItem(pvSelect:pointer;nItem:integer):DWORD;stdcall;
程序实现如下:
var
  st_itemselect:array of PItemSelect; //定义结构体对象数组
begin
  SetLength(st_itemselect,1); //设定数组上限为1
  GetMem(st_itemselect[0],Sizeof(TItemSelect)); //分配数组内存

  st_itemselect[0].wID:=1; //设置传入参数
  t_itemselect[0].pValue:=GetMemory(18); //分配返回缓冲区
  st_itemselect[0].wValueLen:=18;

  GetItem(st_itemselect,1); //调用DLL函数

  showmessage(pChar(st_itemselect[0].pValue)); //显示返回缓冲区结果

  FreeMemory(st_itemselect[0].pValue); //释放缓冲区内存
  FreeMem(st_itemselect[0]); //释放数组内存
  st_itemselect:=nil;
end;

然而程序运行后获取的结果是乱码,并不是正确的数据,请问是上述过程中那个环节我弄错了,谢谢。

作者: miaoyuli   发布时间: 2011-09-07

function GetItem(pvSelect:pointer;nItem:integer):DWORD;cdecl;

作者: yushf   发布时间: 2011-09-07

热门下载

更多