//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <windows.h>
#pragma hdrstop

#include "untDllCheck.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TfrmCheck* frmCheck;

int		prtMaxTimes;
int		prtLines;
int		currentTimes;
ULONG	nCount;

// 関数プロトタイプ宣言
bool			get_file_version(TCHAR* i_path, int o_ver[4]);
bool			checkWCharType();
void			FuncStart();
void			FuncEnd(int nRet);
void __stdcall	resultCallback(PCHAR i_prt, int type, int result1, int result2);
void			BinToHEX(char* o_data, BYTE* i_data, SHORT i_size);
String			SBinToSHex(String i_str);
int				fncMakeHDC(HDC* o_hdc, HBITMAP* o_hBitmap, int i_width, int i_height);
int				fncDelHDC(HDC i_hdc, HBITMAP i_hbitmap);

//---------------------------------------------------------------------------
//  Name	: TfrmCheck
//  Detail	: アプリ開始
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
__fastcall TfrmCheck::TfrmCheck(TComponent* Owner) : TForm(Owner)
{
int		nmsVer[4];
long	nmsErr;
TCHAR	strBuf[64];

	m_lpNEnumPrinters 		= NULL;
	m_lpNRenamePrinter 		= NULL;
	m_lpNGetPrinterInf 		= NULL;
	m_lpNGetPrinterFromID	= NULL;
	m_lpNAutoOpen 			= NULL;
	m_lpNOpenPrinter 		= NULL;
	m_lpNClosePrinter 		= NULL;
	m_lpNClosePrinters 		= NULL;
	m_lpNPrint				= NULL;
	m_lpNDPrint 			= NULL;
	m_lpNImagePrint 		= NULL;
	m_lpNImagePrintF 		= NULL;
	m_lpNBitImagePrint 		= NULL;
	m_lpNBitImagePrintF 	= NULL;
	m_lpNGetStatus 			= NULL;
	m_lpNGetInformation 	= NULL;
	m_lpNResetPrinter 		= NULL;
	m_lpNStartDoc 			= NULL;
	m_lpNEndDoc 			= NULL;
	m_lpNCancelDoc 			= NULL;
	m_lpNEnumDoc 			= NULL;
	m_lpNBarcode 			= NULL;
	m_lpNBarcode2 			= NULL;
	m_lpNBarcodeSettings	= NULL;
	m_lpNFirmwareDL 		= NULL;
	m_lpNWritePrinter 		= NULL;
	m_lpNCheckOnline 		= NULL;
	m_lpNDataConvert 		= NULL;
	m_lpNReadPrinter 		= NULL;
	m_lpNDeletePrinter  	= NULL;
	m_lpNSetUSBProtocol 	= NULL;

	m_lpNEnumPrintersA 		= NULL;
	m_lpNRenamePrinterA 	= NULL;
	m_lpNGetPrinterInfA 	= NULL;
	m_lpNGetPrinterFromIDA	= NULL;
	m_lpNOpenPrinterA 		= NULL;
	m_lpNClosePrinterA 		= NULL;
	m_lpNPrintA 			= NULL;
	m_lpNDPrintA 			= NULL;
	m_lpNImagePrintA 		= NULL;
	m_lpNImagePrintFA 		= NULL;
	m_lpNBitImagePrintA 	= NULL;
	m_lpNBitImagePrintFA 	= NULL;
	m_lpNGetStatusA 		= NULL;
	m_lpNGetInformationA 	= NULL;
	m_lpNResetPrinterA 		= NULL;
	m_lpNStartDocA 			= NULL;
	m_lpNEndDocA 			= NULL;
	m_lpNCancelDocA 		= NULL;
	m_lpNEnumDocA	 		= NULL;
	m_lpNBarcodeA 			= NULL;
	m_lpNFirmwareDLA 		= NULL;
	m_lpNWritePrinterA 		= NULL;
	m_lpNCheckOnlineA 		= NULL;
	m_lpNReadPrinterA 		= NULL;
	m_lpNDeletePrinterA		= NULL;
	m_lpNSetUSBProtocolA    = NULL;

	// WLAN対応
	m_lpNInitializeNetwork  = NULL;
	m_lpNScanPrinters 		= NULL;
	m_lpNSetWriteSize 		= NULL;
	m_lpNTCPPortLock 		= NULL;
	m_lpNBufferClear 		= NULL;
	m_lpNBlockSend	 		= NULL;

	m_lpNTCPPortLockA 		= NULL;
	m_lpNBufferClearA 		= NULL;
	m_lpNBlockSendA	 		= NULL;

	// Callback
	m_lpNSetCallback		= NULL;

	hLibHandle = ::LoadLibrary(L"NPrinterLib.dll");

	if(hLibHandle != NULL)
	{
		// Load the necessary functions for this application
		m_lpNEnumPrinters 		= (lpNEnumPrinters)GetProcAddress(hLibHandle, "NEnumPrinters");
		m_lpNRenamePrinter 		= (lpNRenamePrinter)GetProcAddress(hLibHandle, "NRenamePrinter");
		m_lpNGetPrinterInf 		= (lpNGetPrinterInf)GetProcAddress(hLibHandle, "NGetPrinterInf");
		m_lpNGetPrinterFromID	= (lpNGetPrinterFromID)GetProcAddress(hLibHandle, "NGetPrinterFromID");
		m_lpNAutoOpen 			= (lpNAutoOpen)GetProcAddress(hLibHandle, "NAutoOpen");
		m_lpNOpenPrinter 		= (lpNOpenPrinter)GetProcAddress(hLibHandle, "NOpenPrinter");
		m_lpNClosePrinter  		= (lpNClosePrinter)GetProcAddress(hLibHandle, "NClosePrinter");
		m_lpNClosePrinters 		= (lpNClosePrinters)GetProcAddress(hLibHandle, "NClosePrinters");
		m_lpNPrint 				= (lpNPrint)GetProcAddress(hLibHandle, "NPrint");
		m_lpNDPrint 			= (lpNDPrint)GetProcAddress(hLibHandle, "NDPrint");
		m_lpNImagePrint 		= (lpNImagePrint)GetProcAddress(hLibHandle, "NImagePrint");
		m_lpNImagePrintF 		= (lpNImagePrintF)GetProcAddress(hLibHandle, "NImagePrintF");
		m_lpNBitImagePrint 		= (lpNBitImagePrint)GetProcAddress(hLibHandle, "NBitImagePrint");
		m_lpNBitImagePrintF 	= (lpNBitImagePrintF)GetProcAddress(hLibHandle, "NBitImagePrintF");
		m_lpNGetStatus 			= (lpNGetStatus)GetProcAddress(hLibHandle, "NGetStatus");
		m_lpNGetInformation 	= (lpNGetInformation)GetProcAddress(hLibHandle, "NGetInformation");
		m_lpNResetPrinter 		= (lpNResetPrinter)GetProcAddress(hLibHandle, "NResetPrinter");
		m_lpNStartDoc 			= (lpNStartDoc)GetProcAddress(hLibHandle, "NStartDoc");
		m_lpNEndDoc 			= (lpNEndDoc)GetProcAddress(hLibHandle, "NEndDoc");
		m_lpNCancelDoc 			= (lpNCancelDoc)GetProcAddress(hLibHandle, "NCancelDoc");
		m_lpNEnumDoc 			= (lpNEnumDoc)GetProcAddress(hLibHandle, "NEnumDoc");
		m_lpNBarcode 			= (lpNBarcode)GetProcAddress(hLibHandle, "NBarcode");
		m_lpNBarcode2 			= (lpNBarcode2)GetProcAddress(hLibHandle, "NBarcode2");
		m_lpNBarcodeSettings	= (lpNBarcodeSettings)GetProcAddress(hLibHandle, "NBarcodeSettings");
		m_lpNFirmwareDL 		= (lpNFirmwareDL)GetProcAddress(hLibHandle, "NFirmwareDL");
		m_lpNWritePrinter 		= (lpNWritePrinter)GetProcAddress(hLibHandle, "NWritePrinter");
		m_lpNCheckOnline 		= (lpNCheckOnline)GetProcAddress(hLibHandle, "NCheckOnline");
		m_lpNDataConvert 		= (lpNDataConvert)GetProcAddress(hLibHandle, "NDataConvert");
		m_lpNReadPrinter    	= (lpNReadPrinter)GetProcAddress(hLibHandle, "NReadPrinter");
		m_lpNDeletePrinter  	= (lpNDeletePrinter)GetProcAddress(hLibHandle, "NDeletePrinter");
		m_lpNSetUSBProtocol 	= (lpNSetUSBProtocol)GetProcAddress(hLibHandle, "NSetUSBProtocol");

		m_lpNEnumPrintersA 		= (lpNEnumPrintersA)GetProcAddress(hLibHandle, "NEnumPrintersA");
		m_lpNRenamePrinterA		= (lpNRenamePrinterA)GetProcAddress(hLibHandle, "NRenamePrinterA");
		m_lpNGetPrinterInfA		= (lpNGetPrinterInfA)GetProcAddress(hLibHandle, "NGetPrinterInfA");
		m_lpNGetPrinterFromIDA	= (lpNGetPrinterFromIDA)GetProcAddress(hLibHandle, "NGetPrinterFromIDA");
		m_lpNOpenPrinterA 		= (lpNOpenPrinterA)GetProcAddress(hLibHandle, "NOpenPrinterA");
		m_lpNClosePrinterA 		= (lpNClosePrinterA)GetProcAddress(hLibHandle, "NClosePrinterA");
		m_lpNPrintA				= (lpNPrintA)GetProcAddress(hLibHandle, "NPrintA");
		m_lpNDPrintA 			= (lpNDPrintA)GetProcAddress(hLibHandle, "NDPrintA");
		m_lpNImagePrintA 		= (lpNImagePrintA)GetProcAddress(hLibHandle, "NImagePrintA");
		m_lpNImagePrintFA 		= (lpNImagePrintFA)GetProcAddress(hLibHandle, "NImagePrintFA");
		m_lpNBitImagePrintA 	= (lpNBitImagePrintA)GetProcAddress(hLibHandle, "NBitImagePrintA");
		m_lpNBitImagePrintFA	= (lpNBitImagePrintFA)GetProcAddress(hLibHandle, "NBitImagePrintFA");
		m_lpNGetStatusA 		= (lpNGetStatusA)GetProcAddress(hLibHandle, "NGetStatusA");
		m_lpNGetInformationA	= (lpNGetInformationA)GetProcAddress(hLibHandle, "NGetInformationA");
		m_lpNResetPrinterA 		= (lpNResetPrinterA)GetProcAddress(hLibHandle, "NResetPrinterA");
		m_lpNStartDocA 			= (lpNStartDocA)GetProcAddress(hLibHandle, "NStartDocA");
		m_lpNEndDocA 			= (lpNEndDocA)GetProcAddress(hLibHandle, "NEndDocA");
		m_lpNCancelDocA 		= (lpNCancelDocA)GetProcAddress(hLibHandle, "NCancelDocA");
		m_lpNEnumDocA 			= (lpNEnumDocA)GetProcAddress(hLibHandle, "NEnumDocA");
		m_lpNBarcodeA 			= (lpNBarcodeA)GetProcAddress(hLibHandle, "NBarcodeA");
		m_lpNFirmwareDLA 		= (lpNFirmwareDLA)GetProcAddress(hLibHandle, "NFirmwareDLA");
		m_lpNWritePrinterA 		= (lpNWritePrinterA)GetProcAddress(hLibHandle, "NWritePrinterA");
		m_lpNCheckOnlineA 		= (lpNCheckOnlineA)GetProcAddress(hLibHandle, "NCheckOnlineA");
		m_lpNReadPrinterA		= (lpNReadPrinterA)GetProcAddress(hLibHandle, "NReadPrinterA");
		m_lpNDeletePrinterA 	= (lpNDeletePrinterA)GetProcAddress(hLibHandle, "NDeletePrinterA");
		m_lpNSetUSBProtocolA	= (lpNSetUSBProtocolA)GetProcAddress(hLibHandle, "NSetUSBProtocolA");

		// WLAN
		m_lpNInitializeNetwork	= (lpNInitializeNetwork)GetProcAddress(hLibHandle, "NInitializeNetwork");
		m_lpNScanPrinters 		= (lpNScanPrinters)GetProcAddress(hLibHandle, "NScanPrinters");
		m_lpNSetWriteSize 		= (lpNSetWriteSize)GetProcAddress(hLibHandle, "NSetWriteSize");
		m_lpNTCPPortLock 		= (lpNTCPPortLock)GetProcAddress(hLibHandle, "NTCPPortLock");
		m_lpNBufferClear 		= (lpNBufferClear)GetProcAddress(hLibHandle, "NBufferClear");
		m_lpNBlockSend 			= (lpNBlockSend)GetProcAddress(hLibHandle, "NBlockSendSetting");

		m_lpNTCPPortLockA		= (lpNTCPPortLockA)GetProcAddress(hLibHandle, "NTCPPortLockA");
		m_lpNBufferClearA		= (lpNBufferClearA)GetProcAddress(hLibHandle, "NBufferClearA");
		m_lpNBlockSendA 		= (lpNBlockSendA)GetProcAddress(hLibHandle, "NBlockSendSettingA");

		// Callback
		m_lpNSetCallback		= (lpNSetCallback)GetProcAddress(hLibHandle, "NSetCallback");

		ShowMessage("DLL handle OK!");

		// DLL Version
		if (get_file_version(L"NPrinterLib.dll", nmsVer) == TRUE)
		{
			_stprintf_s(strBuf, sizeof(strBuf) / sizeof(TCHAR), TEXT("Version %i.%i.%i.%i"), nmsVer[0], nmsVer[1], nmsVer[2], nmsVer[3]);
			lblLibVersion->Caption = AnsiString(strBuf);
		}
		else
		{
			MessageBox(0, _TEXT("バージョン情報の取得に失敗しました。"), 0, MB_OK);
		}

		fncGetPrinters();

		edtPrintBox->Text = "";
	}
	else
	{
		nmsErr = GetLastError();
		ShowMessage("DLL handle error:" + IntToStr((int)nmsErr));
	}
}

//---------------------------------------------------------------------------
//  Name	: fncGetPrinters
//  Detail	: NEnumPrinters結果をコンボボックスに入れる
//  引数	: なし
//  戻り値	: 0
//---------------------------------------------------------------------------
void TfrmCheck::fncGetPrinters()
{
int		nmsPos;
int		nmsStart = 0;
int		nmuLen = 0;
UINT	i;
char	strTemp2[1024];

	if(m_lpNEnumPrintersA(NULL, &nmuLen) == 0)
	{
		char* strTemp1;
		strTemp1 = (char*)malloc(nmuLen);
		memset(strTemp1, 0, nmuLen);

		m_lpNEnumPrintersA(strTemp1, &nmuLen);

		for(i = nmsStart; i < nmuLen; i++)
		{
			if(strTemp1[i] == ',')
			{
				memset(strTemp2, 0x00, 256);
				memcpy(strTemp2, &strTemp1[nmsStart], i - nmsStart);
				cmbPrinter->Items->Add(strTemp2);

				nmsStart = i + 1;
			}
		}

		memset(strTemp2, 0x00, 256);
		memcpy(strTemp2, &strTemp1[nmsStart], nmuLen - nmsStart);
		cmbPrinter->Items->Add(strTemp2);

		cmbPrinter->ItemIndex = 0;

		free(strTemp1);
	}
}

//---------------------------------------------------------------------------
//  Name	: FormClose
//  Detail	: アプリ終了
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::FormClose(TObject* Sender, TCloseAction &Action)
{
	if(hLibHandle != NULL)
	{
		m_lpNClosePrinters();
		::FreeLibrary(hLibHandle);
	}
}

//---------------------------------------------------------------------------
//  Name	: btnOpenPrinterClick
//  Detail	: NOpenPrinter
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnOpenPrinterClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNOpenPrinter(cmbPrinter->Text.c_str(), 2, &resultCallback);
	}
	else
	{
		nmsRet = m_lpNOpenPrinterA(AnsiString(cmbPrinter->Text).c_str(), 2, &resultCallback);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnEnumPrtClick
//  Detail	: NEnumPrinters
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnEnumPrtClick(TObject* Sender)
{
int nmsRet = 0;
int nmuLen = 0;

	FuncStart();

	if(checkWCharType())
	{
		if((nmsRet = m_lpNEnumPrinters(NULL, &nmuLen)) == 0)
		{
			WCHAR* str = new WCHAR[nmuLen];
			wmemset(str, 0, sizeof(str));

			nmsRet = m_lpNEnumPrinters(str, &nmuLen);

			edtPrtList->Text = str;
			delete [] str;
		}
	}
	else
	{
		if((nmsRet = m_lpNEnumPrintersA(NULL, &nmuLen)) == 0)
		{
			char* str2;
			str2 = (char*)malloc(nmuLen);
			memset(str2, 0, nmuLen);

			nmsRet = m_lpNEnumPrintersA(str2, &nmuLen);

			edtPrtList->Text = str2;

			free(str2);
		}
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnClosePrinterClick
//  Detail	: NClosePrinter
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnClosePrinterClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNClosePrinter(cmbPrinter->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNClosePrinterA(AnsiString(cmbPrinter->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnNClosePrintersClick
//  Detail	: NClosePrinters
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNClosePrintersClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	nmsRet = m_lpNClosePrinters();

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnPrintClick
//  Detail	: NPrint
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnPrintClick(TObject* Sender)
{
int			nmsRet;
AnsiString	strTemp = AnsiString(edtPrintBox->Text);
char*		prtData = strTemp.c_str();

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNPrint(cmbPrinter->Text.c_str(), prtData, AnsiString(edtPrintBox->Text).Length(), NULL);
	}
	else
	{
		nmsRet = m_lpNPrintA(AnsiString(cmbPrinter->Text).c_str(), prtData, AnsiString(edtPrintBox->Text).Length(), NULL);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnDPrintClick
//  Detail	: NDPrint
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnDPrintClick(TObject* Sender)
{
int		nmsRet;
ULONG	nmuLen;
char	strData[2];
//char* strData = AnsiString(edtPrintBox->Text).c_str();

	FuncStart();

	memset(strData, 0x00, 2);

	if(edtDPrintSize->Text.IsEmpty())
	{
		return;
	}

	strData[0] = 0x1B;
	strData[1] = 0x69;
	nmuLen = edtDPrintSize->Text.ToInt();

	if(checkWCharType())
	{
		nmsRet = m_lpNDPrint(cmbPrinter->Text.c_str(), strData, nmuLen, NULL);
	}
	else
	{
		nmsRet = m_lpNDPrintA(AnsiString(cmbPrinter->Text).c_str(), strData, nmuLen, NULL);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnImagePrintClick
//  Detail	: NImagePrint
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnImagePrintClick(TObject* Sender)
{
int		nmsRet;
int		nmsType;
ULONG	nmuWidth;
ULONG	nmuHeight;
HDC		i_bmp = NULL;

	FuncStart();

	sscanf(AnsiString(cmbImageType->Text).c_str(), "%x", &nmsType);

	if((edtImgWidth->Text.IsEmpty()) || (edtImgHeight->Text.IsEmpty()))
	{
		return;
	}

	nmuWidth = edtImgWidth->Text.ToInt();
	nmuHeight = edtImgHeight->Text.ToInt();

	i_bmp = GetDC(NULL);

	if(checkWCharType())
	{
		nmsRet = m_lpNImagePrint(cmbPrinter->Text.c_str(), i_bmp, nmuWidth, nmuHeight, nmsType, NULL);
	}
	else
	{
		nmsRet = m_lpNImagePrintA(AnsiString(cmbPrinter->Text).c_str(), i_bmp, nmuWidth, nmuHeight, nmsType, NULL);
	}

	ReleaseDC(NULL, i_bmp);

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnImagePrintFClick
//  Detail	: NImagePrintF
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnImagePrintFClick(TObject* Sender)
{
int nmsRet;
int nmsType;

	FuncStart();

	sscanf(AnsiString(cmbImageFType->Text).c_str(), "%x", &nmsType);

	if(checkWCharType())
	{
		nmsRet = m_lpNImagePrintF(cmbPrinter->Text.c_str(), AnsiString(edtImageFileName->Text).c_str(), nmsType, NULL);
	}
	else
	{
		nmsRet = m_lpNImagePrintFA(AnsiString(cmbPrinter->Text).c_str(), AnsiString(edtImageFileName->Text).c_str(), nmsType, NULL);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnGetStatusClick
//  Detail	: NGetStatus
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnGetStatusClick(TObject* Sender)
{
int		nmsRet;
DWORD	o_status;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNGetStatus(cmbPrinter->Text.c_str(), &o_status);
	}
	else
	{
		nmsRet = m_lpNGetStatusA(AnsiString(cmbPrinter->Text).c_str(), &o_status);
	}

	edtStatus->Text = o_status;

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnGetInformationClick
//  Detail	: NGetInformation
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnGetInformationClick(TObject* Sender)
{
int				nmsRet;
int				n;
DWORD			o_time = 0;
BYTE			i_id = cmbInfoNo->Text.ToInt();
char			o_dat[65536];
UnicodeString	strTemp;

	FuncStart();

	memset(o_dat, 0, 65536);

	if(checkWCharType())
	{
		nmsRet = m_lpNGetInformation(cmbPrinter->Text.c_str(), i_id, o_dat, &o_time);
	}
	else
	{
		nmsRet = m_lpNGetInformationA(AnsiString(cmbPrinter->Text).c_str(), i_id, o_dat, &o_time);
	}

	edtInfoRet->Text = o_dat;
	edtInfoHex->Text = "";

	if(i_id == 41)  //ログの場合
	{
		FILE *file = fopen("Log.txt", "w");
		if (file == NULL) {
			printf("ファイルをオープンできませんでした。\n");
		}
		else
		{
			for(n = 0; n < 65536; n++)
			{
				strTemp = UnicodeString::IntToHex((BYTE)o_dat[n], 2) + " ";
				fwprintf(file, strTemp.c_str());
				if((n+1) % 16 == 0)
				{
					fwprintf(file, L"\n");
				}
			}
			fclose(file);
		}
	}
	else
	{
		for(n = 0; n < 32; n++)
		{
			strTemp = UnicodeString::IntToHex((BYTE)o_dat[n], 2);
			edtInfoHex->Text = edtInfoHex->Text + strTemp;
			edtInfoHex->Text = edtInfoHex->Text + " ";
		}
	}

	edtTime->Text = o_time;

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnResetPrinterClick
//  Detail	: NResetPrinter
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnResetPrinterClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNResetPrinter(cmbPrinter->Text.c_str(), NULL);
	}
	else
	{
		nmsRet = m_lpNResetPrinterA(AnsiString(cmbPrinter->Text).c_str(), NULL);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnStartDocClick
//  Detail	: NStartDoc
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnStartDocClick(TObject* Sender)
{
int		nmsRet;
DWORD	o_jobid = 0;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNStartDoc(cmbPrinter->Text.c_str(), &o_jobid);
	}
	else
	{
		nmsRet = m_lpNStartDocA( AnsiString(cmbPrinter->Text).c_str(), &o_jobid);
	}

	edtStartDocNo->Text = AnsiString(o_jobid);

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnEndDocClick
//  Detail	: NEndDoc
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnEndDocClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNEndDoc(cmbPrinter->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNEndDocA(AnsiString(cmbPrinter->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnCancelDocClick
//  Detail	: NCancelDoc
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnCancelDocClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNCancelDoc(cmbPrinter->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNCancelDocA(AnsiString(cmbPrinter->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnFWDLClick
//  Detail	: NFirmwareDL
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnFWDLClick(TObject* Sender)
{
int		nmsRet;
int		nmsMode;
WORD	io_chksum;
DWORD	io_jobid;

	FuncStart();

	sscanf(AnsiString(cmbErrChkMode->Text).c_str(), "%x", &nmsMode);

	if(checkWCharType())
	{
		nmsRet = m_lpNFirmwareDL(cmbPrinter->Text.c_str(), edtFWFFilename->Text.c_str(), nmsMode, &io_chksum, &io_jobid);
	}
	else
	{
		nmsRet = m_lpNFirmwareDLA(AnsiString(cmbPrinter->Text).c_str(), AnsiString(edtFWFFilename->Text).c_str(), nmsMode, &io_chksum, &io_jobid);
	}

	edtCheckSum->Text = IntToStr(io_chksum);

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnFWCHKClick
//  Detail	: NFirmwareDL（チェックサム取得）
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnFWCHKClick(TObject* Sender)
{
int		nmsRet;
WORD	io_chksum = 0x0000;
DWORD	io_jobid;

	if(edtCheckSum->Text == "")
	{
		return;
	}

	FuncStart();

	sscanf(AnsiString(edtCheckSum->Text).c_str(), "%d", (int*)&io_chksum);

	if(checkWCharType())
	{
		nmsRet = m_lpNFirmwareDL(cmbPrinter->Text.c_str(), NULL, 0x01, &io_chksum, &io_jobid);
	}
	else
	{
		nmsRet = m_lpNFirmwareDLA(AnsiString(cmbPrinter->Text).c_str(), NULL, 0x01, &io_chksum, &io_jobid);
	}

	FuncEnd(nmsRet);
}

unsigned char *SJIStoUTF8(char *szSJIS, char *bufUTF8, int size)
{
	wchar_t bufUnicode[8000];
	int lenUnicode = MultiByteToWideChar(CP_ACP, 0, szSJIS, strlen(szSJIS)+1, bufUnicode, 8000);
	WideCharToMultiByte(CP_UTF8, 0, bufUnicode, lenUnicode, bufUTF8, size, NULL, NULL);
	return (unsigned char*)bufUTF8;
}

//---------------------------------------------------------------------------
//  Name	: btnBarcodeClick
//  Detail	: NBarcode
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnBarcodeClick(TObject* Sender)
{
int		nmsRet;
int		nmsType;
ULONG	nmuX;
ULONG	nmuY;
ULONG	nmuWidth;
ULONG	nmuHeight;
int 	nmuLen;
HDC		hdc = NULL;
HBITMAP	hbitmap;
char buf[8001];
unsigned char *szUTF8 = SJIStoUTF8(AnsiString(edtNBarcodeData->Text).c_str(), buf, 8000);  //入力文字が日本語の場合SHIFT-Jisで取り扱われているのでUTF-8に変換して動作を確認する用

	if(edtNBarcodeX->Text.Length() == 0)
	{
		nmuX = NULL;
	}
	else
	{
		nmuX = edtNBarcodeX->Text.ToInt();
	}

	if(edtNBarcodeY->Text.Length() == 0)
	{
		nmuY = NULL;
	}
	else
	{
		nmuY = edtNBarcodeY->Text.ToInt();
	}

	if(edtNBarcodeWidth->Text.Length() == 0)
	{
		nmuWidth = NULL;
	}
	else
	{
		nmuWidth = edtNBarcodeWidth->Text.ToInt();
	}

	if(edtNBarcodeHeight->Text.Length() == 0)
	{
		nmuHeight = NULL;
	}
	else
	{
		nmuHeight = edtNBarcodeHeight->Text.ToInt();
	}

	if(nmuWidth != NULL && nmuHeight != NULL)
	{
		fncMakeHDC(&hdc, &hbitmap, nmuWidth, nmuHeight);
	}

    sscanf(AnsiString(cmbBarcodeType->Text).c_str(), "%x", &nmsType);


	//バイト数計算
	if(chkUTF8->Checked == TRUE)
	{
		nmuLen = strlen((char*)szUTF8);
	}
	else
	{
		nmuLen = strlen(AnsiString(edtNBarcodeData->Text).c_str());
	}




	// 実行
	if(chkUTF8->Checked == TRUE)
	{
		nmsRet = m_lpNBarcode2(hdc, nmuX, nmuX, &nmuWidth, &nmuHeight, szUTF8, nmuLen);
	}
	else
	{
		nmsRet = m_lpNBarcode2(hdc, nmuX, nmuY, &nmuWidth, &nmuHeight, (unsigned char*)AnsiString(edtNBarcodeData->Text).c_str(), nmuLen);
	}

	if(nmsRet == 0)
	{
		nmsRet = m_lpNImagePrint(cmbPrinter->Text.c_str(), hdc, nmuWidth, nmuHeight, nmsType, NULL);
	}


	if(nmuWidth != NULL && nmuHeight != NULL)
	{
		fncDelHDC(hdc, hbitmap);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnAutoOpenClick
//  Detail	: NAutoOpen
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnAutoOpenClick(TObject* Sender)
{
	FuncStart();

	if(m_lpNAutoOpen(StrToInt(cmbAuto->Text)))
	{
		edtResult->Text = "有効";
	}
	else
	{
		edtResult->Text = "無効";
	}

	FuncEnd(0);
}

//---------------------------------------------------------------------------
//  Name	: btnNScanPrintersClick
//  Detail	: NScanPrinters
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNScanPrintersClick(TObject* Sender)
{
int		nmsRet;
DWORD	i_waitmsec;

	if(EdtScanTime->Text.IsEmpty())
	{
		return;
	}

	i_waitmsec = EdtScanTime->Text.ToInt();

	nmsRet = m_lpNScanPrinters(i_waitmsec);

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnNTCPPortLockClick
//  Detail	: NTCPPortLock
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNTCPPortLockClick(TObject* Sender)
{
int		nmsRet;
BYTE	i_type;

	if(cmbPortLockItem->Text == "LOCK")
	{
		i_type = 1;
	}
	else if(cmbPortLockItem->Text == "UNLOCK")
	{
		i_type = 0;
	}
	else
	{
		i_type = 2;
	}

	if(checkWCharType())
	{
		nmsRet = m_lpNTCPPortLock(cmbPrinter->Text.c_str(), i_type);
	}
	else
	{
		nmsRet = m_lpNTCPPortLockA(AnsiString(cmbPrinter->Text).c_str(), i_type);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnNBufferClearClick
//  Detail	: NBufferClear
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNBufferClearClick(TObject* Sender)
{
int nmsRet;

	if(checkWCharType())
	{
		nmsRet = m_lpNBufferClear(cmbPrinter->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNBufferClearA(AnsiString(cmbPrinter->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnNBlockSendClick
//  Detail	: NBlockSendSetting
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNBlockSendClick(TObject* Sender)
{
int		nmsRet;
BYTE	i_type;

	if(cmbBlockType->Text == "OFF")
	{
		i_type = 0;
	}
	else if(cmbBlockType->Text == "ON")
	{
		i_type = 1;
	}
	else
	{
		i_type = 2;
	}

	if(checkWCharType())
	{
		nmsRet = m_lpNBlockSend(cmbPrinter->Text.c_str(), i_type);
	}
	else
	{
		nmsRet = m_lpNBlockSendA(AnsiString(cmbPrinter->Text).c_str(), i_type);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnNGetPrinterInfClick
//  Detail	: NGetPrinterInf
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNGetPrinterInfClick(TObject* Sender)
{
int		nmsRet;
DWORD	o_size;
CHAR	o_ports2[255];
WCHAR	o_ports[255];

	edtPrtInfo->Text = "";

	if(checkWCharType())
	{
		memset(o_ports, 0x00, sizeof(o_ports));

		if((nmsRet = m_lpNGetPrinterInf(cmbPrinter->Text.c_str(), o_ports, &o_size)) == 0)
		{
			edtPrtInfo->Text = UnicodeString(o_ports);
		}
	}
	else
	{
		memset(o_ports2, 0x00, sizeof(o_ports2));

		if((nmsRet = m_lpNGetPrinterInfA(AnsiString(cmbPrinter->Text).c_str(), o_ports2, &o_size)) == 0)
		{
			edtPrtInfo->Text = UnicodeString(o_ports2);
		}
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnDeletePrinterClick
//  Detail	: NDeletePrinter
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnDeletePrinterClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNDeletePrinter(edtPrtName2->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNDeletePrinterA(AnsiString(edtPrtName2->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnRenamePrinterClick
//  Detail	: NRenamePrinter
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnRenamePrinterClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNRenamePrinter(cmbPrinter->Text.c_str(), edtPrtName2->Text.c_str());
	}
	else
	{
		nmsRet = m_lpNRenamePrinterA(AnsiString(cmbPrinter->Text).c_str(), AnsiString(edtPrtName2->Text).c_str());
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnInitializeNetworkClick
//  Detail	: NInitializeNetwork
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnInitializeNetworkClick(TObject* Sender)
{
int nmsRet;

	if(cmbUDPInit->Text == "ON")
	{
		nmsRet = m_lpNInitializeNetwork(1);
	}
	else if(cmbUDPInit->Text == "OFF")
	{
		nmsRet = m_lpNInitializeNetwork(0);
	}
	else
	{
		nmsRet = m_lpNInitializeNetwork(2);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnSetUSBProtocolClick
//  Detail	: NSetUSBProtocol
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnSetUSBProtocolClick(TObject* Sender)
{
int nmsRet;
int nmsType;

	sscanf(AnsiString(cmbUSBPtl->Text).c_str(), "%d", &nmsType);

	if(checkWCharType())
	{
		nmsRet = m_lpNSetUSBProtocol(cmbPrinter->Text.c_str(), nmsType);
	}
	else
	{
		nmsRet = m_lpNSetUSBProtocolA(AnsiString(cmbPrinter->Text).c_str(), nmsType);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: btnFromIDClick
//  Detail	: NGetPrinterFromID
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnFromIDClick(TObject* Sender)
{
int nmsRet;

	FuncStart();

	if(checkWCharType())
	{
		WCHAR*	strWRet = new WCHAR[255];
		memset(strWRet, 0, 255);

		nmsRet = m_lpNGetPrinterFromID(edtFromID->Text.c_str(), strWRet);

		edtFromIDResult->Text = strWRet;

		delete [] strWRet;
	}
	else
	{
		CHAR strRet[255];
		memset(strRet, 0x00, 255);

		nmsRet = m_lpNGetPrinterFromIDA(AnsiString(edtFromID->Text).c_str(), strRet);

		edtFromIDResult->Text = strRet;
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: checkWCharType
//  Detail	: 文字タイプ設定
//  引数	: なし
//  戻り値	: true / false
//---------------------------------------------------------------------------
bool checkWCharType()
{
	if(frmCheck->cmbChar->Text == "PWCHAR")
	{
		return true;
	}

	return false;
}

//---------------------------------------------------------------------------
//  Name	: btnNCallbackClick
//  Detail	: NCallbackをライブラリにセット
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnNCallbackClick(TObject* Sender)
{
	if(cmbCallback->Text == "ON")
	{
		m_lpNSetCallback(&resultCallback);
	}
	else
	{
		m_lpNSetCallback(NULL);
	}
}

//---------------------------------------------------------------------------
//  Name	: FuncStart
//  Detail	: ライブラリ関数動作中の画面表示
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void FuncStart()
{
	frmCheck->edtResult->Text = "処理中";
	frmCheck->edtResult->Color = clYellow;
	Application->ProcessMessages();
}

//---------------------------------------------------------------------------
//  Name	: FuncEnd
//  Detail	: ライブラリ関数動作後の画面表示
//  引数	:
//				nRet	：動作結果（エラーコード等）
//  戻り値	: なし
//---------------------------------------------------------------------------
void FuncEnd(int nmsRet)
{
	frmCheck->edtResult->Text = nmsRet;
	frmCheck->edtResult->Color = clWhite;
}

//---------------------------------------------------------------------------
//  Name	: CallbackThread
//  Detail	: 非同期書き込み処理スレッド
//  引数	:	lpParameter	:コールバック文字列ポインタ
//  戻り値:
//---------------------------------------------------------------------------
DWORD WINAPI CallbackThread(LPVOID lpParameter)
{
	frmCheck->lstBoxCallback->Items->Add((char*)lpParameter);
	frmCheck->lstBoxCallback->ItemIndex = frmCheck->lstBoxCallback->Items->Count-1;
	free((char*)lpParameter);
    return 0;
}

//---------------------------------------------------------------------------
//  Name	: resultCallback
//  Detail	: コールバック内容を画面表示
//  引数	:
//				i_prt	：対象プリンタ名
//				type	：対象動作種別
//				result1	：結果１
//				result2	：結果２
//  戻り値	: なし
//---------------------------------------------------------------------------
void __stdcall resultCallback(PCHAR i_prt, int type, int result1, int result2)
{
TDateTime	date = Now();
AnsiString	tmpDate = date.FormatString("yyyy/MM/dd HH:mm:ss.zzz");
char* strLog;

	strLog = (char*)malloc(500);
	memset(strLog,0,500);
	if(i_prt == NULL)
	{
		sprintf_s(strLog,500,"%s [NULL] Type:%d  Result[%d,%d]", tmpDate.c_str(), type,result1,result2);
	}
	else
	{
		sprintf_s(strLog,500,"%s [%s] Type:%d  Result[%d,%d]", tmpDate.c_str(), i_prt,type,result1,result2);
	}
	CreateThread(NULL, 0, CallbackThread, (LPDWORD)strLog, 0, NULL);
}

//---------------------------------------------------------------------------
//  Name	: btnCallbackClearClick
//  Detail	: コールバック表示クリア
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnCallbackClearClick(TObject* Sender)
{
	lstBoxCallback->Items->Clear();
}

//---------------------------------------------------------------------------
//  Name	: btnSampleClick
//  Detail	: レシートサンプル印字
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnSampleClick(TObject* Sender)
{
int			nmsRet;
char*		prtData;
AnsiString	str;

	FuncStart();

	str = "1d\"P\"00"
		  "0a"
		  "1C\"C\"01"
		  "1C\"!\"00"
		  "\"日本プリンタエンジニアリング(株)\"0a"
		  "\"山梨県南都留郡富士河口湖町勝山２６６０\"0a"
		  "1b\"a\"02"
		  "\"電話：0555-83-5678     ﾚｼﾞ#1\"0a"
		  "0a"
		  "1b\"a\"00"
		  "\"2014年12月26日（金）13:35　責017\"0a"
		  "1b\"a\"01"
		  "1C\"!\"06 1b\"3\"00"
		  "\"┏━━━━━┓\"0a"
		  "\"┃    領 収 書    ┃\"0a"
		  "\"┗━━━━━┛\"0a"
		  "1b\"3\"11"
		  "0a"
		  "1b\"3\"22"
		  "1C\"!\"00"
		  "\"NEX-M2302              \\25,000\"0a"
		  "1C\"!\"06 1b\"!\"20"
		  "\"合　計　\\25,000\"0a"
		  "1C\"!\"00 1b\"!\"00"
		  "\"（内消費税等　　　　　\\1,852)\"0a"
		  "\"お買上明細は上記のとおりです。\"0a"
		  "\"商品価格には消費税等を含みます。\"0a"
		  "0a"
		  "1b\"a\"01"
		  "1d\"P\"01"
		  "0a0a0a0a 1b69"  ;

	prtData = str.c_str();

	if(checkWCharType())
	{
		nmsRet = m_lpNPrint(cmbPrinter->Text.c_str(), prtData, str.Length(), NULL);
	}
	else
	{
		nmsRet = m_lpNPrintA(AnsiString(cmbPrinter->Text).c_str(), prtData, str.Length(), NULL);
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: Button2Click
//  Detail	: デバッグ用テスト印字（ドライバのみ）
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::Button2Click(TObject* Sender)
{
int			nmsRet;
int			fx, fy;
int 		ax, ay;
int 		cnt;
int 		x, y;
DWORD		endstamp;
DWORD		nowstamp;
DWORD		newstamp;
BYTE		pi[64];
AnsiString	dat;
AnsiString	wk;

	dat = "1B7407";

	m_lpNPrint(L"NPI Integration Driver", dat.c_str(), 6, NULL);

	dat = "";

	//BMP->BINデータ変換
	fx = 12;
	fy = 24;
	dat = String("1d\"&\"00");

	for(ay = 0; ay < fy*14; ay += fy)
	{
		for(ax = 0; ax < fx*16; ax += fx)
		{
			wk = "";

			if(fx == 9)
			{
				for(cnt = 0; cnt < 7; cnt++)
				{
					dat = dat + "0000";
				}
			}

			for(y = 0; y < fy; y++)
			{
				for(x = 0; x < fx; x++)
				{
					wk = wk + "1";
				}
				if(fx ==  12)
				{
					wk = wk + "0000";
				}
				if(fx ==  9)
				{
					wk = wk + "0000000";
				}
			}

			wk = SBinToSHex(wk);
			dat = dat + wk;
		}

		nmsRet = m_lpNPrint(L"NPI Integration Driver", dat.c_str(), dat.Length(), NULL);

		dat = "";
	}

	if(nmsRet == 0)
	{
		ShowMessage(L"ジョブが正常に生成されました");
	}
	else
	{
		ShowMessage(L"ジョブの生成に失敗しました");
	}
}

//---------------------------------------------------------------------------
//  Name	: btnUDPInfoRequestClick
//  Detail	: 指定IPアドレスへ拡張情報要求
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnUDPInfoRequestClick(TObject* Sender)
{
int					sendSize;
int					infoNo = StrToInt(cmbInfoNo->Text);
int					subID;
bool				yes = 1;
char				sendBuf[20];
char				extendNoBuf[2];
char				subIDBuf[2];
SOCKET				sock;
struct sockaddr_in	addr;

	memset(extendNoBuf, 0x00, sizeof(extendNoBuf));
	memset(subIDBuf, 0x00, sizeof(subIDBuf));
	memset(sendBuf, 0x00, sizeof(sendBuf));

	// Header
	sendBuf[0] = 0x10;
	sendBuf[1] = 0x16;
	sendBuf[2] = 0x01;
	sendBuf[3] = 0x80;      // 1000 0000

	// Query
	sendBuf[4] = 0x31;
	sendBuf[5] = 0x32;
	sendBuf[6] = 0x4E;

	BinToHEX(extendNoBuf, (BYTE*)&infoNo, 1);
	memcpy(&sendBuf[7], extendNoBuf, 2);

	if((infoNo >= 32 && infoNo <= 34) || infoNo == 11)
	{
		subID = StrToInt(cmbSubID->Text);
		BinToHEX(subIDBuf, (BYTE*)&subID, 1);
		memcpy(&sendBuf[9], subIDBuf, 2);
		sendSize = 12;
	}
	else
	{
		sendBuf[9] = 0x00;
		sendSize = 10;
	}

	sock = socket(AF_INET, SOCK_DGRAM, 0);

	addr.sin_family = AF_INET;
	addr.sin_port = htons(10001);

	addr.sin_addr.S_un.S_addr = inet_addr(((AnsiString)edtUDPAddr->Text).c_str());

	setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof(yes));

	sendto(sock, sendBuf, sendSize, 0, (struct sockaddr *)&addr, sizeof(addr));
}

//---------------------------------------------------------------------------
//  Name	: btnStatusLoopClick
//  Detail	: ステータス自動取得開始
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnStatusLoopClick(TObject* Sender)
{
	if(timerStatus->Enabled)
	{
		timerStatus->Enabled = false;
		btnStatusLoop->Caption = "Cheking stopped";
	}
	else
	{
		timerStatus->Enabled = true;
		btnStatusLoop->Caption = "Checking...";
	}
}

//---------------------------------------------------------------------------
//  Name	: btnRenzokuClick
//  Detail	: 連続印字開始
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnRenzokuClick(TObject* Sender)
{
	if(Timer2->Enabled)
	{
		btnRenzoku->Caption = "Printout suspended";
		Timer2->Enabled = false;
	}
	else
	{
		currentTimes = 0;
		prtMaxTimes = StrToInt(edtRenzokuTimes->Text);
		prtLines = StrToInt(edtRenzokuLines->Text);
		btnRenzoku->Caption = "Printing...";
		Timer2->Enabled = true;
	}
}

//---------------------------------------------------------------------------
//  Name	: timerStatusTimer
//  Detail	: btnStatusLoopClickで開始した連続ステータス取得実行
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::timerStatusTimer(TObject* Sender)
{
int		nmsRet;
DWORD	o_status;

	FuncStart();

	if(checkWCharType())
	{
		nmsRet = m_lpNGetStatus(cmbPrinter->Text.c_str(), &o_status);
	}
	else
	{
		nmsRet = m_lpNGetStatusA( AnsiString(cmbPrinter->Text).c_str(), &o_status);
	}

	edtStatus->Text = o_status;

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: Timer2Timer
//  Detail	: btnRenzokuClickで開始した連続印字実行
//  引数	: なし
//  戻り値	: なし
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::Timer2Timer(TObject* Sender)
{
int			nmsRet;
int			n;
int			nmsInch;
bool		flgHData = true;
char		str[100];
char*		prtData;
AnsiString	strData = "";
AnsiString	printData = "";
String		FEED			= "1B4A60";
String		FULLCUT			= "1B69";
String		DATA_2INCH_MEGA	= "\"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\"0A";
String		DATA_3INCH_MEGA	= "\"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\"0A";
String		strInch = "";

//	if(cmbInch->ItemIndex == 0)
//	{
//		strInch = " HHHHHHH\"0A";
//	}
//	else
//	{
//		strInch = "\"0A";
//	}

	if(cmbInch->Text == "2inch")
	{
		nmsInch = 2;
	}
	else
	{
		nmsInch = 3;
	}

	if(cmbHData->Text == "Date")
	{
		flgHData = false;
	}

	strInch = "\"0A";

	if(flgHData)
	{
		printData = "1B2102";
	}
	else
	{
		TDateTime date = Now();
		AnsiString tmpDate = date.FormatString("[yyyy/MM/dd HH:mm:ss.zzz]");

		strData = "\"";
		strData += tmpDate;
		strData += " Count:";

		memset(str, 0x00, sizeof(str));
		sprintf(str, "%06d",(currentTimes + 1));

		strData += str;
		strData += " Line:";
	}

	for(n = 0; n < prtLines; n++)
	{
		if(flgHData)
		{
			if(nmsInch == 3)
			{
				printData += DATA_3INCH_MEGA;
			}
			else
			{
				printData += DATA_2INCH_MEGA;
			}
		}
		else
		{
			printData += strData;

			memset(str, 0x00, sizeof(str));
			sprintf(str, "%03d",(n+1));

			printData += str;
			printData += strInch;
		}
	}

	printData += FEED;

	if(cmbCut->ItemIndex == 1)
	{
		printData += FULLCUT;
	}

	FuncStart();

	prtData = printData.c_str();

	if(checkWCharType())
	{
		nmsRet = m_lpNPrint(cmbPrinter->Text.c_str(), prtData, printData.Length(), NULL);
	}
	else
	{
		nmsRet = m_lpNPrintA(AnsiString(cmbPrinter->Text).c_str(), prtData, printData.Length(), NULL);
	}

	currentTimes++;
	edtRenzokuCurrent->Text = IntToStr(currentTimes);

	Timer2->Interval = StrToInt(edtRenzokuInterval->Text);

	if(prtMaxTimes != 0 && currentTimes >= prtMaxTimes)
	{
		// 設定した印字回数に達した
		currentTimes = 0;
		btnRenzoku->Caption = "Printout suspended";
		Timer2->Enabled = false;
	}

	FuncEnd(nmsRet);
}

//---------------------------------------------------------------------------
//  Name	: get_file_version
//  Detail	: ライブラリバージョン取得
//  引数	:
//				i_path	：ライブラリパス
//				o_ver	：ライブラリバージョン
//  戻り値	: true / false
//---------------------------------------------------------------------------
bool get_file_version(TCHAR* i_path, int o_ver[4])
{
UINT				nmuLen;
DWORD				dwZero = 0;
DWORD				sz = GetFileVersionInfoSize(i_path, &dwZero);
VS_FIXEDFILEINFO*	v;

	if(sz)
	{
		char* buf = new char[sz];
		GetFileVersionInfo(i_path, dwZero, sz, buf);

		if(VerQueryValue(buf, TEXT("\\"), (LPVOID*)&v, &nmuLen))
		{
			o_ver[0] = int(HIWORD(v->dwFileVersionMS));
			o_ver[1] = int(LOWORD(v->dwFileVersionMS));
			o_ver[2] = int(HIWORD(v->dwFileVersionLS));
			o_ver[3] = int(LOWORD(v->dwFileVersionLS));
			delete buf;
			return TRUE;
		}
		else
		{
			delete buf;
			return FALSE;
		}
	}
	else
	{
		return FALSE;
	}
}

//---------------------------------------------------------------------------
//  Name	: BinToHEX
//  Detail	: 16進数変換
//  引数	:
// 				o_data	：変換後文字列
// 				i_data	：変換前数値
// 				i_size	：変換サイズ
//  戻り値	: なし
//---------------------------------------------------------------------------
void BinToHEX(char* o_data, BYTE* i_data, SHORT i_size)
{
SHORT posi;

	for(posi = 0; posi < i_size; posi++)
	{
		o_data[posi*2] = ((i_data[posi] >> 4) & 0x0F) + 0x30;
		o_data[posi*2+1] = (i_data[posi] & 0x0F) + 0x30;

		// 入力データが英字の場合は調整する
		if(o_data[posi*2] > 0x39)
		{
			o_data[posi*2]+=0x07;
		}

		if(o_data[posi*2+1] > 0x39)
		{
			o_data[posi*2+1]+=0x07;
		}
	}
}

//---------------------------------------------------------------------------
//  Name	: SBinToSHex
//  Detail	: 16進数変換
//  引数	: i_str	：変換前文字列
//  戻り値	: 16進文字列
//---------------------------------------------------------------------------
String SBinToSHex(String i_str)
{
int		i;
UCHAR  	wk;
String	strRet;

	strRet = "";
	wk = NULL;

	for(i = 0; i < i_str.Length(); i++)
	{
		wk = UCHAR(wk | ((i_str[i+1] - 0x30) << (7-i%8)));

		if(i % 8 == 7)
		{
			strRet = strRet + IntToHex(int(wk), 2);
			wk = NULL;
		}
	}

	return(strRet);
}

//---------------------------------------------------------------------------
//  Name	: fncMakeHDC
//  Detail	: 画像コンテキスト作成
//  引数	:
//      o_hdc		：画像コンテキスト
//      o_hBitmap	：
//      i_width		：画像幅
//      i_height	：画像高さ
//  戻り値	: 0
//---------------------------------------------------------------------------
int fncMakeHDC(HDC* o_hdc, HBITMAP* o_hBitmap, int i_width, int i_height)
{
BITMAPINFO	bmpInfo;
LPDWORD		lpPixel;
HBRUSH		hBrush, hOldBrush;
RECT		lprc[1];

	//DIBの情報を設定する
	bmpInfo.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth       = i_width;
	bmpInfo.bmiHeader.biHeight      = i_height;
	bmpInfo.bmiHeader.biPlanes      = 1;
	bmpInfo.bmiHeader.biBitCount    = 24;
	bmpInfo.bmiHeader.biCompression = BI_RGB;

	//DIBSection作成
	*o_hBitmap = CreateDIBSection(NULL, &bmpInfo, DIB_RGB_COLORS, (void**)&lpPixel, NULL, 0);	//ビットマップ作成
	*o_hdc     = CreateCompatibleDC(NULL);														//ビットマップ描画用DC作成
	SelectObject(*o_hdc, *o_hBitmap);															//ビットマップをDCに選択

	//白に領域塗潰し
	lprc[0].left   = 0;
	lprc[0].top    = 0;
	lprc[0].right  = i_width;
	lprc[0].bottom = i_height;
	hBrush    = CreateSolidBrush(RGB(255,255,255));
	hOldBrush = (HBRUSH)SelectObject(*o_hdc, hBrush);
	FillRect(*o_hdc, &lprc[0], hBrush);
	SelectObject(*o_hdc, hOldBrush);
	DeleteObject(hBrush);

	return(0);
}

//---------------------------------------------------------------------------
//  Name	: fncDelHDC
//  Detail	: 画像コンテキスト削除
//  引数	:
//      io_hdc		：画像コンテキスト
//      io_hBitmap	：
//  戻り値	: 0
//---------------------------------------------------------------------------
int fncDelHDC(HDC io_hdc, HBITMAP io_hbitmap)
{
	//自らlpPixelを解放するべからず
	DeleteDC(io_hdc);
	DeleteObject(io_hbitmap);  //BMPを削除した時、lpPixelも自動的に解放される
	io_hdc = NULL;
	io_hbitmap = NULL;

	return(0);
}

void __fastcall TfrmCheck::btnFreeLibraryClick(TObject *Sender)
{
	if(hLibHandle != NULL)
	{
		::FreeLibrary(hLibHandle);
	}
}
//---------------------------------------------------------------------------
void __fastcall TfrmCheck::btnBarcodeSettingsClick(TObject *Sender)
{
int nmsRet;
UINT BarcodeID = NULL;
UINT Width = NULL;
UINT Height = NULL;
CHAR Option1 = NULL;
CHAR Option2 = NULL;
CHAR Option3 = NULL;
CHAR Option4 = NULL;
UINT HRI = NULL;
UINT Spin = NULL;
UINT Code = NULL;

	FuncStart();

	BarcodeID = cmbSettingBarcodeID->ItemIndex;
	if(cmbSettingBarcodeID->ItemIndex >= 23)
	{
		BarcodeID += 7;
	}

	if(edtSettingWidth->Text.Length() != 0)
	{
		Width = edtSettingWidth->Text.ToInt();
	}

	if(edtSettingHeight->Text.Length() != 0)
	{
		Height = edtSettingHeight->Text.ToInt();
	}

	if(edtSettingOption1->Text.Length() != 0)
	{
		Option1 = StrToInt(edtSettingOption1->Text);
	}
	if(edtSettingOption2->Text.Length() != 0)
	{
		Option2 = StrToInt(edtSettingOption2->Text);
	}
	if(edtSettingOption3->Text.Length() != 0)
	{
		Option3 = StrToInt(edtSettingOption3->Text);
	}
	if(edtSettingOption4->Text.Length() != 0)
	{
		Option4 = StrToInt(edtSettingOption4->Text);
	}

	HRI = cmbSettingHRI->ItemIndex;
	Spin = cmbSettingSpin->ItemIndex;
	Code = cmbSettingCode->ItemIndex;

	nmsRet = m_lpNBarcodeSettings(BarcodeID,Width,Height,Option1,Option2,Option3,Option4,HRI,Spin,Code);
	FuncEnd(nmsRet);
}
//---------------------------------------------------------------------------

void __fastcall TfrmCheck::btnSampleDataClick(TObject *Sender)
{
	if(cmbSettingBarcodeID->ItemIndex == 0)
	{
		edtNBarcodeData->Text = "12345678901";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 1)
	{
		edtNBarcodeData->Text = "1234567";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 2)
	{
		edtNBarcodeData->Text = "123456789012";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 3)
	{
		edtNBarcodeData->Text = "1234567";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 4)
	{
		edtNBarcodeData->Text = "*ABC123$% +-./*";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 5)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 6)
	{
		edtNBarcodeData->Text = "A1234567890A";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 7)
	{
		edtNBarcodeData->Text = "{A123{C123";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 8)
	{
		edtNBarcodeData->Text = "123ABC$% +-./!";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 9)
	{
		edtNBarcodeData->Text = "9781234567897";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 10)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 11)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 12)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 13)
	{
		edtNBarcodeData->Text = "1234567890ABCDE";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 14)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 15)
	{
		edtNBarcodeData->Text = "[26]ABC!\"#$%&'()";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 16)
	{
		edtNBarcodeData->Text = "1234567890123";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 17)
	{
		edtNBarcodeData->Text = "1234567890123";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 18)
	{
		edtNBarcodeData->Text = "1234567890123";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 19)
	{
		edtNBarcodeData->Text = "[26]11234567890123";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 20)
	{
		edtNBarcodeData->Text = "*ABC123$% +-./*";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 21)
	{
		edtNBarcodeData->Text = "12345ABCDE";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 22)
	{
		edtNBarcodeData->Text = "12345ABCDE";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 23)
	{
		edtNBarcodeData->Text = "QR Code";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 24)
	{
		edtNBarcodeData->Text = "Micro QR";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 25)
	{
		edtNBarcodeData->Text = "Aztec Code";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 26)
	{
		edtNBarcodeData->Text = "PDF417";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 27)
	{
		edtNBarcodeData->Text = "Micro PDF417";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 28)
	{
		edtNBarcodeData->Text = "MaxiCode";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 29)
	{
		edtNBarcodeData->Text = "DataMatrix";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 30)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 31)
	{
		edtNBarcodeData->Text = "1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 32)
	{
		edtNBarcodeData->Text = "[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 33)
	{
		edtNBarcodeData->Text = "1234567|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 34)
	{
		edtNBarcodeData->Text = "123456789012|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 35)
	{
		edtNBarcodeData->Text = "12345678901|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 36)
	{
		edtNBarcodeData->Text = "1234567|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 37)
	{
		edtNBarcodeData->Text = "[26]1234567890|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 38)
	{
		edtNBarcodeData->Text = "1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 39)
	{
		edtNBarcodeData->Text = "1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 40)
	{
		edtNBarcodeData->Text = "1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 41)
	{
		edtNBarcodeData->Text = "[26]1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 42)
	{
		edtNBarcodeData->Text = "1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 43)
	{
		edtNBarcodeData->Text = "1234567890123|[26]1234567890";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 44)
	{
		edtNBarcodeData->Text = "[26]1|[26]1";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 45)
	{
		edtNBarcodeData->Text = "[26]123ABC!\"#$%&'()";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 46)
	{
		edtNBarcodeData->Text = "[26]123ABC!\"#$%&'()";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 47)
	{
		edtNBarcodeData->Text = "[26]123ABC!\"#$%&'()";
	}
	else if(cmbSettingBarcodeID->ItemIndex == 48)
	{
		edtNBarcodeData->Text = "QRCode(concatenation)";
	}
}
//---------------------------------------------------------------------------

void __fastcall TfrmCheck::btnEnumDocClick(TObject *Sender)
{
int nmsRet = 0;

	FuncStart();
    memEnumDoc->Text = "";
	if(checkWCharType())
	{
		if((nmsRet = m_lpNEnumDoc(cmbPrinter->Text.c_str(), NULL)) > 0)
		{
			WCHAR* str = new WCHAR[nmsRet];
			wmemset(str, 0, sizeof(str));

			nmsRet = m_lpNEnumDoc(cmbPrinter->Text.c_str(), str);

			memEnumDoc->Text = str;
			delete [] str;
		}
	}
	else
	{
		if((nmsRet = m_lpNEnumDocA(AnsiString(cmbPrinter->Text).c_str(), NULL)) > 0)
		{
			char* str2;
			str2 = (char*)malloc(nmsRet);
			memset(str2, 0, nmsRet);

			nmsRet = m_lpNEnumDocA(AnsiString(cmbPrinter->Text).c_str(), str2);

			memEnumDoc->Text = str2;

			free(str2);
		}
	}

	FuncEnd(nmsRet);
}
//---------------------------------------------------------------------------

