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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/*
Windows内核下用户态和内核共享内存 3环代码
编译方法参见makefile. TAB = 8
*/
#include <windows.h>
#include <stdio.h>
#include <windowsx.h>
#include "resource.h"
#define SYS_LINK_NAME "\\\\.\\SysLinkShareMemory"
#define IOCTL_MEMORY CTL_CODE( FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS )
#define TIMER_ID 100
#define STATIC_EDIT 1000
#pragma comment(linker, "/Entry:Jmain")
#pragma comment(linker, "/subsystem:windows")
PVOID g_pShareMemory;
//===========================================================================
/*
WM\_INITDIALOG消息处理
*/
BOOL Test_OnInitDialog( HWND hWnd, HWND hwndFocus, LPARAM lParam ) {
SetTimer(hWnd, TIMER_ID, 1000, NULL );
return TRUE;
}
//===========================================================================
/*
WM\_TIMER消息
*/
void Test_OnTimer(HWND hWnd, UINT id) {
SYSTEMTIME StTime;
CHAR buf[128];
if ( g_pShareMemory ) {
FileTimeToSystemTime( (FILETIME*)g_pShareMemory, &StTime );
wsprintf( buf, "%02d:%02d:%02d", StTime.wHour, StTime.wMinute, StTime.wSecond );
SendDlgItemMessage( hWnd, STATIC_EDIT, WM_SETTEXT, 0, (LPARAM)buf );
}
}
//===========================================================================
/*
WM\_CLOSE消息处理
*/
void Test_OnClose( HWND hWnd ) {
KillTimer(hWnd, TIMER_ID );
EndDialog( hWnd, 0 );
ExitProcess(0);
}
BOOL WINAPI DialogProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
switch ( uMsg ) {
HANDLE_MSG( hWnd, WM_CLOSE, Test_OnClose );
HANDLE_MSG( hWnd, WM_INITDIALOG, Test_OnInitDialog );
HANDLE_MSG( hWnd,WM_TIMER, Test_OnTimer);
}
return FALSE;
}
//===========================================================================
//入口
int Jmain() {
HANDLE hFile = 0;
BOOL bRet;
DWORD dwByteRead;
//打开设备
hFile = CreateFile( SYS_LINK_NAME, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
if ( hFile == INVALID_HANDLE_VALUE ) {
printf( "打开设备错误了!\n" );
return -1;
}
bRet = DeviceIoControl(hFile, IOCTL_MEMORY, NULL, 0, &g_pShareMemory,
sizeof(g_pShareMemory), &dwByteRead, NULL);
if ( !bRet ) {
printf( "DeviceIoControl 错误!\n" );
return -1;
}
DialogBox ( GetModuleHandle( NULL ), MAKEINTRESOURCE ( IDD_DIALOG1 ), NULL, &DialogProc );
return 0;
}
|