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下相应目录重新编译即可(记得备份啊)

你可能感兴趣的:(C/C++)