有人提供了一个函数,但编译时提示很多对象未定义。因为小弟对COM不是很明白,所以请大家帮忙看看还需要引用什么头文件,另外这个函数是否还需要改动
BOOL GetConnectString(TCHAR *lpszConnectString,HWND hWndParent){CComPtr<IDBPromptInitialize> pIDBPromptInitialize=NULL;CComPtr<IDBProperties > pIDBProperties = NULL;CComPtr<IDataInitialize> spDataInit=NULL;CComBSTR bstrConnectString;HRESULT hr;// Create an instance of the Data Links component.hr=CoCreateInstance(CLSID_DataLinks, NULL, CLSCTX_INPROC_SERVER,IID_IDBPromptInitialize, (void **)&pIDBPromptInitialize);if (FAILED(hr))return FALSE;// Prompt the user for the provider and initialization properties.hr=pIDBPromptInitialize->PromptDataSource(NULL, hWndParent,DBPROMPTOPTIONS_PROPERTYSHEET, 0, NULL, NULL, IID_IDBProperties,(IUnknown **)&pIDBProperties);if (FAILED(hr))return FALSE;hr = CoCreateInstance(CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, IID_IDataInitialize, (void**)&spDataInit);if (FAILED(hr))return FALSE;hr=spDataInit->GetInitializationString(pIDBProperties,true,&bstrConnectString);if (FAILED(hr))return FALSE;
lstrcpy(lpszConnectString,(char*)_bstr_t(bstrConnectString));return TRUE;}

解决方案 »

  1.   

    用到COM技术时,好象还要::CoInitialize(NULL);
    CComPtr 所需头文件 Atlbase.h
    _bstr_t #include <comdef.h>
    其他的再查查MSDN吧。应该都有的。
      

  2.   

    d:\软件项目\dataconn\debug\msado15.tlh(405) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
    D:\软件项目\DataConn\DataConnDlg.cpp(21) : error C2065: 'IDBPromptInitialize' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(21) : error C2955: 'CComPtr' : use of class template requires template argument list
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    D:\软件项目\DataConn\DataConnDlg.cpp(21) : error C2514: 'ATL::CComPtr' : class has no constructors
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    D:\软件项目\DataConn\DataConnDlg.cpp(21) : error C2262: 'pIDBPromptInitialize' : cannot be destroyed
    D:\软件项目\DataConn\DataConnDlg.cpp(25) : error C2065: 'IDataInitialize' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(25) : error C2955: 'CComPtr' : use of class template requires template argument list
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    D:\软件项目\DataConn\DataConnDlg.cpp(25) : error C2514: 'ATL::CComPtr' : class has no constructors
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    D:\软件项目\DataConn\DataConnDlg.cpp(25) : error C2262: 'spDataInit' : cannot be destroyed
    D:\软件项目\DataConn\DataConnDlg.cpp(31) : error C2086: 'hr' : redefinition
    D:\软件项目\DataConn\DataConnDlg.cpp(35) : error C2065: 'CLSID_DataLinks' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(37) : error C2065: 'IID_IDBPromptInitialize' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(47) : error C2678: binary '->' : no operator defined which takes a left-hand operand of type 'class ATL::CComPtr' (or there is no acceptable conversion)
    D:\软件项目\DataConn\DataConnDlg.cpp(47) : error C2039: 'PromptDataSource' : is not a member of 'CComPtr'
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    D:\软件项目\DataConn\DataConnDlg.cpp(49) : error C2065: 'DBPROMPTOPTIONS_PROPERTYSHEET' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(57) : error C2065: 'CLSID_MSDAINITIALIZE' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(59) : error C2065: 'IID_IDataInitialize' : undeclared identifier
    D:\软件项目\DataConn\DataConnDlg.cpp(67) : error C2678: binary '->' : no operator defined which takes a left-hand operand of type 'class ATL::CComPtr' (or there is no acceptable conversion)
    D:\软件项目\DataConn\DataConnDlg.cpp(67) : error C2039: 'GetInitializationString' : is not a member of 'CComPtr'
            c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h(563) : see declaration of 'CComPtr'
    Error executing cl.exe.