gSoap生成的客户端结构初始化及使用
gSoap 生成的客户端结构初始化和使用(使用 ANSI C 绑定)
在通读 gSoap 示例和文档后,我无法找到任何直接回答此问题的内容。我已经解决了。这篇文章/答案对列出了问题和我的解决方案。
问题描述:
我正在使用 gSoap 生成的客户端源代码来构建 ANSI C 绑定来访问 Web 服务。论据 4 &作为应用程序接口提供的 5 个“soap_call__”函数(在 soapClient.c
中定义)通常生成为复杂(嵌套)结构。由于 struct ns3__send(第四个参数)是输入结构,因此必须在调用应用程序中声明、初始化、分配和释放它。
例如,给定以下 gSoa
p 生成的原型:
SOAP_FMAC5 int SOAP_FMAC6 soap_call___ns1__SendFile((struct soap *soap, const char *soap_endpoint, const char *soap_action, struct ns3__send *mtdf, struct recv *response)
在 soapStub.h
中定义以下结构定义(仅查看参数 4)
注意:我已缩短名称并从结构的原始内容中减少成员数量以简化。
struct ns3__send
{
char *wsStDate; /* optional element of type xsd:date */
int *wsStDuration; /* optional element of type xsd:int */
int *wsStFailures; /* optional element of type xsd:int */
char *wsStFileName; /* optional element of type xsd:string */
struct ns3__Param *details; /* optional element of type ns3:Param */
};
struct ns3__Param
{
int __sizeRow; /* sequence of elements <wsStdDetailsRow> */
struct ns3__Row *row; /* optional element of type ns3:xxmtdfws_wsStdDetailsRow */
};
struct ns3__Row
{
int *wsStdSeq; /* optional element of type xsd:int */
char *wsStdStep; /* optional element of type xsd:string */
char *wsStdTestDesc; /* optional element of type xsd:string */
char *wsStdLowLim; /* optional element of type xsd:string */
};
问题:
这个复杂(嵌套)输入结构中的成员和指针如何正确初始化、分配内存、分配值和释放内存,以便它们可以在调用应用程序中使用?
gSoap generated client-side structure initialization and use (using ANSI C bindings)
After reading through gSoap
examples and documentation I was not able to find anything directly answering this issue. I have since sorted it out. This post/answer pair lays out the problem and my solution.
Problem description:
I am using gSoap generated client source code to build ANSI C bindings to access web services. Arguments 4 & 5 of the "soap_call__" functions provided as application interfaces (defined in soapClient.c
) are often generated as complex (nested) structures. Because struct ns3__send
(4th argument) is the input structure, it must be declared, initialized, allocated and freed within the calling application.
for example, given the following gSoa
p generated prototype:
SOAP_FMAC5 int SOAP_FMAC6 soap_call___ns1__SendFile((struct soap *soap, const char *soap_endpoint, const char *soap_action, struct ns3__send *mtdf, struct recv *response)
with the following structure definition (looking only at argument 4) defined in soapStub.h
NOTE: I have shortened the names and reduced the number of members from original contents of the structures to simplify.
struct ns3__send
{
char *wsStDate; /* optional element of type xsd:date */
int *wsStDuration; /* optional element of type xsd:int */
int *wsStFailures; /* optional element of type xsd:int */
char *wsStFileName; /* optional element of type xsd:string */
struct ns3__Param *details; /* optional element of type ns3:Param */
};
struct ns3__Param
{
int __sizeRow; /* sequence of elements <wsStdDetailsRow> */
struct ns3__Row *row; /* optional element of type ns3:xxmtdfws_wsStdDetailsRow */
};
struct ns3__Row
{
int *wsStdSeq; /* optional element of type xsd:int */
char *wsStdStep; /* optional element of type xsd:string */
char *wsStdTestDesc; /* optional element of type xsd:string */
char *wsStdLowLim; /* optional element of type xsd:string */
};
Question:
How are the members and pointers within this complex (nested) input structure properly initialized, memory allocated, values assigned and memory freed such that they are useable within a calling application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面描述了一种 ANSI C 方法,用于初始化、分配、指派和释放嵌套结构构造中的成员和指针,其中嵌套部分中的字段数量在运行时之前未知。
为了解释需要这种处理的结构的形状,数据模式由已知数量的标头字段组成,每个字段每行都有一个值。最后一个字段(行)用作 header 和 data 部分之间的分隔符
********
。数据部分包含未知数量的数据记录,但每条记录都包含已知(且恒定)数量的逗号分隔字段:示例数据:
下面显示的两个文件已完整注释。它们将一起使用任何 ANSI C 编译器进行编译和构建:
InitComplexStructs.h:
InitComplexStructs.c
The following describes an ANSI C method for initializing, allocating, assigning and freeing members and pointers within a nested structure construct where the number of fields in the nested section is unknown until run-time.
To explain the shape of structs requiring this treatment, The data schema is comprised of a known number of header fields, each field having one value per row. The last field (row) serves as a delimiter
********
between the header and data sections. The data section contains an unknown number of data records, but each record contains a known (and constant) number of comma delimited fields:Example data:
The two files shown below are fully commented. Together, they will compile and build with any ANSI C compiler:
InitComplexStructs.h:
InitComplexStructs.c
您是否知道 gSOAP 会为您的数据结构分配内存,这样您就不必这样做?您只需为请求结构中的任何数据分配内存,而不必为回复结构分配内存。
Are you aware that gSOAP allocates memory for your data structures for you so that you don't have to do this? You only have to allocate memory for any data in a Request structure, but never the a reply structure.