对方公司以 OLE 对象的方式将功能提供给我,
成功使用 regsvr32 注册了 DLL 文件,在 VBA 中创建 OLE 对象并调用其方法必定成功:Public Sub mysub()
Dim appcode As Integer
Dim appmsg As String
Set yhif = CreateObject("YinHai.XNHInterface")
yhif.yh_interface_init appcode, appmsg // 在 VBA 中调用此方法成功
If appcode <= 0 Then
MsgBox apmsg
End If
End Sub
但是在 MFC 中,调用此 OLE 对象的方法却总是失败:Cn_yh_interface_xnh yhif; // 从类型库中自动生成的OLE对象包装类
short nAppCode;
BSTR bstrAppMsg;try
{
if( !yhif.CreateDispatch( _T("YinHai.XNHInterface") ) )
{
MessageBox( _T("创建 YinHai OLE 对象失败!") );
EndDialog( IDCANCEL );
} yhif.yh_interface_init( &nAppCode, &bstrAppMsg ); // 总是失败
...
......在 MFC 中,执行到
yhif.yh_interface_init( &nAppCode, &bstrAppMsg );
这句时总是提示:
Test.exe 中的 0x770f4c47 处未处理的异常: 0xC0000005: 读取位置 0xccccccc8 时发生访问冲突
请高手指点,为什么会这样?
成功使用 regsvr32 注册了 DLL 文件,在 VBA 中创建 OLE 对象并调用其方法必定成功:Public Sub mysub()
Dim appcode As Integer
Dim appmsg As String
Set yhif = CreateObject("YinHai.XNHInterface")
yhif.yh_interface_init appcode, appmsg // 在 VBA 中调用此方法成功
If appcode <= 0 Then
MsgBox apmsg
End If
End Sub
但是在 MFC 中,调用此 OLE 对象的方法却总是失败:Cn_yh_interface_xnh yhif; // 从类型库中自动生成的OLE对象包装类
short nAppCode;
BSTR bstrAppMsg;try
{
if( !yhif.CreateDispatch( _T("YinHai.XNHInterface") ) )
{
MessageBox( _T("创建 YinHai OLE 对象失败!") );
EndDialog( IDCANCEL );
} yhif.yh_interface_init( &nAppCode, &bstrAppMsg ); // 总是失败
...
......在 MFC 中,执行到
yhif.yh_interface_init( &nAppCode, &bstrAppMsg );
这句时总是提示:
Test.exe 中的 0x770f4c47 处未处理的异常: 0xC0000005: 读取位置 0xccccccc8 时发生访问冲突
请高手指点,为什么会这样?
COM库是这样初始化的:HRESULT hRet = ::CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
{
MessageBox( _T("创建 YinHai OLE 对象失败!") );
EndDialog( IDCANCEL );
} yhif.yh_interface_init( &nAppCode, &bstrAppMsg ); // 如果第一个方法成功
...
......
yhif.yh_interface_call( sJYBH, sJYKZ_XML, sJYSR_XML,
&bstrPCBH, &bstrJYLSH, &bstrJYYZM, &bstrJYSC_XML, &lAppCode, &bstrAppMsg ); // 那么在第二个方法处就会出现同样的提示。
...
......
PorgID 肯定是正确的,因为 CreateDispatch() 都成功了。