⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 softlock.cpp

📁 人民邮电出版社出版的《FPGA硬件接口设计实践》一书的代码
💻 CPP
字号:
// SoftLock.cpp: implementation of the CSoftLock class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "cube.h"
#include "SoftLock.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSoftLock::CSoftLock()
{
	m_hDevice = INVALID_HANDLE_VALUE;
}

CSoftLock::~CSoftLock()
{
	CloseIfOpen();
}

BOOL CSoftLock::OpenDevice()
{
	if (m_hDevice != INVALID_HANDLE_VALUE)
		return TRUE;

	const char *sLinkName = "\\\\.\\USBSoftLockDevice0";
	m_hDevice = CreateFile(sLinkName,
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	return m_hDevice != INVALID_HANDLE_VALUE;
}

////////////////////////////////////////////////////////////////////////
// Test_USBSOFTLOCK_IOCTL_GET_PASSWORD
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
BOOL CSoftLock::GetPassword(char* password)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
//		bufInput  supplies data to the device
//		bufOutput is written by the device to return data to this application

	CHAR	bufInput[BUFFER_LENGTH];		// Input to device
	CHAR	bufOutput[BUFFER_LENGTH];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	memset(bufInput, 0, BUFFER_LENGTH);
	memset(bufOutput, 0, BUFFER_LENGTH);

	printf("bufInput : 0x%X, bufOutput : 0x%X\n", (LONG)bufInput, (LONG)bufOutput);

	// Call device IO Control interface (USBSOFTLOCK_IOCTL_GET_PASSWORD) in driver
	printf("Issuing Ioctl to device - ");
	if (!DeviceIoControl(m_hDevice,
						 USBSOFTLOCK_IOCTL_GET_PASSWORD,
						 bufInput,
						 PASSWORD_LENGTH,
						 bufOutput,
						 PASSWORD_LENGTH,
						 &nOutput,
						 NULL) )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		return FALSE;
	}
	else {
		printf("input buffer is : %s, output buffer is %s, output buffer size is %d", 
			bufInput, 
			bufOutput,
			nOutput);
		memcpy(password, bufOutput, PASSWORD_LENGTH);
	}

	return TRUE;
}

////////////////////////////////////////////////////////////////////////
// Test_USBSOFTLOCK_IOCTL_SET_PASSWORD
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
BOOL CSoftLock::SetPassword(char* password)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
//		bufInput  supplies data to the device
//		bufOutput is written by the device to return data to this application

	CHAR	bufInput[IOCTL_INBUF_SIZE];		// Input to device
	CHAR	bufOutput[IOCTL_OUTBUF_SIZE];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	memset(bufInput, 0, BUFFER_LENGTH);
	memset(bufOutput, 0, BUFFER_LENGTH);

	memcpy(bufInput, password, PASSWORD_LENGTH);

	// Call device IO Control interface (USBSOFTLOCK_IOCTL_SET_PASSWORD) in driver
	printf("Issuing Ioctl to device - ");
	if (!DeviceIoControl(m_hDevice,
						 USBSOFTLOCK_IOCTL_SET_PASSWORD,
						 bufInput,
						 PASSWORD_LENGTH,
						 bufOutput,
						 PASSWORD_LENGTH,
						 &nOutput,
						 NULL) )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		return FALSE;
	}
	else {
		printf("input buffer is : %s, output buffer is %s, output buffer size is %d", 
			bufInput, 
			bufOutput,
			nOutput);
	}

	return TRUE;
}

void CSoftLock::CloseIfOpen()
{
	if (m_hDevice != INVALID_HANDLE_VALUE)
	{
		// Close the handle to the driver
		if (!CloseHandle(m_hDevice))
		{
			printf("ERROR: CloseHandle returns %0x.\n", GetLastError());
		}
		m_hDevice = INVALID_HANDLE_VALUE;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -