从非托管代码调用托管代码, 出错:type mismatch(200分)

  • 主题发起人 主题发起人 hongsen
  • 开始时间 开始时间
H

hongsen

Unregistered / Unconfirmed
GUEST, unregistred user!
1.生成托管的dll,以及typelibrary.
public interface IDotNetInterface
{
void Initialize();
string Caption
{
set;
}
int ShowDialog(string sText);
int ShowDialog();
}
public classdo
tNetClass : IDotNetInterface
{
//COM的互操作性需要一个缺省的公有构造函数
publicdo
tNetClass()
{
}
public void Initialize()
{
m_sCaption = "";
}
public string Caption
{
set { m_sCaption = value;
}
}
public int ShowDialog(string sText)
{
System.Windows.Forms.MessageBox.Show(sText, m_sCaption);
return 0;
}
public int ShowDialog()
{
System.Windows.Forms.MessageBox.Show("hello", "moto");
return 0;
}

private string m_sCaption;
}
2.在VC中引入该typelibrary文件生成新的class(从COleDispatchDriver继承的)
class CDotNetInterface : public COleDispatchDriver
{
public:
CDotNetInterface(){} // Calls COleDispatchDriver default constructor
CDotNetInterface(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
CDotNetInterface(const CDotNetInterface&
dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
// Attributes
public:
// Operations
public:

// IDotNetInterface methods
public:
void Initialize()
{
InvokeHelper(0x60020000, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
}
void put_Caption(LPCTSTR newValue)
{
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x60020001, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, newValue);
}
long ShowDialog(LPCTSTR sText)
{
long result;
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x60020002, DISPATCH_METHOD, VT_I4, (void*)&result, parms, sText);
return result;
}
long ShowDialog_2()
{
long result;
InvokeHelper(0x60020003, DISPATCH_METHOD, VT_I4, (void*)&result, NULL);
return result;
}
// IDotNetInterface properties
public:
};
3.调用代码
CDotNetInterface obj;
if (obj.CreateDispatch(_T("DotNetClass")))
{
obj.ShowDialog_2();
}
结果出错:Type mismatch
请诸位帮忙。
 
这么妇杂、还有VC、类型不匹配造成的吧。
 
没大看懂
 
后退
顶部