c++ 到 delphi 转换
c++ to delphi convert
现在我正在尝试从我的大华终端获取图片,代码变成了一个小技巧。
在 C++ 示例代码中,我有:
//Struct
// face info
typedef struct tagNET_ACCESS_FACE_INFO
{
char szUserID[32]; // user ID
int nFaceData; // count of face data,the max number is 20
char szFaceData[20][2048]; // face data
int nFaceDataLen[20]; // face data length
int nFacePhoto; // count of face photo,max size: 5
int nInFacePhotoLen[5]; // the size of each photo used by the user
int nOutFacePhotoLen[5]; // the actual size of each photo
char* pFacePhoto[5]; // face photo data,max size: 120K
BYTE byReserved[2048]; // reserved
} NET_ACCESS_FACE_INFO;
//Part of the c++ code
//...
NET_ACCESS_FACE_INFO stuFaceInfo;
memset(&stuFaceInfo,0,sizeof(stuFaceInfo));
for (int i=0;i<5;i++)
{
stuFaceInfo.nInFacePhotoLen[i] = 100*1024;
stuFaceInfo.pFacePhoto[i] = new char[100*1024]; //<-- Don´t know how do it
memset(stuFaceInfo.pFacePhoto[i],0,100*1024);
}
stuFaceGetOut.pFaceInfo = &stuFaceInfo;
我转换成这个Delphi代码:
// face info Record
PNetAccessFaceInfo = ^TNetAccessFaceInfo;
TNetAccessFaceInfo = record
szUserID: array[0..31] of AnsiChar; // user ID
nFaceData: Integer; // count of face data,the max number is 20
szFaceData: array[0..19, 0..2047] of AnsiChar; // face data
nFaceDataLen: array[0..19] of Integer; // face data length
nFacePhoto: Integer; // count of face photo,max size: 5
nInFacePhotoLen: array[0..4] of Integer; // the size of each photo used by the user
nOutFacePhotoLen: array[0..4] of Integer; // the actual size of each photo
pFacePhoto: array[0..4] of PAnsiChar; // face photo data,max size: 120K
byReserved: array[0..2047] of Byte; // reserved
end;
...
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo,sizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
ZeroMemory(@faceInfo.pFacePhoto[i],sizeof(faceInfo.pFacePhoto[i])); //<-- this get nothing, no error code, no crash
// faceInfo.pFacePhoto[i] := pAnsichar(100*1024); //<-- tryed this but got a exception
FillChar(faceInfo.pFacePhoto[i],0,100*1024);
end;
用pFacePhoto *char[5]
我转换成pFacePhoto : Array [0..4] of PAnsiChar;
但是在示例代码中,...pfacePhoto[i] = new char[100*1024]
在我有限的 C++ 知识中意味着 PAnsiChar
接收一个 Delphi 数组 [0..(100*1024)-1] of AnsiChar
.
因为 pFacePhoto[i]
是一个 PAnsiChar
,我可以创建一个新的 AnsiChar
数组并指向它吗?
运行 我的 Delphi 代码,我从 GetLastError()
:
得到这个
#define NET_RETURN_DATA_ERROR _EC(21) // Error occurs when verify returned data.
But in the sample code, ...pfacePhoto[i] = new char[100*1024]
in my limited knowledge of C++ means the PAnsiChar
receiving a Delphi array [0..(100*1024)-1] of AnsiChar
.
Since pFacePhoto[i]
is a PAnsiChar
, can I create a new array of AnsiChar and point to it?
是的,这正是您需要做的。您需要分配一个包含 100*1024
个 AnsiChar
个元素的数组,然后将 pFacePhoto[i]
指向该数组的第一个元素。
有不同的方法可以做到这一点,例如使用 GetMem()
:
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo, SizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
GetMem(faceInfo.pFacePhoto[i], 100*1024);
ZeroMemory(faceInfo.pFacePhoto[i], 100*1024);
end;
...
for I := 0 to 4 do
begin
FreeMem(faceInfo.pFacePhoto[i]);
end;
或者,使用 AnsiStrings.AnsiStrAlloc()
:
uses
..., AnsiStrings;
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo, SizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
faceInfo.pFacePhoto[i] := AnsiStrAlloc(100*1024);
ZeroMemory(faceInfo.pFacePhoto[i], 100*1024);
end;
...
for I := 0 to 4 do
begin
StrDispose(faceInfo.pFacePhoto[i]);
end;
现在我正在尝试从我的大华终端获取图片,代码变成了一个小技巧。
在 C++ 示例代码中,我有:
//Struct
// face info
typedef struct tagNET_ACCESS_FACE_INFO
{
char szUserID[32]; // user ID
int nFaceData; // count of face data,the max number is 20
char szFaceData[20][2048]; // face data
int nFaceDataLen[20]; // face data length
int nFacePhoto; // count of face photo,max size: 5
int nInFacePhotoLen[5]; // the size of each photo used by the user
int nOutFacePhotoLen[5]; // the actual size of each photo
char* pFacePhoto[5]; // face photo data,max size: 120K
BYTE byReserved[2048]; // reserved
} NET_ACCESS_FACE_INFO;
//Part of the c++ code
//...
NET_ACCESS_FACE_INFO stuFaceInfo;
memset(&stuFaceInfo,0,sizeof(stuFaceInfo));
for (int i=0;i<5;i++)
{
stuFaceInfo.nInFacePhotoLen[i] = 100*1024;
stuFaceInfo.pFacePhoto[i] = new char[100*1024]; //<-- Don´t know how do it
memset(stuFaceInfo.pFacePhoto[i],0,100*1024);
}
stuFaceGetOut.pFaceInfo = &stuFaceInfo;
我转换成这个Delphi代码:
// face info Record
PNetAccessFaceInfo = ^TNetAccessFaceInfo;
TNetAccessFaceInfo = record
szUserID: array[0..31] of AnsiChar; // user ID
nFaceData: Integer; // count of face data,the max number is 20
szFaceData: array[0..19, 0..2047] of AnsiChar; // face data
nFaceDataLen: array[0..19] of Integer; // face data length
nFacePhoto: Integer; // count of face photo,max size: 5
nInFacePhotoLen: array[0..4] of Integer; // the size of each photo used by the user
nOutFacePhotoLen: array[0..4] of Integer; // the actual size of each photo
pFacePhoto: array[0..4] of PAnsiChar; // face photo data,max size: 120K
byReserved: array[0..2047] of Byte; // reserved
end;
...
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo,sizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
ZeroMemory(@faceInfo.pFacePhoto[i],sizeof(faceInfo.pFacePhoto[i])); //<-- this get nothing, no error code, no crash
// faceInfo.pFacePhoto[i] := pAnsichar(100*1024); //<-- tryed this but got a exception
FillChar(faceInfo.pFacePhoto[i],0,100*1024);
end;
用pFacePhoto *char[5]
我转换成pFacePhoto : Array [0..4] of PAnsiChar;
但是在示例代码中,...pfacePhoto[i] = new char[100*1024]
在我有限的 C++ 知识中意味着 PAnsiChar
接收一个 Delphi 数组 [0..(100*1024)-1] of AnsiChar
.
因为 pFacePhoto[i]
是一个 PAnsiChar
,我可以创建一个新的 AnsiChar
数组并指向它吗?
运行 我的 Delphi 代码,我从 GetLastError()
:
#define NET_RETURN_DATA_ERROR _EC(21) // Error occurs when verify returned data.
But in the sample code,
...pfacePhoto[i] = new char[100*1024]
in my limited knowledge of C++ means thePAnsiChar
receiving a Delphi array[0..(100*1024)-1] of AnsiChar
.Since
pFacePhoto[i]
is aPAnsiChar
, can I create a new array of AnsiChar and point to it?
是的,这正是您需要做的。您需要分配一个包含 100*1024
个 AnsiChar
个元素的数组,然后将 pFacePhoto[i]
指向该数组的第一个元素。
有不同的方法可以做到这一点,例如使用 GetMem()
:
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo, SizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
GetMem(faceInfo.pFacePhoto[i], 100*1024);
ZeroMemory(faceInfo.pFacePhoto[i], 100*1024);
end;
...
for I := 0 to 4 do
begin
FreeMem(faceInfo.pFacePhoto[i]);
end;
或者,使用 AnsiStrings.AnsiStrAlloc()
:
uses
..., AnsiStrings;
var
faceInfo: TNetAccessFaceInfo;
begin
...
ZeroMemory(@faceInfo, SizeOf(faceInfo));
for I := 0 to 4 do
begin
faceInfo.nInFacePhotoLen[i] := 100*1024;
faceInfo.pFacePhoto[i] := AnsiStrAlloc(100*1024);
ZeroMemory(faceInfo.pFacePhoto[i], 100*1024);
end;
...
for I := 0 to 4 do
begin
StrDispose(faceInfo.pFacePhoto[i]);
end;