我在 Visual Studio 2008 中下载并打开的示例项目中有以下代码行
_bstr_t bstrConnect = "PATH to MDB file";
int _tmain(int argc, _TCHAR* argv[]){
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
cout<<DAM<<": Failed to CoInitialize() COM."<<endl;
return hr;
}
DAO::_DBEngine* pEngine = NULL;
hr = CoCreateInstance(
__uuidof(DAO::DBEngine),
NULL,
CLSCTX_ALL,
IID_IDispatch,
(LPVOID*)&pEngine);
if (SUCCEEDED(hr) && pEngine)
{
try
{
DAO::DatabasePtr pDbPtr = NULL;
pDbPtr = pEngine->OpenDatabase(bstrConnect);
此代码有效。然后我将一些非常相似的代码放入 Visual Studio 2012 项目中
_bstr_t bstrConnect = "PATH to MDB file";
int _tmain(int argc, _TCHAR* argv[]) {
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout<<"Failed to CoInitialize() COM."<<endl;
cout << _com_error(hr).ErrorMessage() << endl;
return hr;
}
DAO::_DBEngine* pEngine = NULL;
hr = CoCreateInstance(
__uuidof(DAO::DBEngine),
NULL,
CLSCTX_ALL,
IID_IDispatch,
(LPVOID*)&pEngine);
if (SUCCEEDED(hr) && pEngine)
{
try
{
DAO::DatabasePtr pDbPtr = NULL;
pDbPtr = pEngine->OpenDatabase(bstrConnect);
在 2012 版本中,当我调用 pEngine->OpenDatabase 时,我得到一个 System.AccessViolationException。我需要在 2012 代码中更改什么吗?我最初在 2012 版本中有 CoInitialize,但它一直返回错误,直到我切换到 CoInitializeEx。