/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_2_get_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to enumerate all the SenseLock EL
*            devices connected to current PC and get their "Device ID"
*            
*            About "Device ID": an 8 octets array preset in SenseLock EL by software devloper
*            that can be used to distinguish devices.
*            
*  History:        
*  --------------------------------------------------------        
*            11/08/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/


#include "sense4.h"
#include "malloc.h"
#include "stdio.h"
#include "conio.h"
int main(int argc, char* argv[])
{
	PSENSE4_CONTEXT pstS4CtxList = NULL;
	DWORD dwCtxListSize = 0;

	DWORD  btDeviceNumber = 0;
	DWORD dwResult = 0;

	DWORD i = 0;

	dwResult = S4Enum(NULL, &dwCtxListSize);
	if (dwResult != S4_SUCCESS && dwResult != S4_INSUFFICIENT_BUFFER)
	{
		printf("Enumerate SenseLock EL failed! <error code: 0x%08x>\n", dwResult);
		return 0;
	}
	if (0 == dwCtxListSize)
	{
		printf("No SenseLock EL device present!\n");
		return 1;
	}

	pstS4CtxList = (PSENSE4_CONTEXT)malloc(dwCtxListSize);

	dwResult = S4Enum(pstS4CtxList, &dwCtxListSize);
	if (S4_SUCCESS != dwResult) 
	{
		printf("S4Enum failed with error code:%d\n",dwResult);
		free(pstS4CtxList);
		pstS4CtxList = NULL;
		return 1;
	}

	btDeviceNumber = dwCtxListSize/sizeof(SENSE4_CONTEXT);
	
	for(i=0; i<btDeviceNumber; i++)
	{

		char buffer[200];

		dwResult = S4Open(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Open for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}
		/* need reset to get the new bID */
		dwResult = S4Control(pstS4CtxList+i,S4_RESET_DEVICE,NULL,0,NULL,0,NULL);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Control(S4_SET_DEVICE_ID) for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}	

		memcpy(buffer, &((pstS4CtxList+i)->bID), 8);
		memcpy(buffer+8, "\0", 1);
		printf("The Customer ID for device No.%d is:%s\n", i, buffer);

		S4Close(pstS4CtxList+i);
	}
	free(pstS4CtxList);
	pstS4CtxList = NULL;
	
	getch();
	return 0;
}










































