CreateInstance(__uuidof(Connection)) 不支持此接口解決方法

bool CAdoMethod::OpenDatabase(CString dbIP, CString dbKey, CString dbName)
{
    CString my_connect;
    my_connect.Format("Provider=sqloledb;server=%s;database=%s", dbIP, dbName);
    HRESULT hr;
    try{
        hr = m_pConnection.CreateInstance(__uuidof	(Connection));//CreateInstance("ADODB.Connection");
	if (SUCCEEDED(hr)){
		hr = m_pConnection->Open((LPCTSTR)my_connect, "sa", (_bstr_t)dbKey, adModeUnknown);
		if (SUCCEEDED(hr))
			m_IsConnection = true;
		else
			AfxMessageBox("m_pConnection->Open失敗");
	}
	else{
		_com_error e(hr);
		AfxMessageBox(e.ErrorMessage());
		}
	}
	catch(_com_error e){
		m_IsConnection = FALSE;
		_bstr_t bstrDescription(e.Description());
		TRACE("Exception thrown for classes generated by #import");
		TRACE("\t Description = %s \n", (LPCTSTR)bstrDescription);
		AfxMessageBox(bstrDescription);
	} 
	return m_IsConnection;
}

剛開始在win7上編譯運行沒有問題,但是程序拷貝到xp上就出現 不支持此接口的錯誤提示,找了半天找到原因了,主要是msado15.dll版本不同,在stdafx.h中,

#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
no_namespace rename("EOF", "adoEOF")

此處編譯進去的是win7的msado15.dll,當然在xp上沒法運行。

解決方法:將xp下的C:\Program Files\Common Files\System\ado\msado15.dll拷貝到win7下相應目錄重新編譯即可(記得備份啊)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章