// hidtestDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "hidtest.h"
#include "hidtestDlg.h"

//#include "hid_dll.h"
//using namespace std;  
//#pragma comment(lib,"hid_dll.lib") 

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


typedef unsigned char uint8,uint8_t;
typedef unsigned int uint32,uint32_t;
uint8_t is_init=0;
HMODULE hMod = NULL;
typedef void(_stdcall *funShowDlg)();
typedef int(_stdcall *funScan_custom_data_write)(char *pBuf, UINT uiBufLen);

typedef void(_stdcall *funScan_init)();
//timeout = INFINITE, 一直等待
typedef int(_stdcall *funScan_custom_data_read)(char *pReadbuf, UINT pReadLen, uint32 timeout);

funScan_custom_data_write Scan_custom_data_write =NULL;
funScan_custom_data_read Scan_custom_data_read=NULL;



// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};



CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// ChidtestDlg 对话框




ChidtestDlg::ChidtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(ChidtestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void ChidtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(ChidtestDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &ChidtestDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &ChidtestDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON1, &ChidtestDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON_INITIAL, &ChidtestDlg::OnBnClickedButtonInitial)
END_MESSAGE_MAP()

void DisplayLastError(char* Operation)
{
#ifdef _DEBUG

	LPVOID lpMsgBuf;
	USHORT Index = 0;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
		);

	TRACE(Operation);
	//printf("%ws",lpMsgBuf);
	OutputDebugString((LPCWSTR)lpMsgBuf);

	//MessageBox((LPCTSTR)lpMsgBuf, _T("Error"), MB_OK | MB_ICONINFORMATION,0 );
	//MessageBox(NULL,(LPCTSTR)lpMsgBuf,_T("提示"),0);
	LocalFree(lpMsgBuf); 
#endif
}


// ChidtestDlg 消息处理程序

BOOL ChidtestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void ChidtestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void ChidtestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR ChidtestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



// read data from Scanner
void ChidtestDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
	if (hMod)
	{
		//获取动态链接库中Scan_custom_data_read函数的地址
		TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
		if(Scan_custom_data_read==NULL){
			Scan_custom_data_read = (funScan_custom_data_read)GetProcAddress(hMod,"Scan_custom_data_read");
		}
		if(Scan_custom_data_read){
			TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
			char Buf[1024];
			UINT uiBufLen = 33;
			int nRead;
			memset(Buf, 0, sizeof(Buf));
			while(1){// contiune read
				nRead = Scan_custom_data_read(Buf, uiBufLen, 10000);
				if(nRead>0){
					int i;
					TRACE("\n read len%d, data->: ",nRead);
		    		for(i=0; i<nRead; i++){
						TRACE("%02x ",Buf[i]&0xff);
					}
					TRACE("\n");
				}else if(nRead==-1){
					break;
				}
			}
		}else{
			TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
		}

		/*{
			char *buf="0123456789012345678901234567890123456789";
			uint32_t data_checksum = check_sum((unsigned char *)buf, 40);
			TRACE("%s, L:%d,data_checksum:%d, 0x%8x\n", __FUNCTION__,__LINE__,data_checksum,data_checksum);
			
		}*/

	}else{
		TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
	}
}



void ChidtestDlg::OnBnClickedCancel()
{
	FreeLibrary(hMod);
	OnCancel();
}

// write
char Buf[128];
void ChidtestDlg::OnBnClickedButton1()
{
	TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
	if(hMod)
	{
		//获取动态链接库中ShowDlg函数的地址
		TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
		if(Scan_custom_data_write==NULL){
			TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
			Scan_custom_data_write = (funScan_custom_data_write)GetProcAddress(hMod,"Scan_custom_data_write");
		}
		if(Scan_custom_data_write)
		{
			memset(Buf, 0, sizeof(Buf));
			sprintf((char *)Buf, "%s,L:%d\n", __FUNCTION__, __LINE__);
			if(Scan_custom_data_write(Buf, strlen(Buf))<0){ // error
				return;
			}
			TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
		}else{
			TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
		}

	}else{
		TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
	}
}

void ChidtestDlg::OnBnClickedButtonInitial()
{
	// TODO: 在此添加控件通知处理程序代码
	funScan_init Scan_init;

	if(hMod==NULL){
		/*
#ifdef _DEBUG
		hMod = LoadLibrary(_T("../hid_dll_Release/hid_dll.dll"));
#else
		hMod = LoadLibrary(_T("../../hid_dll_Release/hid_dll.dll"));
#endif*/
		hMod = LoadLibrary(_T("hid_dll.dll"));
		is_init = 0;
	}else{
		is_init = 1;
	}
	TRACE("%s, L:%d\n", __FUNCTION__,__LINE__);
	Scan_init = (funScan_init)GetProcAddress(hMod,"Scan_init");
	if (Scan_init && !is_init)
	{
		Scan_init();
	}
	Scan_custom_data_write = (funScan_custom_data_write)GetProcAddress(hMod,"Scan_custom_data_write");
	TRACE("%s, L:%d,Scan_custom_data_write:%p\n", __FUNCTION__,__LINE__,Scan_custom_data_write);
	DisplayLastError("GetProcAddress:");
	Scan_custom_data_read = (funScan_custom_data_read)GetProcAddress(hMod,"Scan_custom_data_read");

}
