STDMETHODIMP CConn::SqlConn(BSTR sestr, _Recordset **ptr)
{
// TODO: Add your implementation code here
    try 
    {
        CoInitialize(NULL);
        conPtr->CursorLocation=adUseClient;
        conPtr->Open(strconn,"","",-1);
        RecPtr->CursorLocation=adUseClient;
        RecPtr->Open(_variant_t(sestr),_variant_t((IDispatch*)           conPtr,true),adOpenDynamic,adLockBatchOptimistic,adCmdText);
        RecPtr->QueryInterface(__uuidof(Recordset),(void**) ptr);
        CoUninitialize();    }    catch(_com_error &e)
    {
        MessageBox(NULL,e.Description(),"warning",MB_OK|MB_ICONWARNING);
    } return S_OK;
}上面的语句我通过atlcom技术将他写成了testsql.dll,功能是调用SqlConn返回一个recordset。但是我在客户端不会调用这个dll程序,由于我公司的因素,我的客户端非得用vc写,没办法只能求大家的帮助了!

解决方案 »

  1.   

    ft,跟那个调用_Recordset差不多阿void CTestDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    USES_CONVERSION;//想用T2OLE就必须加上这一句 CoInitialize(NULL); ITestArrayPtr ptr;
    ptr.CreateInstance(__uuidof(TestArray)); SAFEARRAYBOUND pSab[1];
    pSab[0].lLbound = 0;
    pSab[0].cElements= 3;

    SAFEARRAY *pSa;
    pSa = SafeArrayCreate(VT_BSTR, 1, pSab); BSTR *pBstr;
    SafeArrayAccessData(pSa, (void**)&pBstr);
    *(pBstr) = SysAllocString(T2OLE("BSTR1 "));//也可用A2W
    *(pBstr+1) = SysAllocString(T2OLE("BSTR2 "));
    *(pBstr+2) = SysAllocString(T2OLE("BSTR3 "));
    SafeArrayUnaccessData(pSa); ptr->Show1(pSa); SafeArrayDestroy(pSa); ptr.Release(); CoUninitialize();
    }
      

  2.   

    不要想那么复杂,你用过_RecordsetPtr吗?用过_ConnectPtr嘛?其实他们也都是组件阿。调用自己的组件
    跟调用那些系统的组件方法差不多阿
    客户短:
    stdfx.h
    #import ".\Dll\xtwh.dll" rename_namespace("xtwh") rename ("IPatient","xtwhIPatient")//系统维护
    //.\Dll\xtwh.dll相对路径,存放别人的DLL
    //xtwh工程名.因为自己的工程已经存在IPatient, 而引入得DLL中也包含IPatient,so rename
    program:
    IOperatorPtr pIOperator;
    pIOperator.CreateInstance(__uuidof(Operator));
    pRs=pIOperator->GetOperRsByYHBM((_bstr_t)m_strYHDM);组建
    interface IOperator : IDispatch
    {
    [id(1), helpstring("method GetOperRsByYHBM")] HRESULT GetOperRsByYHBM([in]BSTR yhbm,[out,retval]_Recordset **ppRs);
    [id(2), helpstring("method GetPopedom")] HRESULT GetPopedom([in]BSTR yhbm,[in]BSTR mkbm,[out,retval]_Recordset **ppRs);
    }
    STDMETHODIMP COperator::GetOperRsByYHBM(BSTR yhbm, _Recordset **ppRs)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here
    _ConnectionPtr pConn;
    _RecordsetPtr  pRs;
    CString sqlstr;
    try
    {
    pConn.CreateInstance(__uuidof( Connection ) );
    pConn->CursorLocation = adUseClient;  
    pConn->Open(dsn,"sa","",-1 );
    pRs.CreateInstance(__uuidof( Recordset ) );
    pRs->CursorLocation = adUseClient;
    pRs->PutActiveConnection(pConn.GetInterfacePtr());
    sqlstr="select a.yhbm,a.yhmm,a.yhxm,a.yhxb,a.yhnl,a.ksbm,a.zcbm,a.yhlb,a.yhsm,a.ypsx,b.ksmc,b.kslxbm from gy_yhxx a,gy_ksb b where yhbm='"+CString(yhbm)+"' and a.ksbm=b.ksbm";
    //TCHAR szBuffer[512];
    //wsprintf(szBuffer,_T("select yhmm,yhxm,yhxb,yhnl,ksbm,zcbm,yhlb,yhsm from gy_yhxx where yhbm='%S'"),yhbm);
    //_bstr_t strQuery = szBuffer;
    //MessageBox(NULL,strQuery,"succeed",MB_OK );
    pRs ->Open(_bstr_t(sqlstr),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText );
    pRs ->PutRefActiveConnection( NULL );
    // 关闭连接
    if( pConn ->GetState() == adStateOpen )
    pConn ->Close();
    pConn = NULL;
    *ppRs = pRs.Detach();
    }
    catch(_com_error &e)
    {
    return AtlReportError(CLSID_Operator,_com_util::ConvertBSTRToString(e.Description()),IID_IOperator,E_FAIL);
    }
    catch(...)
    {
    return AtlReportError(CLSID_Operator,"未知错误!",IID_IOperator,E_FAIL); }
    return S_OK;
    }