COM组件初始化时的错误

使用环境:

Windows XP、VC++

出现错误:

在用ADO组件进行数据库操作时,由于是COM组件,需要初始化COM组件。

使用函数:AfxOleInit()

在编译时出现如下错误:

error C2065: 'AfxOleInit' : undeclared identifier

错误分析:

看到很多使用AfxOleInit()的场合都是MFC情景下,因此需要包含一些afx开头的头文件

应该是缺少头文件

在网上查找,AfxOleInit()方法对应的头文件应该是:#include <afxdisp.h>

但是在包含了该头文件之后,链接的时候会出错

问题解决:

用CoInitialize(NULL);来代替AfxOleInit();

关于的说明:Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA).

函数原型:

HRESULT CoInitialize(
  __in_opt  LPVOID pvReserved
);

参数:pvReserved
This parameter is reserved and must be NULL.

你可能感兴趣的:(thread,windows,数据库,mfc,concurrency,library)