高手请进,C类型转Delphi类型 本帖最后由 zshsuming 于 2009-12-10 09:13:36 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 k:=SSSendBuffer(0,@StrParam,PChar(Memo1.Lines.Text),false); StrParam变量分配空间了吗?应该用Create之类的函数实例对象吧期待高手指点! 俺试了一下,应该这样使用:var StrParam: PFileParam;New(StrParam);k:=SSSendBuffer(0,@StrParam,PChar(Memo1.Lines.Text),false); Dispose(StrParam); StrParam:TFileParam; 这是一个栈内存,本身就已经分配了一个结构体的大小,还需要如何分配内存? 分配了,StrParam:TFileParam; 这样后编译器会自己帮你分配StrParam的内存(如果StrParam:PFileParam;这样是指针才需要自己用new分配).你的代码看起来没错,你可以试一下先初始化一下StrParam,如下: Fillchar(StrParam,sizeof(TFileParam),#0);//初始化 StrParam.effect:=8; StrParam.Font:=16; StrParam.ShowTitle:=0; StrParam.TitleColor:=2; StrParam.WordColor:=0; k:=SSSendBuffer(0,@StrParam,PChar(Memo1.Lines.Text),false); //。 如果还不行在结构声明添加packed关键字试一下,如下:FileParam=packed record effect:Byte; level:Byte; Speed:Byte; Delay:Byte; Font:Byte; ShowTitle:Byte; TitleColor:Byte; WordColor:Byte; end; 整个结构体就刚好是8个字节,应该不会有程序刻意采用16byte对齐吧? 不清楚楼主所使用的是什么版本的Delphi,建议将函数定义由:TSSSendBuffer=function(Index:Integer;Param:PFileParam;StrBuffer:PChar;Flag:Boolean):Integer;stdcall;修改为:TSSSendBuffer=function(Index:Integer;Param:PFileParam;StrBuffer:PAnsiChar;Flag:Boolean):Integer;stdcall; 不好意思,我用的是D2007,XP平台 唯一有可能有问题的地方就是 bool 的声明c 标准里是没有 bool 类型的,c++ 里才有。因为我不懂 cpp,所以不知道 bool 是1字节还是4字节。但在 c 中 BOOL 一般是定义成和 int 一样宽的,32位平台中也就是4字节。而 delphi 中的 Boolean 是1字节,如果 cpp 中的 bool 也是1字节的话那什么问题都没有,否则在传参数之前不会先 movzx eax, al,而是直接 push eax,这样就很可能导致原本为 True 的参数实际上传成了 FALSE 这样子看唯一可能有差异的就是C++里面的函数定义:extern "C" __declspec(dllexport) int __stdcall SS_Send_Buffer(int W_index,TFileParam *param,const char *Buffer,bool IsSave); 会不会是extern "C" __declspec(dllexport) int __stdcall SS_Send_Buffer(int W_index,TFileParam *param,const char *Buffer,BOOL IsSave); 如果是后者,那就将Delphi当中的定义修改为:TSSSendBuffer=function(Index:Integer;Param:PFileParam;const StrBuffer:PAnsiChar;Flag:LongBool):Integer;stdcall; 刚才不小心说反了,True 不会变成 FALSE,而是 False 很可能变成非0…… 关于form前后显示的问题 如果在网页上判断电脑上是否安装了我的软件 致谢 虚拟网络打印机(答者有分,好方案送300----分次) BCD码,分不够再加 snailking领分,解决问题加分 加分 TADOQuery型的控件,其中有个属性connection,这个属性是做什么用的,该怎么用,我看过帮助了,没看懂:( BDE连接SQL的问题? 这么简单的SQL为何会出现error creating cursor handle? 有没有检测本机网卡流量的控件或者资料 请教各位做的系统如何解决偏僻字的输入问题
StrParam变量分配空间了吗?
应该用Create之类的函数实例对象吧
期待高手指点!
StrParam: PFileParam;New(StrParam);
k:=SSSendBuffer(0,@StrParam,PChar(Memo1.Lines.Text),false);
Dispose(StrParam);
这是一个栈内存,本身就已经分配了一个结构体的大小,还需要如何分配内存?
这样后编译器会自己帮你分配StrParam的内存(如果StrParam:PFileParam;这样是指针才需要自己用new分配).
你的代码看起来没错,你可以试一下先初始化一下StrParam,如下:
Fillchar(StrParam,sizeof(TFileParam),#0);//初始化
StrParam.effect:=8;
StrParam.Font:=16;
StrParam.ShowTitle:=0;
StrParam.TitleColor:=2;
StrParam.WordColor:=0; k:=SSSendBuffer(0,@StrParam,PChar(Memo1.Lines.Text),false);
//。
如果还不行在结构声明添加packed关键字试一下,如下:FileParam=packed record
effect:Byte;
level:Byte;
Speed:Byte;
Delay:Byte;
Font:Byte;
ShowTitle:Byte;
TitleColor:Byte;
WordColor:Byte;
end;
TSSSendBuffer=function(Index:Integer;Param:PFileParam;StrBuffer:PChar;Flag:Boolean):Integer;stdcall;
修改为:TSSSendBuffer=function(Index:Integer;Param:PFileParam;StrBuffer:PAnsiChar;Flag:Boolean):Integer;stdcall;
c 标准里是没有 bool 类型的,c++ 里才有。因为我不懂 cpp,所以不知道 bool 是1字节还是4字节。但在 c 中 BOOL 一般是定义成和 int 一样宽的,32位平台中也就是4字节。
而 delphi 中的 Boolean 是1字节,如果 cpp 中的 bool 也是1字节的话那什么问题都没有,否则在传参数之前不会先 movzx eax, al,而是直接 push eax,这样就很可能导致原本为 True 的参数实际上传成了 FALSE
extern "C" __declspec(dllexport) int __stdcall SS_Send_Buffer(int W_index,TFileParam *param,const char *Buffer,bool IsSave);
会不会是
extern "C" __declspec(dllexport) int __stdcall SS_Send_Buffer(int W_index,TFileParam *param,const char *Buffer,BOOL IsSave);
如果是后者,那就将Delphi当中的定义修改为:TSSSendBuffer=function(Index:Integer;Param:PFileParam;const StrBuffer:PAnsiChar;Flag:LongBool):Integer;stdcall;