99244dd997caf3aba3d3c7d8fe7a0d12194ae725
[reactos.git] / rosapps / tests / apc / apc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <windows.h>
4 #define NTOS_MODE_USER
5 #include <ndk/ntndk.h>
6
7 HANDLE OutputHandle;
8 HANDLE InputHandle;
9
10 VOID STDCALL
11 ApcRoutine(PVOID Context,
12 PIO_STATUS_BLOCK IoStatus,
13 ULONG Reserved)
14 {
15 printf("(apc.exe) ApcRoutine(Context %p)\n", Context);
16 }
17
18
19 int main(int argc, char* argv[])
20 {
21 NTSTATUS Status;
22 HANDLE FileHandle;
23 OBJECT_ATTRIBUTES ObjectAttributes;
24 UNICODE_STRING FileName = RTL_CONSTANT_STRING(L"\\C:\\a.txt");
25 IO_STATUS_BLOCK IoStatus;
26 CHAR Buffer[256];
27 HANDLE EventHandle;
28 LARGE_INTEGER off;
29
30 AllocConsole();
31 InputHandle = GetStdHandle(STD_INPUT_HANDLE);
32 OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
33
34 printf("APC test program\n");
35
36 EventHandle = CreateEventW(NULL,
37 FALSE,
38 FALSE,
39 NULL);
40 if (EventHandle == INVALID_HANDLE_VALUE)
41 {
42 printf("Failed to create event\n");
43 return 0;
44 }
45
46 printf("Opening file\n");
47 InitializeObjectAttributes(&ObjectAttributes,
48 &FileName,
49 0,
50 NULL,
51 NULL);
52
53 printf("Creating file\n");
54 FileHandle = CreateFileW(L"C:\\a.txt",
55 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
56 0,
57 NULL,
58 OPEN_EXISTING,
59 FILE_FLAG_OVERLAPPED,
60 NULL);
61
62 if (FileHandle == INVALID_HANDLE_VALUE)
63 {
64
65 printf("Open failed last err 0x%lu\n",GetLastError());
66 return 0;
67 }
68
69 off.QuadPart = 0;
70
71 printf("Reading file\n");
72 Status = ZwReadFile(FileHandle,
73 NULL,
74 (PIO_APC_ROUTINE)ApcRoutine,
75 (PVOID) 0xdeadbeef,
76 &IoStatus,
77 Buffer,
78 256,//len
79 &off ,//offset must exist if file was opened for asynch. i/o aka. OVERLAPPED
80 NULL);
81
82 if (!NT_SUCCESS(Status))
83 {
84 printf("Read failed status 0x%lu\n",Status);
85 }
86 printf("Waiting\n");
87 WaitForSingleObjectEx(EventHandle, INFINITE, TRUE);
88 printf("Returned from wait\n");
89 ZwClose(FileHandle);
90 printf("Program finished\n");
91 return 0;
92 }
93