ReactOS  0.4.15-dev-3299-gbe8e5fc
atexit.c
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for atexit
5 * PROGRAMMER: Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7 
8 #include <apitest.h>
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <ntndk.h>
13 
14 int g_sequence = 0;
16 
17 void exitfunc1(void)
18 {
19  ok_int(g_sequence, 1);
20  g_sequence++;
22 }
23 
24 void exitfunc2(void)
25 {
26  ok_int(g_sequence, 2);
27  g_sequence++;
29 }
30 
31 void exitfunc3(void)
32 {
33  ok_int(g_sequence, 0);
34  g_sequence++;
36  printf("exitfunc3\n");
37 }
38 
40 
42 {
43  HMODULE hmod;
44  PFN_atexit patexit;
45 
46  /* Open the named sempahore to count atexit callbacks */
47  g_hSemaphore = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, FALSE, "atext_apitest_sempahore");
48  ok(g_hSemaphore != NULL, "couldn't open semaphore.\n");
49 
50  /* Load atexit from msvcrt.dll */
51  hmod = GetModuleHandleA("msvcrt.dll");
52  patexit = (PFN_atexit)GetProcAddress(hmod, "atexit");
53  ok(patexit != NULL, "failed to get atexit from msvcrt.dll\n");
54 
55  /* Register 3 exit functions, the second one in msvcrt. */
56  ok_int(atexit(exitfunc1), 0);
57  if (patexit != NULL)
58  {
59  ok_int(patexit(exitfunc2), 0);
60  }
61  ok_int(atexit(exitfunc3), 0);
62 }
63 
65 {
67  PSTR CommandLine;
68  int result;
69  HANDLE hSemaphore;
72 
73  /* Check recursive call */
74  CommandLine = GetCommandLineA();
75  if (strstr(CommandLine, "-run") != NULL)
76  {
77  Test_atexit();
78  return;
79  }
80 
81  /* Create a named semaphore to count atexit callbacks in remote process */
82  hSemaphore = CreateSemaphoreA(NULL, 1, 20, "atext_apitest_sempahore");
83 
84  /* Run the actual test in a new process */
85  sprintf(Buffer, "%s -run", CommandLine);
86  result = system(Buffer);
87  ok_int(result, 0);
88 
89  /* Check the new semaphore state */
90  Status = NtQuerySemaphore(hSemaphore, SemaphoreBasicInformation, &SemInfo, sizeof(SemInfo), NULL);
91  ok(NT_SUCCESS(Status), "NtQuerySemaphore failed: 0x%lx\n", Status);
92  ok_int(SemInfo.CurrentCount, 4);
93 }
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
GLuint64EXT * result
Definition: glext.h:11304
#define __cdecl
Definition: accygwin.h:79
HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreA(IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes OPTIONAL, IN LONG lInitialCount, IN LONG lMaximumCount, IN LPCSTR lpName OPTIONAL)
Definition: synch.c:430
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
char CHAR
Definition: xmlstorage.h:175
LONG NTSTATUS
Definition: precomp.h:26
HANDLE g_hSemaphore
Definition: atexit.c:15
int __cdecl atexit(void(__cdecl *)(void))
Definition: atonexit.c:97
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define FALSE
Definition: types.h:117
#define ok_int(expression, result)
Definition: atltest.h:134
Definition: bufpool.h:45
int __cdecl system(_In_opt_z_ const char *_Command)
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
LPSTR WINAPI GetCommandLineA(VOID)
Definition: proc.c:2004
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
int g_sequence
Definition: atexit.c:14
static PEXPLICIT_ACCESSW *static HMODULE hmod
Definition: security.c:141
void exitfunc2(void)
Definition: atexit.c:24
void exitfunc3(void)
Definition: atexit.c:31
NTSTATUS NTAPI NtQuerySemaphore(IN HANDLE SemaphoreHandle, IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass, OUT PVOID SemaphoreInformation, IN ULONG SemaphoreInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: sem.c:222
#define ok(value,...)
Definition: atltest.h:57
signed char * PSTR
Definition: retypes.h:7
#define NULL
Definition: types.h:112
void Test_atexit()
Definition: atexit.c:41
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore(IN HANDLE hSemaphore, IN LONG lReleaseCount, IN LPLONG lpPreviousCount)
Definition: synch.c:542
HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreA(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
Definition: synch.c:516
#define GetProcAddress(x, y)
Definition: compat.h:612
START_TEST(atexit)
Definition: atexit.c:64
int(__cdecl * PFN_atexit)(void(__cdecl *)(void))
Definition: atexit.c:39
#define SEMAPHORE_ALL_ACCESS
Definition: winbase.h:160
void exitfunc1(void)
Definition: atexit.c:17
#define printf
Definition: config.h:203
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31