描述:
我用mfc appwizard(dll)作了一个Automation的DLL,如何在该DLL中定义一枚举或其他的如结构体类型
最后生成的DLL,在VB或其他开发工具引用时,可使用这些类型?如:在VB中可直接定义:
dim a as DLL工程.指定的枚举类型.比如说类型如下:
enum ENUM_CXIMAGE_FORMATS{
CXIMAGE_FORMAT_UNKNOWN,
CXIMAGE_FORMAT_BMP
};
本人菜鸟,大家别笑啊,多谢帮忙!
解决方案1:
在IDL定义枚举类型:
typedef
[
uuid(F1A3AFB8-EC53-4bc9-A0D7-5F91D65BCACB) , version(1.0),
helpstring("User Infomation enum Type")
]
enum tagImageFMT
{
[helpstring("TIF")] IF_TIFF=0,
[helpstring("BMP")] IF_BMP=1,
}ImageFMT;
MIDL编译器能将之生成头文件,有两种方法:
1、命令行: midl /Oicf /h "NonATLObject.h" /iid "NonATLObject_i.c"
2、工程设置中选中IDL/ODL文件-〉MIDL标签页-〉output header filename,输入头文件名称即可。
然后哪里要用,就将其Include进去即可。
导出类,在你的导出类里面建立一个你定义的Enum变量
如:
enum yourCustomEnum{
yourCustomEnumFirst = 0,
yourCustomEnumSecond = 1,
};
class AFX_EXT_CLASS yourCustom
{
public:
yourCustomEnum yCusEnum;
yourCustom();
virtual ~yourCustom();
};
不修改idl的话,导出2个只读属性,名字叫CXIMAGE_FORMAT_UNKNOWN、CXIMAGE_FORMAT_BMP,类型为long
解决方案4: 我没做过MFC DLL
在ATL下的做法
1.在idl文件中定义
typedef
[
uuid(5FD2DC77-2F65-44C1-AD21-AF384D26342B) , //这个uuid可以用vc的GUIDGEN.EXE工具生成
version(1.0),
helpstring("User Infomation struct Type")
]
struct MY
{
[helpstring("Name")] BSTR bstrName;
}MY;
2.stdafx.h中定义
const IID USERINFO_IID = { 0x5FD2DC77, 0x2F65,0x44C1,{
0xad,
0x21,
0xAF,
0x38,
0x4d,
0x26,
0x34,
0x2b
}
注意名字和uuid要一样
好久没做这块了,说得不对,还望见谅。
在MSDN中就有相关介绍(搜索关键字IDL):
enum
enum [tag ] { identifier [=integer-value ] [ , ... ] }
tag
Specifies an optional tag for the enumerated type.
identifier
Specifies the particular enumeration.
integer-value
Specifies a constant integer value.
Examples
typedef enum {M />
typedef enum {Clemens=21, Palmer=22, Ryan=34} pitchers;
Remarks
The keyword enum is used to identify an enumerated type. Enum types can appear as type specifiers in typedef declarations, general declarations, and function declarators (either as the function-return-type or as a parameter-type specifier). For the context in which type specifiers appear, see IDL.
In the MIDL compiler's default mode, you can assign integer values to enumerators. (This feature is not available when you compile with the /osf switch.) As with C-language enumerators, enumerator names must be unique, but the enumerator values need not be.
When assignment operators are not provided, identifiers are mapped to consecutive integers from left to right, starting with zero. When assignment operators are provided, assigned values start from the most recently assigned value.
The maximum number of identifiers is 65,535.
Objects of type enum are int types, and their size is system-dependent. By default, objects of enum types are treated as 16-bit objects of type unsigned short when transmitted over a network. Values outside the range 0 - 32,767 cause the run-time exception RPC_X_ENUM_VALUE_OUT_OF_RANGE. To transmit objects as 32-bit entities, apply the v1_enum attribute to the enum typedef.
用idl来定义枚举
外部是可以看到的
最好声明成OleVariant类型的变量,接口通用
解决方案8: idl是支持枚举类型的
这些类型应该符合C语言的规定。