//AccKey.cpp..........
#include "stdafx.h"
#include <stdio.h>
#include "AccKey.h"

SENSE4_CONTEXT * AccessKey()
{
	
	SENSE4_CONTEXT * s4CtxList =NULL ;
	DWORD			dwSize=0;

	//enumerate device number
	S4Enum(NULL,&dwSize);

	//if the dwSize is 0, or is not multiple of sizeof(SENSE4_CONTEXT),
	//no device is found, or something wrong with the S4Enum function.
	if ((0 == dwSize) || (dwSize % sizeof(SENSE4_CONTEXT))) return NULL;

	//allocate the memory according to the return value of S4Enum
	s4CtxList = (SENSE4_CONTEXT *)malloc(dwSize);

	//invoke S4Enum again to get device context
	if(S4Enum(s4CtxList,&dwSize)== S4_SUCCESS)
		printf("Enum device ok! \n");
	else
	{
		printf("Enum device failed.\n"); 
		free(s4CtxList);
		return NULL;
	}

	int i = 7; 
	S4OPENINFO		S4_OpenInfo;
	S4_OpenInfo.dwS4OpenInfoSize = sizeof(S4OPENINFO);
	S4_OpenInfo.dwShareMode = S4_EXCLUSIZE_MODE;
	//open the specified device
	if(S4OpenEx(&s4CtxList[0], &S4_OpenInfo) == S4_SUCCESS)
	{
		//print device ID
		printf("Open device success. Device ID is :");

		do
		{
			printf("%02x",s4CtxList[0].bID[7-i]);
			i--;
		}while (i>=0);

		printf("\n");
	}
	else
	{
		printf("Open device failure.\n");
		free(s4CtxList);
		return NULL;
	}

	return (&s4CtxList[0]);
}