⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmscanfs531ctl.cpp

📁 一个与控件的测试程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  // HMScanFS531Ctl.cpp : Implementation of the CHMScanFS531Ctrl ActiveX Control class.

#include "stdafx.h"
#include "HMScanFS531.h"
#include "HMScanFS531Ctl.h"
#include "HMScanFS531Ppg.h"
#include "Mywnd.h"

#include <process.h>

#include "DebugLog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define ENABLE_FLAG 1
#define DISABLE_FLAG 0

#define FILENAME ".\\id001.bmp"
#define PATH_SIZE MAX_PATH

void ThreadCalibProc(LPVOID pParam);     //校验线程
void ThreadScanProc(LPVOID pParam);   //普通扫描线程
void ThreadNidScanProc(LPVOID pParam);   //防伪扫描

bool g_load;
IDCARD_ALL g_card;
CRITICAL_SECTION cs;

bool g_bCalibFlag;  ///校验标志,, 1--停止。
bool g_bNidScanFlag;   ////防伪扫描标志,,1--停止。
bool g_bScanFlag;     ///无防伪扫描标志,,1--停止。

//声明接口
int  (WINAPI *pLoad)(void);         //加载动态库
void (WINAPI *pFree)(void);         //释放动态库
void (WINAPI *pCalibrate)(void);    //扫描器校验。
//扫描,并取出信息
int (WINAPI *pScanRecogExAll)(LPCTSTR,IDCARD_ALL&,bool,int,int,char *,short);
int (WINAPI *pRecogIdcardRe)(LPCTSTR,IDCARD_ALL&,bool,int,int,char*,short,bool);
//配置扫描器参数
//int   (WINAPI *pScanOther)(char*,Scan_Para&);

#define WM_THREADSCANEVENT WM_USER+120   //少秒完成
#define WM_THREADCALIBEVENT WM_USER+121   //校验完成
#define WM_OIDDEVICEERR			WM_USER+126  //一代证设备错


IMPLEMENT_DYNCREATE(CHMScanFS531Ctrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CHMScanFS531Ctrl, COleControl)
	//{{AFX_MSG_MAP(CHMScanFS531Ctrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CHMScanFS531Ctrl, COleControl)
	//{{AFX_DISPATCH_MAP(CHMScanFS531Ctrl)
	DISP_PROPERTY_EX(CHMScanFS531Ctrl, "DeviceStatus", GetDeviceStatus, SetDeviceStatus, VT_I2)
	DISP_FUNCTION(CHMScanFS531Ctrl, "InitComm", InitComm, VT_I2, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "CloseCom", CloseCom, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "Calibrate", Calibrate, VT_I2, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "IdScanRcog", IdScanRcog, VT_I2, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "NidScanRcog", NidScanRcog, VT_I2, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardName", GetCardName, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardNum", GetCardNum, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardSex", GetCardSex, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardAddress", GetCardAddress, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardPeople", GetCardPeople, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardBirthday", GetCardBirthday, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardSigndata", GetCardSigndata, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardValidterm", GetCardValidterm, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetCardOther", GetCardOther, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "CancelCom", CancelCom, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CHMScanFS531Ctrl, "GetEndData", GetEndData, VT_BSTR, VTS_NONE)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CHMScanFS531Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CHMScanFS531Ctrl, COleControl)
	//{{AFX_EVENT_MAP(CHMScanFS531Ctrl)
	EVENT_CUSTOM("DeviceStatus", FireDeviceStatus, VTS_I2)
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CHMScanFS531Ctrl, 1)
	PROPPAGEID(CHMScanFS531PropPage::guid)
END_PROPPAGEIDS(CHMScanFS531Ctrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CHMScanFS531Ctrl, "HMSCANFS531.HMScanFS531Ctrl.1",
	0xa6a59fee, 0x54a8, 0x4119, 0xab, 0xf0, 0x30, 0xe7, 0x99, 0x8c, 0xe8, 0x94)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CHMScanFS531Ctrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DHMScanFS531 =
		{ 0xa51d3868, 0xe29, 0x4c2c, { 0x86, 0xd8, 0x8f, 0x1d, 0x7e, 0xa6, 0x5a, 0xdb } };
const IID BASED_CODE IID_DHMScanFS531Events =
		{ 0x4123b8b5, 0x2850, 0x416b, { 0x92, 0xfe, 0x5d, 0x11, 0x93, 0xb7, 0xed, 0xdf } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwHMScanFS531OleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CHMScanFS531Ctrl, IDS_HMSCANFS531, _dwHMScanFS531OleMisc)


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::CHMScanFS531CtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CHMScanFS531Ctrl

