如何在COM服务器中使用用户定义类型(UDT)? [英] How can I use a User Defined Type (UDT) in a COM server?

查看:59
本文介绍了如何在COM服务器中使用用户定义类型(UDT)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个COM服务器,其方法当前返回一个整数:

I have a COM server with a method currently returning an integer:

[
    object,
    uuid("..."),
    dual,
    helpstring("IMyCOMServer Interface"),
    pointer_default(unique)
]
__interface IMyCOMServer : IDispatch
{
    [id(1), helpstring("method MyQuery")]
    HRESULT MyQuery([in] BSTR instr, [out,retval] int* outint);
};

这可以很好地编译,但是我宁愿返回一个枚举:(此代码实际上在接口定义之上)

This compiles fine, but I'd rather return an enum: (this code is actually above the interface definition)

typedef
[
    uuid("..."),
    v1_enum,
    helpstring("Enum")
]
enum {
    value_a,
    value_b,
    value_c
} MyEnum;

再一次编译就可以了,但是一旦我在接口和实现中将 int * 更改为 MyEnum * ,就会出现链接器错误:

Which again compile fine of its own right, but as soon as I change the int* to MyEnum* in the interface and implementation, I get linker errors:

[id(1), helpstring("method MyQuery")]
HRESULT MyQuery([in] BSTR instr, [out,retval] MyEnum* outint);

error MIDL2025 : syntax error : expecting a type specification near "MyEnum"

无论采用哪种方式,我都无法对其进行编译.

Whichever way I do it, I can't get it to compile.

由于 Euro Micelli ,事实证明,真正的问题是我的用户定义类型(枚举)没有将其放入生成的.IDL文件中.通过在线论坛查询来判断,这似乎是一个常见问题.

Thanks to Euro Micelli it turns out that the real problem is that my User Defined Type (the enum) wasn't making it into the generated .IDL file. Judging by forum queries online, this seems to be a common problem.

博客文章 Star Tech:UDT(用户定义类型)和COM 引导我走了正确的路.似乎在使用属性化ATL时需要解决方法.

A blog article Star Tech: UDT (User Defined Types) and COM guided me down the right path. It seems that a workaround is needed when using attributed ATL.

总而言之,我进行了以下更改:

In summary, I made the following changes:

创建的 udt.idl :

import "oaidl.idl";
import "ocidl.idl";

[
    uuid("..."),
    v1_enum,
    helpstring("Enum")
]
typedef enum MyEnum {
    value_a,
    value_b,
    value_c
} MyEnum_t;

[
    version(1.0),
    uuid(...),
    helpstring(...)
]
library MyLibrary
{
    enum MyEnum;
}

在主 .cpp 文件中的模块属性之前添加了以下行,以便将上述IDL导入到生成的文件中:

Added the following line prior to the module attribute in the main .cpp file so that the above IDL gets imported into the generated file:

[importidl("udt.idl")];

推荐答案

(这是根据实际的IDL改编而成的,因此我知道它可以工作)

(This is adapted from an actual IDL, so I know it works)

[uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX), v1_enum, helpstring("Enum")]
enum MyEnum {
    value_a,
    value_b,
    value_c
} ;

此外,在库"部分中,您还必须包括枚举,否则该枚举将不会导出到类型库中:

Plus, in your Library section you must include the enum as well, or the enum won't be exported to the type library:

library MyLib
{
    enum MyEnum;
...
};

这篇关于如何在COM服务器中使用用户定义类型(UDT)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