summaryrefslogtreecommitdiffstats
path: root/res/ATFBlast_ALL/userport/UserPort/Examples/IOPort.c
blob: 7cef1825aca286f64c09dec964fe42a2ffdce856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <windows.h>
static BOOL bPrivException = FALSE;

void outport(UINT portid, UINT value)
{
  __asm mov edx,portid;
  __asm mov eax,value;
  __asm out dx,ax;
}
void outportb(UINT portid, BYTE value)
{
  __asm mov edx,portid
  __asm mov al,value
  __asm out dx,al
}

BYTE inportb(UINT portid)
{
  unsigned char value;
  
  __asm mov edx,portid
  __asm in al,dx
  __asm mov value,al
  return value;
}

UINT inport(UINT portid)
{
  int value=0;
  __asm mov edx,portid
  __asm in ax,dx
  __asm mov value,eax
  return value;
}

LONG WINAPI HandlerExceptionFilter ( EXCEPTION_POINTERS *pExPtrs )
{

	if (pExPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
	{
		pExPtrs->ContextRecord->Eip ++; // Skip the OUT or IN instruction that caused the exception
		bPrivException = TRUE;
		return EXCEPTION_CONTINUE_EXECUTION;
	}
	else
		return EXCEPTION_CONTINUE_SEARCH;
}

BOOL StartUpIoPorts(UINT PortToAccess, BOOL bShowMessageBox, HWND hParentWnd)
{
	HANDLE hUserPort;

  hUserPort = CreateFile("\\\\.\\UserPort", GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  CloseHandle(hUserPort); // Activate the driver
	Sleep(100); // We must make a process switch

	SetUnhandledExceptionFilter(HandlerExceptionFilter);
	
	bPrivException = FALSE;
	inportb(PortToAccess);  // Try to access the given port address

	if (bPrivException)
	{
		if (bShowMessageBox)
		{
    	MessageBox(hParentWnd,"Privileged instruction exception has occured!\r\n\r\n"
												 "To use this program under Windows NT or Windows 2000\r\n"
												 "you need to install the driver 'UserPort.SYS' and grant\r\n"
												 "access to the ports used by this program.",
												 NULL,MB_OK);
    }
		return FALSE;
	}
	return TRUE;
}