BOOL CHMScanFS531Ctrl::CHMScanFS531CtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_HMSCANFS531,
			IDB_HMSCANFS531,
			afxRegApartmentThreading,
			_dwHMScanFS531OleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::CHMScanFS531Ctrl - Constructor

CHMScanFS531Ctrl::CHMScanFS531Ctrl()
{
	InitializeIIDs(&IID_DHMScanFS531, &IID_DHMScanFS531Events);

	g_load = false;

	m_sDeviceStatus = OLD_CARD_DEVICE_STATUS_NULL;

	pWnd = new CMyWindow(this);
	if(NULL == pWnd)
	{
		return;
	}

	hWndMsg = pWnd->Create();

	hThreadScanProc = NULL;
	hThreadCalibProc = NULL;
	hThreadNidScanProc = NULL;

	memset(&g_card, 0x00, sizeof(IDCARD_ALL));

	InitializeCriticalSection(&cs);
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::~CHMScanFS531Ctrl - Destructor

CHMScanFS531Ctrl::~CHMScanFS531Ctrl()
{
	g_load = false;

	m_sDeviceStatus = OLD_CARD_DEVICE_STATUS_NULL;

	if(pWnd != NULL)
	{
		pWnd->Close();
		pWnd = NULL;
	}

	DeleteCriticalSection(&cs);

	g_bNidScanFlag = FALSE;
	g_bCalibFlag = FALSE;
	g_bScanFlag = FALSE;

	if(hThreadCalibProc != NULL)
	{
		CloseHandle(hThreadCalibProc);
		hThreadCalibProc = NULL;
	}

	if(hThreadScanProc != NULL)
	{
		CloseHandle(hThreadScanProc);
		hThreadScanProc = NULL;
	}

	if(hThreadNidScanProc == NULL)
	{
		CloseHandle(hThreadNidScanProc);
		hThreadNidScanProc = NULL;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::OnDraw - Drawing function

void CHMScanFS531Ctrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	pdc->Ellipse(rcBounds);
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::DoPropExchange - Persistence support

void CHMScanFS531Ctrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::OnResetState - Reset control to default state

void CHMScanFS531Ctrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl::AboutBox - Display an "About" box to the user

void CHMScanFS531Ctrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_HMSCANFS531);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CHMScanFS531Ctrl message handlers
/*!****************************************************
函数名称:Init_Com
函数功能:加载动态库
函数类型:void
输入参数:
输出参数:void
返回值:  0 -- 成功, 3--加载动态库失败, 1--核心失败, 2--硬件校验失败,
****************************************************!*/
short CHMScanFS531Ctrl::InitComm() 
{
//	char filename[100];
	int val = 0;
	WriteParseLog(__FILE__,__LINE__,"%s","初始化,加载动态库,,InitComm !!");

	if(g_load)
		return INIT_SUCCEED;

	handle =::LoadLibrary("idcarddll.dll");
    if(NULL == handle)
	{
		WriteParseLog(__FILE__,__LINE__,"%s","初始化,加载动态库失败!!..,,InitComm !!");
		m_sDeviceStatus = OLD_DEVICE_STATUS_ERR;
		return LOAD_DLL_FAIL;   ///   "加载动态库失败!"
	}

	pLoad           = (int  (WINAPI *)(void))GetProcAddress(handle,"LoadIdcardLibrary");
	pFree           = (void (WINAPI *)(void))GetProcAddress(handle,"FreeIdcardLibrary");
	pCalibrate      = (void (WINAPI *)(void))GetProcAddress(handle,"CalibrateScanner");
	pScanRecogExAll = (int  (WINAPI *)(LPCTSTR,IDCARD_ALL&,bool,int,int,char *,short))GetProcAddress(handle,"RecogIdcardExALL");
	pRecogIdcardRe  = (int  (WINAPI *)(LPCTSTR,IDCARD_ALL&,bool,int,int,char *,short,bool))GetProcAddress(handle,"RecogIdcardRe");
	if(NULL == pLoad || NULL == pFree || NULL == pCalibrate 
		|| NULL == pScanRecogExAll || NULL == pRecogIdcardRe)
	{
		WriteParseLog(__FILE__,__LINE__,"%s","初始化,加载动态库失败,卸载动态库,,,InitComm !!");

		FreeLibrary(handle);
		handle = NULL;
		m_sDeviceStatus = OLD_DEVICE_STATUS_ERR;
		return LOAD_DLL_FAIL;   ///  "加载动态库失败!"
	}
	
	//加载识别核心
	val = pLoad();
	WriteParseLog(__FILE__,__LINE__,"%s [加载核心动态库状态]=[%d]","初始化,,,InitComm !!",val);

	switch(val)
	{
	case LOAD_KNERL_FAIL_FLAG:
		FreeLibrary(handle);
		handle = NULL;
		m_sDeviceStatus = OLD_DEVICE_STATUS_ERR;
		return LOAD_KNERL_MODULE_FAIL;    ////  "加载识别核心失败!"
	case LOAD_HD_ENCRYPT_FAIL_FLAG:
		FreeLibrary(handle);
		handle = NULL;
		m_sDeviceStatus = OLD_DEVICE_STATUS_ERR;
		return SCAN_CLIAP_FAIL;   ////  "硬件加密校验失败!"
	default:
		break;
	}
	
	WriteParseLog(__FILE__,__LINE__,"%s","初始化,加载动态库成功。,,InitComm !!");
	m_sDeviceStatus = OLD_DEVICE_STATUS_OK;

	g_load = true;
	return INIT_SUCCEED;    // 成功.
}

/*!****************************************************
函数名称:Close_Com
函数功能:卸载动态库
函数类型:void
输入参数:
输出参数:void
返回值:  void
****************************************************!*/
void CHMScanFS531Ctrl::CloseCom() 
{
	WriteParseLog(__FILE__,__LINE__,"%s","关闭端口,,CloseCom !!");

	if(g_bCalibFlag == FALSE)
		g_bCalibFlag = TRUE;

	if(g_bNidScanFlag == FALSE)
		g_bNidScanFlag = TRUE;

	if(g_bScanFlag == FALSE)
		g_bScanFlag = TRUE;

	if (g_load)
	{
		WriteParseLog(__FILE__,__LINE__,"%s","卸载动态库,,CloseCom !!");

		//卸载识别核心
		pFree();
		//卸载动态库
		FreeLibrary(handle);
		m_sDeviceStatus = OLD_DEVICE_STATUS_NULL;

		handle = NULL;

		g_load = false;
		memset(&g_card,0x00,sizeof(IDCARD_ALL));
	}

	WriteParseLog(__FILE__,__LINE__,"%s","关闭端口成功。。。,,CloseCom !!");
}

/*!****************************************************
函数名称:Calibrate
函数功能:扫描器校验
函数类型:void
输入参数:
输出参数:void
返回值:  void
****************************************************!*/
short CHMScanFS531Ctrl::Calibrate() 
{
	WriteParseLog(__FILE__,__LINE__,"%s","校验主线程,,Calibrate !!");

    if(hThreadCalibProc)
	{
		hThreadCalibProc = NULL;
	}

	if (!g_load)
	{
		WriteParseLog(__FILE__,__LINE__,"%s","端口未打开,,Calibrate !!");

		return -1;
	}

	WriteParseLog(__FILE__,__LINE__,"%s","启动校验线程,,Calibrate !!");

	hThreadCalibProc = (HANDLE)_beginthread(ThreadCalibProc,0,(LPVOID)this);

	if (!hThreadCalibProc)
	{
		WriteParseLog(__FILE__,__LINE__,"%s","校验主线程启动失败。。,,Calibrate !!");
		hThreadCalibProc = NULL;
		return -1;
	}

	WriteParseLog(__FILE__,__LINE__,"%s","校验主线程,运行成功。!,Calibrate !!");

	return OLD_CARD_CALIB_SUCCEED;
}


void ThreadCalibProc(LPVOID pParam)
{	
	CHMScanFS531Ctrl *pCtrl = (CHMScanFS531Ctrl*)pParam;
	short ret1=0;

	pCtrl->m_sDeviceStatus = OLD_CARD_DEVICE_STATUS_NULL;

	WriteParseLog(__FILE__,__LINE__,"%s","校验副线程运行。。。。, !!");

	while(1)   
	{
		WaitForSingleObject(pCtrl->hThreadCalibProc, 200);

		if(g_bCalibFlag)
		{
			_endthread();
		}

		if(ret1==0)
		{
			WriteParseLog(__FILE__,__LINE__,"%s","校验。。。。,, !!");

			pCalibrate();
			pCtrl->m_sDeviceStatus = OLD_CARD_CALIB_SUCCEED;
			break;
		}

		pCtrl->m_sDeviceStatus = OLD_DEVICE_STATUS_BUSY;
		Sleep((DWORD)100);
	}

	WriteParseLog(__FILE__,__LINE__,"%s","校验完成,发送消息,WM_THREADCALIBEVENT,。。,,!!");

	PostMessage(pCtrl->hWndMsg, WM_THREADCALIBEVENT, (WPARAM)NULL, (LPARAM)NULL); 

	WriteParseLog(__FILE__,__LINE__,"%s","校验线程运行成功。。!。,, !!");

	_endthread();
}

/*!****************************************************
函数名称:IdScanRcog

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -