我在EXE程序里是这么做的
_ConnectionPtr  m_spConnection;
m_spConnection.CreateInstance(__uuidof(Connection));
m_spConnection->Open(_bstr_t(szValue), "", "", NULL);//以上都没问题,szValue是连接参数m_spAdo->SetConnPtr((unsigned short *)m_spConnection);//m_spAdo是组件,我想把数据库连接指针传进COM//下面是组件里的SetConnPtr方法实现
//STDMETHODIMP CHCAdoService::SetConnPtr(TCHAR* pConnection)
//{
// TODO: 在此添加实现代码// m_spConnection = (_ConnectionPtr)(pConnection);//m_spConnection 是组件里的_ConnectionPtr变量
// return S_OK;
//}有没有什么方法把数据库连接指针传进来

解决方案 »

  1.   

    你的组件运行在你的EXE进程种吗?
      

  2.   

    我还试了下用DWORD  LONG等类型强制转换再 传进来
    也不行 ,传进来以后就变成1,0之类的了
      

  3.   

    进程内同一套件的接口传递方法STDMETHODIMP CHCAdoService::SetConnPtr(_Connection* pConnection)
    {
         m_spConnection = pConnection;
    }
      

  4.   

    调用
    m_spAdo->SetConnPtr(m_spConnection);//m_spAdo是组件,我想把数据库连接指针传进COM 
      

  5.   

    这个我试过了,编译的时候会报错,提示不认识_ConnectionPtr 。
    据说能注册下_ConnectionPtr这个类型,COM就能认识了
    不知道怎么注册
      

  6.   

    传m_spConnection,他是数据库连接指针
      

  7.   

    组件类中的m_spConnection你是怎么定义的?
    不是_Connection类型的指针吗?
      

  8.   

    如果你在COM组件中需要_ConnectionPtr类型变量在你的stdafx.h文件中添加以下代码,然后重新编译#pragma warning(disable: 4146)
    #import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\
     rename("EOF","adoEOF") rename("BOF","adoBOF")
      

  9.   

    有添加,在组件中我是这么定义的_ConnectionPtr m_spConnection;
    (这个跟EXE一样,我就是想把EXE中初始化的链接参数传进来,因为是从连接池中取的)
      

  10.   

    没用过_Connection,我一直用_ConnectionPtr
      

  11.   

    _ConnectionPtr就是_Connection的智能指针,保障了一下而已。
      

  12.   

    程序目的如下:
    EXE里有个连接池,每次有执行数据库操作的时候,从中取一个已连接的数据库连接指针然后把这个指针传进m_SPAdo这个组件,由这个组件进行数据库操作
      

  13.   


    如果添加了下面错误就不会发生
    编译的时候会报错,提示不认识_ConnectionPtr 。
    btw。 在使用_ConnectionPtr地方必须#include "stdafx.h"
      

  14.   

    不管咋的,感谢下雪影大哥,太热心了还有大牛吗,讲下怎么在COM里注册类型啊
      

  15.   


    CPP里都有加,关键是IDL文件里报错,说不认识这个类型
    以前是在m_spAdO里做连接的,能用
      

  16.   

    参考:
    http://topic.csdn.net/t/20030810/15/2129906.html
      

  17.   

    试了一下 _ConnectionPtr& _connection  传引用可以使用的
      

  18.   

    我是在cpp里用的哈,不知道你那个行不行哦
      

  19.   

    晕死IDL中你也使用上述语句啊?
    IDL中使用
    importlib("C:\Program   Files\Common   Files\System\ADO\msado15.dll");   
      

  20.   

    给你一个例子import   "oaidl.idl";  
    import   "ocidl.idl";  
       
      [  
      uuid(D58CA366-E404-4F1A-92E4-D818D3B191CA),  
      version(1.0),  
      helpstring("MyInterfaceLib   1.0   Type   Library")  
      ]  
      library   MYINTERFACELib  
      {  
      importlib("stdole32.tlb");  
      importlib("stdole2.tlb");  
      importlib("C:\Program   Files\Common   Files\System\ADO\msado15.dll");  //在这里添加支持ADO接口的类库
       
      [  
      object,  
      uuid(A0EF8702-BFA1-4081-A209-323BDA9B598B),  
      dual,  
      helpstring("IMyInterface   Interface"),  
      pointer_default(unique)  
      ]  
      interface   IMyInterface   :   IDispatch  
      {  
      [id(1),   helpstring("method   getrs")]   HRESULT   getrs([in]   BSTR   strQuery,   [out,retval]   _Recordset   **ppRs);  
      };  
       
      [  
      uuid(F6A5DFD8-A4D0-4B51-9A2D-88DA156CEF2B),  
      helpstring("MyInterface   Class")  
      ]  
      coclass   MyInterface  
      {  
      [default]   interface   IMyInterface;  
      };  
      };   
      

  21.   

    .\HCAdo.idl(6) : error MIDL2347 : importlib is not allowed outside of a library block : C:\\Program Files\\Common Files\\System\\ado\\msado15.dll1>.\HCAdo.idl(21) : error MIDL2025 : syntax error : expecting a type specification near "_ConnectionPtr"
    1>.\HCAdo.idl(21) : error MIDL2026 : cannot recover from earlier syntax errors; aborting compilation 添加这个importlib ("C:\\Program Files\\Common Files\\System\\ado\\msado15.dll");
    出错上面的错误诶
      

  22.   

    importlib("C:\Program Files\Common Files\System\ADO\MSADO15.DLL");
    单斜线不是双斜线
      

  23.   

    import "oaidl.idl";
    import "ocidl.idl";[
    object,
    uuid(B41D7A6C-F435-48E4-87E5-E60CF3720FDA),
    dual,
    nonextensible,
    helpstring("IHCAdoService 接口"),
    pointer_default(unique)
    ]
    interface IHCAdoService : IDispatch{
    [id(1), helpstring("方法execQuery")] HRESULT execQuery([in] TCHAR* sql, [out] LPDISPATCH** ppRecordset);
    [id(2), helpstring("方法execDML")] HRESULT execDML([in] TCHAR* sql);
    [id(3), helpstring("方法SetConnPtr")] HRESULT SetConnPtr([in]_ConnectionPtr pConnection);
    };
    [
    uuid(DD82DE12-5050-48AD-BEBD-941FA958955D),
    version(1.0),
    helpstring("HCAdo 1.0 类型库")
    ]
    library HCAdoLib
    {
    importlib("stdole2.tlb");
    importlib ("C:\Program Files\Common Files\System\ado\msado15.dll")
    [
    uuid(E6849756-06CB-4E00-B889-4F7CBE08C199),
    helpstring("HCAdoService Class")
    ]
    coclass HCAdoService
    {
    [default] interface IHCAdoService;
    };
    };还是那两行错误
    1>.\HCAdo.idl(21) : error MIDL2025 : syntax error : expecting a type specification near "_ConnectionPtr"
    1>.\HCAdo.idl(21) : error MIDL2026 : cannot recover from earlier syntax errors; aborting compilation 
      

  24.   

    Com组件的参数用LPDISPATCH类型好了,这样就可以传递过去了
    STDMETHODIMP CHCAdoService::SetConnPtr(LPDISPATCH lpDispatch) 

        m_spConnection = lpDispatch; 
    }
      

  25.   

    哎library HCAdoLib     //Library必须手动提前[
    uuid(DD82DE12-5050-48AD-BEBD-941FA958955D),
    version(1.0),
    helpstring("HCAdo 1.0 类型库")
    ]
    library HCAdoLib   //这里要提前
    {
    importlib("stdole2.tlb");
    importlib ("C:\Program Files\Common Files\System\ado\msado15.dll")
    }; interface IHCAdoService : IDispatch{
    [id(1), helpstring("方法execQuery")] HRESULT execQuery([in] TCHAR* sql, [out] LPDISPATCH** ppRecordset);
    [id(2), helpstring("方法execDML")] HRESULT execDML([in] TCHAR* sql);
    [id(3), helpstring("方法SetConnPtr")] HRESULT SetConnPtr([in]_ConnectionPtr pConnection);
    }; 累死我了
      

  26.   

    类库的导入要在接口声明的前面即library HCAdoLib要在
    interface IHCAdoService前面
      

  27.   

    import "oaidl.idl";
    import "ocidl.idl";[
    object,
    uuid(B41D7A6C-F435-48E4-87E5-E60CF3720FDA),
    dual,
    nonextensible,
    helpstring("IHCAdoService 接口"),
    pointer_default(unique)
    ]
    library HCAdoLib
    { importlib ("stdole2.tlb");
    importlib ("C:\Program Files\Common Files\System\ado\msado15.dll");
    [
    uuid(E6849756-06CB-4E00-B889-4F7CBE08C199),
    helpstring("HCAdoService Class")
    ]
    coclass HCAdoService
    {
    [default] interface IHCAdoService;
    };
    };
    interface IHCAdoService : IDispatch{
    [id(1), helpstring("方法execQuery")] HRESULT execQuery([in] TCHAR* sql, [out] LPDISPATCH** ppRecordset);
    [id(2), helpstring("方法execDML")] HRESULT execDML([in] TCHAR* sql);
    [id(3), helpstring("方法SetConnPtr")] HRESULT SetConnPtr([in]_ConnectionPtr pConnection);
    };
    [
    uuid(DD82DE12-5050-48AD-BEBD-941FA958955D),
    version(1.0),
    helpstring("HCAdo 1.0 类型库")
    ]我的
      

  28.   

    我只负责ADO组件,具体原因不是特别清楚,
    可能是这么考虑的:每次数据库操作都是通过一个ADO组件来执行,只要传参数,就返回记录集(有点把数据库的东西封装起来的意思)如果每次都做连接数据库操作影响效率,如果只用一个连接怕这个连接意外断掉,所以每次操作都由连接池给个活的连接。不知道是不是有点四不像。。请指教
      

  29.   

    请问在ATL中能否将ASP中的数据库连接对象conn传到COM中? 
    时间:2009-07-25 22:36:09  来源:  作者: 
    请问能否在ATL中将ASP中的数据库连接对象如:   
        
          Dim   Conn   
          Set   Conn   =   Server.CreateObject(;ADODB.Connection;)   
          conn.Open   ;Driver={Microsoft   ODBC   for   Oracle};;   &   _   
                              ;Server=server1;;   &   _   
                              ;Uid=dd;;   &   _   
                              ;Pwd=12345;;   
        
      这个conn对象通过COM的参数传到COM的方法中,在COM中可以直接用这个CONN   对象?   
      如果可行那请问方法的参数怎么写?是不是和_ConnectionPtr的对象一样用法?问题点数:0、回复次数:3|||1         在   你的   .idl文件中   加入   importlib(;C:\Program   Files\Common   Files\System\ADO\MSADO15.DLL;);   
        
      2         在   stdafx.h   中加入   #import   ;C:\Program   Files\Common   Files\System\ADO\msado15.dll;     no_namespace   rename(;EOF;,   ;EndOfFile;)   
        
      3     给你的ATL对象增加一个属性   
      名称:   Connect   
      类型:   _Connection   
      注意选择   propPputref   
      |||接上文:   
        注意选择   PropPutRef   
        
      4   给你的ATL类加一个私有成员:   _ConnectionPtr       m_ConnectionPtr;       
        
      相关的代码如下:   
      STDMETHODIMP   YourOBJ::putref_ActiveConnection(_Connection   *newVal)   
      {   
      if(   m_ConnectionPtr!=0)   m_ConnectionPtr=0;   
      m_ConnectionPtr   =   _ConnectionPtr(   newVal);   
                        return   S_OK;   
      }   
        
      STDMETHODIMP   YourOBJ::get_ActiveConnection(_Connection   **pVal)   
      {   
              if(   m_ConnectionPtr==0)   return   E_POINTER;   
              return   m_ConnectionPtr->QueryInterface(__uuidof(_Connection),(void**)(pVal));   
        
      }   
        
      |||接上文:   
        
      勘误:   
        
      3     给你的ATL对象增加一个属性   
      名称:   ActiveConnection   
      类型:   _Connection*   
      注意选择   PropPutRef