#import的rename使用
Author:zfive5(zidong)
Email :zfive5@yahoo.com.cn
由于工作的原因,需要编写一个vc的程序去调用vb编写的组件,本以为简单的很,但由于对方提供的组件里出现了类似下面的代码:
Public Function GetA() As String
GetA = "hello wordl GetA"
End Function
Public Property Get A() As String
A = "hello world A"
End Property
这让我冥思苦想半个多小时,因为在vc里如果用API方式调用组件的话,写上一大堆代码才能完成,而vb代码就如下:
Dim o1 as Object
set o1=CreateObject("ZFive5.ZClass")
o1.GetA()
所以决定用#import生成一个导出类,这样以来好多东西都不需要写了,但又出现如下的编译错误:
--------------------Configuration: test - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
test.cpp
testDlg.cpp
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tlh(48) : error C2535: 'class _bstr_t __thiscall ZFive5::_ZClass::GetA(void)' : member function already defined or declared
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tlh(47) : see declaration of 'GetA'
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tli(21) : error C2084: function 'class _bstr_t __thiscall ZFive5::_ZClass::GetA(void)' already has a body
Generating Code...
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)
原因就是A属性的Get方法与GetA方法重名了,本来想看看是否能删掉用不到的调用声明,这样一来就可以规避重名的问题,但每次编译的时间又再一次的出现,看来这种方法不是最好的方法,此时想起了rename属性 (当然这也是ADO在#import时rename给我留有的印象) ,在vc的代码上加上如下代码:
#import "c:/zfive5.dll" rename("A","B")
最终相应的调用代码:
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
::CoInitialize(NULL);
{
_ZClassPtr ptr;
ptr.CreateInstance(L"ZFive5.ZClass");
_bstr_t strH=ptr->GetA();//GetA方法 ,而得到属性为:_bstr_t strH=ptr->GetB();
AfxMessageBox(strH);
}
::CoUninitialize();
}
就这个rename使我开始了#import 的所有属性的研究:
The #import attributes are listed below:
auto_rename
auto_search
embedded_idl
exclude
high_method_prefix
high_property_prefixes
implementation_only
include()
inject_statement
named_guids
no_auto_exclude
no_dual_interfaces
no_implementation
no_namespace
no_search_namespace
no_smart_pointers
raw_dispinterfaces
raw_interfaces_only
raw_method_prefix
raw_native_types
raw_property_prefixes
rename
rename_namespace
rename_search_namespace
tlbid
关于这些属性,详细的可以参见
http://msdn2.microsoft.com/en-us/library/8etzzkb6(VS.71).aspx#_predir_the_23import_directive_import_attributes
Author:zfive5(zidong)
Email :zfive5@yahoo.com.cn
由于工作的原因,需要编写一个vc的程序去调用vb编写的组件,本以为简单的很,但由于对方提供的组件里出现了类似下面的代码:
Public Function GetA() As String
GetA = "hello wordl GetA"
End Function
Public Property Get A() As String
A = "hello world A"
End Property
这让我冥思苦想半个多小时,因为在vc里如果用API方式调用组件的话,写上一大堆代码才能完成,而vb代码就如下:
Dim o1 as Object
set o1=CreateObject("ZFive5.ZClass")
o1.GetA()
所以决定用#import生成一个导出类,这样以来好多东西都不需要写了,但又出现如下的编译错误:
--------------------Configuration: test - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
test.cpp
testDlg.cpp
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tlh(48) : error C2535: 'class _bstr_t __thiscall ZFive5::_ZClass::GetA(void)' : member function already defined or declared
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tlh(47) : see declaration of 'GetA'
c:/program files/microsoft visual studio/myprojects/test/debug/zfive5.tli(21) : error C2084: function 'class _bstr_t __thiscall ZFive5::_ZClass::GetA(void)' already has a body
Generating Code...
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)
原因就是A属性的Get方法与GetA方法重名了,本来想看看是否能删掉用不到的调用声明,这样一来就可以规避重名的问题,但每次编译的时间又再一次的出现,看来这种方法不是最好的方法,此时想起了rename属性 (当然这也是ADO在#import时rename给我留有的印象) ,在vc的代码上加上如下代码:
#import "c:/zfive5.dll" rename("A","B")
最终相应的调用代码:
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
::CoInitialize(NULL);
{
_ZClassPtr ptr;
ptr.CreateInstance(L"ZFive5.ZClass");
_bstr_t strH=ptr->GetA();//GetA方法 ,而得到属性为:_bstr_t strH=ptr->GetB();
AfxMessageBox(strH);
}
::CoUninitialize();
}
就这个rename使我开始了#import 的所有属性的研究:
The #import attributes are listed below:
auto_rename
auto_search
embedded_idl
exclude
high_method_prefix
high_property_prefixes
implementation_only
include()
inject_statement
named_guids
no_auto_exclude
no_dual_interfaces
no_implementation
no_namespace
no_search_namespace
no_smart_pointers
raw_dispinterfaces
raw_interfaces_only
raw_method_prefix
raw_native_types
raw_property_prefixes
rename
rename_namespace
rename_search_namespace
tlbid
关于这些属性,详细的可以参见
http://msdn2.microsoft.com/en-us/library/8etzzkb6(VS.71).aspx#_predir_the_23import_directive_import_attributes