Click here to Skip to main content
15,894,539 members
Articles / Programming Languages / C++

An UMDF Driver for a Virtual Smart Card Reader

Rate me:
Please Sign up or sign in to vote.
4.89/5 (70 votes)
30 Jan 2014CPOL23 min read 593.1K   10.5K   104  
A simple implementation of a driver for a virtual smart card reader, based on UMDF
#include "memory.h"
#include "SectionLocker.h"

bool getBuffer(IWDFIoRequest* pRequest,BYTE *buffer,int *bufferLen) {

	IWDFMemory *inmem=NULL;
	pRequest->GetInputMemory(&inmem);
	if (inmem==NULL) {
		OutputDebugString(L"GetInputMemory failed");
		return false;
	}
	else {
		SIZE_T size;
		void *data=inmem->GetDataBuffer(&size);
		memcpy(buffer,data,size);
		(*bufferLen)=(int)size;
		inmem->Release();
		return true;
	}
}
void setBuffer(CMyDevice *device,IWDFIoRequest* pRequest,BYTE *result,int inSize) {
	IWDFMemory *outmem=NULL;
	pRequest->GetOutputMemory (&outmem);
	if (outmem==NULL) {
		SectionLocker lock(device->m_RequestLock);
		OutputDebugString(L"GetOutputMemory failed");
        pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
	}
	else {
		SectionLocker lock(device->m_RequestLock);
		outmem->CopyFromBuffer(0,result,inSize);
		outmem->Release();
        pRequest->CompleteWithInformation(0,(SIZE_T)inSize);
	}
}

void setString(CMyDevice *device,IWDFIoRequest* pRequest,char *result,int outSize) {
	IWDFMemory *outmem=NULL;
	pRequest->GetOutputMemory (&outmem);
	if (outmem==NULL) {
		SectionLocker lock(device->m_RequestLock);
		OutputDebugString(L"GetOutputMemory failed");
        pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
	}
	else {
		SectionLocker lock(device->m_RequestLock);
		int size=min(outSize,(int)strlen(result)+1);
		outmem->CopyFromBuffer(0,result,size);
		outmem->Release();
        pRequest->CompleteWithInformation(0,(SIZE_T)size);
	}
}
void setInt(CMyDevice *device,IWDFIoRequest* pRequest,DWORD result) {
	IWDFMemory *outmem=NULL;
	pRequest->GetOutputMemory (&outmem);
	if (outmem==NULL) {
		SectionLocker lock(device->m_RequestLock);
		OutputDebugString(L"GetOutputMemory failed");
        pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
	}
	else {
		SectionLocker lock(device->m_RequestLock);
		outmem->CopyFromBuffer(0,&result,sizeof(result));
		outmem->Release();
        pRequest->CompleteWithInformation(0,sizeof(result));
	}
}
DWORD getInt(IWDFIoRequest* pRequest) {

	IWDFMemory *inmem=NULL;
	pRequest->GetInputMemory(&inmem);
	if (inmem==NULL) {
		OutputDebugString(L"GetInputMemory failed");
		return 0xffffffff;
	}
	else {
		SIZE_T size;
		void *data=inmem->GetDataBuffer(&size);
		DWORD d=*(DWORD *)data;
		inmem->Release();
		return d;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions