三、初始化COM
在COM函数可以被调用之前,这个必须先初始化COM库,并且在程序退出之前必须关闭。在ReadBarcodeFromDIB.cpp文件中像下面这样说明:
[cpp]
view plain
copy
- // CReadBarcodeFromDIBApp initialization
- BOOL CReadBarcodeFromDIBApp::InitInstance()
- {
- // Initialize the COM library on the current apartment and identify
- // the currency model as single-thread apartment (STA). Applications
- // must initialize the COM
- // library before they can call COM library functions other than
- // CoGetMalloc and memory allocation functions.
- HRESULT hRes = CoInitialize(NULL);
- AfxEnableControlContainer();
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
>>> 完整代码
四、创建BarcodeXpress COM对象
要使用BarcodeXpress,需要创建一个BarcodeXpress COM实例。在对象被创建之后,对象的属性和方法可以用于创建你的扫描应用程序。在ReadBarcodeFromDIB项目中,这个COM对象在 ReadBarcodeFromDIBDlg.cpp文件中如下创建:
[cpp]
view plain
copy
- // CReadBarcodeFromDIBDlg message handlers
- BOOL CReadBarcodeFromDIBDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- CWnd* pControl = NULL;
- pControl = GetDlgItem(Description);
- if (pControl)
- {
- pControl->SetWindowText(lpctstrDescription);
- }
>>>完整代码