delphi与C++ dll 之间的数据传输(字符串,数组)

1、delphi向C++ dll中传入字符串

delphi中:

TsavePicture= procedure(filename:pansiChar); cdecl;

name:PAnsiChar;

name:=PAnsiChar(AnsiString('test.jpg'));

C++中:

MYDLL void GetImage(char* filename);

string str = filename;


2、delphi与C++ dll之间互传数组

delphi代码部分


TNewMcsPxl = function(McsList: pcvPoint2f; McsCount: Integer;
                        PxlList: pcvPoint2f; PxlCount: Integer;
                           NewMcsList: pcvPoint2f;
                           NewPxlList: pcvPoint2f): Boolean; cdecl;

 mcsppoint,pixppoint,newmcsppoint,newpixppoint:pcvPoint2f;//申明数据类型
如何调用
//将 PixList中内容放入指针中
  GetMem(mcsppoint,SizeOf(cPoint)*McsList.Count);
  GetMem(pixppoint,SizeOf(cPoint)*PixList.Count);
  GetMem(newpixppoint,SizeOf(cPoint)*4);
  GetMem(newmcsppoint,SizeOf(cPoint)*4);
  //ShowMessage(IntToStr(SizeOf(cPoint)));
  for idx := 0 to McsList.Count - 1 do
  begin
    mcsppoint[idx]:= McsList[idx];
    pixppoint[idx]:= PixList[idx];
  end;
//调用函数
  vision_NewMcsPxl(mcsppoint,McsList.Count,pixppoint,McsList.Count,newmcsppoint,newpixppoint);
//调用函数,newmcsppoint,newpixppoint所指向的内存已经被dll中函数写入point2f数据
//释放内存
 FreeMem(mcsppoint);
  FreeMem(pixppoint);
  FreeMem(newpixppoint);
  FreeMem(newmcsppoint);

C++代码部分

MYDLL  bool GetPerspectiveMatrix(Point2f* arrMcs, int cntMcs, Point2f* arrPxl, int cntPxl, Point2f* newMcs, Point2f* newPxl)
{
vector quad_pts;//机床坐标
for (int i = 0; i < cntMcs; i++)
{
quad_pts.push_back(arrMcs[i]);
}
vector corners;//图像坐标
for (int i = 0; i < cntPxl; i++)
{
corners.push_back(arrPxl[i]);
}
transmtx = findHomography(quad_pts, corners, 0, 3.0);//从机床到图像
quad_pts.clear();
corners.clear();

quad_pts.push_back(Point2f(0, 0));
quad_pts.push_back(Point2f(-1450, 0));
quad_pts.push_back(Point2f(-1450, 2900));
quad_pts.push_back(Point2f(0, 2900));
perspectiveTransform(quad_pts, corners, transmtx);
for (int i = 0; i < 4; i++)
{
newMcs[i] = quad_pts[i];
newPxl[i] = corners[i];
}
return true;
}

注意两点:1、point2f和pcvpoint2f在delphi和C++中占据同样的内存大小  2、需事先确定好指针指向内容的长度


3、delphi向C++ dll传递int型数组

话不多说,直接上代码

delphi部分

定义接口函数

type
  Ttest = function(p:PInteger;count:Integer): Boolean; cdecl;
var
  vision_test:Ttest;
函数部分

procedure TForm1.btn1Click(Sender: TObject);
var
 idx,cnt:Integer;
 teee:array [0..255] of Integer;
begin
  cnt:=255;
  for idx := 0 to cnt - 1 do
  begin
    teee[idx]:=200;
  end;
  vision_test(@teee,cnt);//传入了 数组第一个元素的地址

  for idx := 0 to cnt - 1 do
  begin
    mmo1.Lines.Add(IntToStr(teee[idx]));
  end;

end;
C++部分代

MYDLL void test(int* tem,int count)
{
	for (int i = 0; i < count; i++)
	{
		int temint = tem[i]-5;
		tem[i] = temint;
	}
}





你可能感兴趣的:(C++,delphi)