vc 中代码:
结构体定义:
typedef struct tagKCBPCONNINFO
{
char szUserName[KC_USERINFO_LEN];
char szPassword[KC_USERINFO_LEN];
...
int iTimeOut;
}KCBPCONNINFO,*LPKCBPCONNINFO;需调用函数声明(该函数为dll中的) int KC_CALLING InitKCBP(KCBPCONNINFO *lpstKCBPConninfo)delphi中定义
type
TKCBPConnInfo =^tagKCBPConnInfo;
tagKCBPConnInfo = record
szUserName:string;
szPassword:string;
...
iTimeOut:integer;
end;
调用dll函数声明为:
function InitKCBP(pk:TKCBPConnInfo):integer;stdcall ;external KDlib name '_InitKCBP@4';
我现在调用该delphi 函数为:
var
KCBPConnInfo: tagKCBPConnInfo;
begin
KCBPConnInfo.szUserName:='test';
KCBPConnInfo.szPassword:='test2';
...
InitKCBP(@KCBPConnInfo);
end; 程序虽然没报错,但返回结果根本不对。。
应该怎样改写呢?由衷感谢您的帮助。
结构体定义:
typedef struct tagKCBPCONNINFO
{
char szUserName[KC_USERINFO_LEN];
char szPassword[KC_USERINFO_LEN];
...
int iTimeOut;
}KCBPCONNINFO,*LPKCBPCONNINFO;需调用函数声明(该函数为dll中的) int KC_CALLING InitKCBP(KCBPCONNINFO *lpstKCBPConninfo)delphi中定义
type
TKCBPConnInfo =^tagKCBPConnInfo;
tagKCBPConnInfo = record
szUserName:string;
szPassword:string;
...
iTimeOut:integer;
end;
调用dll函数声明为:
function InitKCBP(pk:TKCBPConnInfo):integer;stdcall ;external KDlib name '_InitKCBP@4';
我现在调用该delphi 函数为:
var
KCBPConnInfo: tagKCBPConnInfo;
begin
KCBPConnInfo.szUserName:='test';
KCBPConnInfo.szPassword:='test2';
...
InitKCBP(@KCBPConnInfo);
end; 程序虽然没报错,但返回结果根本不对。。
应该怎样改写呢?由衷感谢您的帮助。
1、szUserName:string; //不要用string,用char数组,大小要和vc中一致
2、tagKCBPConnInfo = record //用tagKCBPConnInfo =packed record ,但是要和vc的对齐方式一致
type
TKCBPConnInfo =^tagKCBPConnInfo;
tagKCBPConnInfo = packed record
szUserName:array[0..KC_USERINFO_LEN-1] of char;
szPassword:array[0..KC_USERINFO_LEN-1] of char;
...
iTimeOut:integer;
end;
C#允许的东西,Delphi不一定允许,改了再试试看。
type
TKCBPConnInfo =^tagKCBPConnInfo;
tagKCBPConnInfo = packed record
szUserName:array[0..KC_USERINFO_LEN-1] of char;
szPassword:array[0..KC_USERINFO_LEN-1] of char;
...
iTimeOut:integer;
end; var
KCBPConnInfo: tagKCBPConnInfo;
begin
KCBPConnInfo.szUserName:='test';
KCBPConnInfo.szPassword:='test2';
...
InitKCBP(@KCBPConnInfo);
end; 改了之后,如何附值?
procedure Move(const Source; var Dest; Count: Integer);
type
TKCBPConnInfo =^tagKCBPConnInfo;
tagKCBPConnInfo = packed record
szUserName:array[0..KC_USERINFO_LEN-1] of char;
szPassword:array[0..KC_USERINFO_LEN-1] of char;
...
iTimeOut:integer;
end; //这里加上var
function InitKCBP(var pk:TKCBPConnInfo):integer;stdcall ;external KDlib name '_InitKCBP@4'; var
KCBPConnInfo: tagKCBPConnInfo;
begin
//KCBPConnInfo.szUserName:='test';
//KCBPConnInfo.szPassword:='test2';
//字符串到数组不能直接赋值
应该用strcopy进行拷贝 ...
InitKCBP(KCBPConnInfo);
end; 改了之后,如何附值?
加上var (TKCBPConnInfo =^tagKCBPConnInfo); 会报错吧strcopy试过不行,才问的如何赋值..
我是这样调用的
var
KCBPConnInfo: tagKCBPConnInfo;
begin
strcopy(@KCBPConnInfo.szUserName,'test');
strcopy(@KCBPConnInfo.szPassword,'test2');
...
InitKCBP(@KCBPConnInfo);
end;
lpstKCBPConninfo.szPassword='est2'
什么原因?????function InitKCBP(pk:pointer):integer;stdcall ;external KDlib name '_InitKCBP@4'; var
KCBPConnInfo: tagKCBPConnInfo;
begin
strcopy(@KCBPConnInfo.szUserName,'test');
strcopy(@KCBPConnInfo.szPassword,'test2');
...
InitKCBP(pchar(@KCBPConnInfo));
end;
也是一样.