有关自定义结构体我一直没有弄的很清楚。例如结构体定义用如下的形式:
typedef
[ uuid(E6F62D77-F3AF-42aa-8C04-C8F7F0FAC85D), version(1.0), helpstring("A Demo struct ")]
struct structSA
{
//CHAR[10] achStr;
[helpstring("CHAR array")] VARIANT pSA;
[helpstring("Bstr")] BSTR bstrTest;
[helpstring("INT the variable")] long lNum;
} structSA;而我用下面的方式也可以通过:
typedef struct structSA
{
VARIANT pSA;
BSTR bstrTest;
long lNum;
} structSA;
能否给解释一下这两种表示方式有什么不同,会造成什么样的后果。
另外如果我开发了两个独立的组件,在这两个组件中需要使用同一个结构体,那么在我的客户端调用组件的时候会出现重复定义的错误,这种情况下该怎么解决呢?
typedef
[ uuid(E6F62D77-F3AF-42aa-8C04-C8F7F0FAC85D), version(1.0), helpstring("A Demo struct ")]
struct structSA
{
//CHAR[10] achStr;
[helpstring("CHAR array")] VARIANT pSA;
[helpstring("Bstr")] BSTR bstrTest;
[helpstring("INT the variable")] long lNum;
} structSA;而我用下面的方式也可以通过:
typedef struct structSA
{
VARIANT pSA;
BSTR bstrTest;
long lNum;
} structSA;
能否给解释一下这两种表示方式有什么不同,会造成什么样的后果。
另外如果我开发了两个独立的组件,在这两个组件中需要使用同一个结构体,那么在我的客户端调用组件的时候会出现重复定义的错误,这种情况下该怎么解决呢?
如上, 使用三个参数就好了。
用VB什么的可以轻松使用。
第二个问题,你可以把struct定义单独放在一个idl文件里,单独编译
有关第二个问题,能不能详细点?如果单独编译,一个组件里不就得包含两个idl文件了吗?
前后两种方式的差别就是uuid.作为一个struct or enum or union,你可以
给它们一个uuid,前提是你是否需要.如果你觉得它们很重要,在其它环境下需要
通过uuid来获取,你就加上,相反,就不用理会.
第二个问题:
paul2002(Now 2003!) 兄弟说的好,你可以单独写个idl,写入struct,编译,再嵌入
.h文件. 还有种方式就用 #define 宏 #ifndef .... ok.
关于第二个问题,能不能step by step给举例说明一下?
bow!
import "oaidl.idl";
import "ocidl.idl";
#include "struct1.h"
[
object,
uuid(1BE92708-69D4-4E8F-8E55-F165484E7E36),
helpstring("IA Interface"),
pointer_default(unique)
]
interface IA : IUnknown
{
[helpstring("method SetParam")] HRESULT SetParam([in]MYSTRUCT *pParam);
[helpstring("method GetParam")] HRESULT GetParam([out,retval]MYSTRUCT *pParam);
组件1:
组件1:
import "oaidl.idl";
import "ocidl.idl";
#include "struct2.h"
[
object,
uuid(1BE92708-69D4-4E8F-8E55-F165484E7E36),
helpstring("IB Interface"),
pointer_default(unique)
]
interface IB : IUnknown
{
[helpstring("method SetParam")] HRESULT SetParam([in]MYSTRUCT *pParam);
[helpstring("method GetParam")] HRESULT GetParam([out,retval]MYSTRUCT *pParam);struct1.h定义如下:
#ifndef __STRUCT1_H_
#define __STRUCT1_H_typedef struct MYSTRUCT
{
float x;
float y;
float z;
}MYSTRCUT;
#endif
struct2.h定义如下:
#ifndef __STRUCT2_H_
#define __STRUCT2_H_
typedef struct MYSTRUCT
{
float x;
float y;
float z;
}MYSTRCUT;
#endif
然后在客户端两个组件都需要运行。另外再问个比较弱的问题,如何单独生成idl文件,我都是用ATL自动生成的。
换成: import "myStruct.idl";
可用普通的文本编辑器(notepad 也成),编写如下,扩展名为idl.
//===== myStruct.idl ========
import "oaidl.idl";
import "ocidl.idl";
typedef struct MYSTRUCT
{
float x;
float y;
float z;
}MYSTRCUT;
//===== myStruct.idl End =====
然后,你需要使用midl.exe 工具,ms提供的.
在ms-dos模式下,输入: midl myDriver:/myPath/myStruct.idl
如此,会生成一个myStruct.h 文件.
然后,你把.idl 和 .h 靠到你的两个工程中去, ok了.
"oaidl.idl"和"ocidl.idl"起到什么作用?
输入: midl myDriver:/myPath/myStruct.idl后出现
can not open input file oaidl.idl的错误。怎么办呢?