问题是这样的小弟正在看一个程序是在VB下调用ActiveX控件写的一个程序.
在VB程序的开头是这样写的
Option Explicit
Dim V111 As New CPLCBit
然后在Form里又这样写的其中VarMgr1是控件
VarMgr1.Init "9600,n,8,1", 1
VarMgr1.RegisterPLCVar "V111", V1111
V111.Value = Text2.Text
然后运行好使.我想把这个程序用VC写一下
之后我建了个对话框把这个控件加到VC的工程里之后我这样写的
BOOL CDlg::OnInitDialog()
{
......
CComVariant varAddress="9600,n,8,1";
CComVariant var=1;
m_PLC.Init(varAddress.pbstrVal,var.piVal);
CComVariant aa="V1029.0";
LPDISPATCH* lpDispatch=NULL;
m_PLC.RegisterPLCVar(aa.pbstrVal, lpDispatch);
}
其中m_PLC是这个控件的对象
BOOL Init(BSTR* settingStr, short* port);
BOOL RegisterPLCVar(BSTR* varAddress, LPDISPATCH* plcVar);这是我调用那2个函数的参数原形.
然后我可以编译过去可是运行时提示
Automation error 
The object invoked has disconnetded from its clients.
请高手给我讲下为什么这样问题在那?如何做那?请高手说详细些好吗?谢谢!

解决方案 »

  1.   

    使用COM组件要先对COM库进行初始化
    CoInitializeEx
      

  2.   

    我在BOOL CAGVApp::InitInstance()函数里调用了::CoInitialize(NULL);呀还是一样呀!
      

  3.   

    BOOL CAGVApp::InitInstance()
    {
    ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
    AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CAGVDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }
    我在初始化函数里这样写的
      

  4.   

    就讲一个最基本的问题好了
     CComVariant varAddress="9600,n,8,1";
     m_PLC.Init(varAddress.pbstrVal,var.piVal);
    你自己Debug看看这里的varAddress.pbstrVal是什么值?这个好象是赋值给varAddress.bstrVal的吧,你用&varAddress.bstrVal倒好说得过去,var.piVal就不多说了,你自己看吧(你可以用short x = *(var.piVal)先运行看看能不能通过)不明白为什么要用这种BSTR*,LPDISPATCH*之类的这里好象是MFC吧,虽然说没问题,不过用上一个CComVariant感觉真是不伦不类,明明用CString和简单的类型可以解决的问题,非要来个CComVariant干嘛你VB中的RegisterPLCVar的第二个参数好象有值的吧,你在VC中传个NULL,好象不一样吧,会不会出问题就不知道了。总之,不可思议,你这样的代码能行才叫怪
      

  5.   

    我知道这样写有问题,我只是想把用VB写的程序改为VC 写的请高手指教好吗?