FreeRDP
TestLibraryGetModuleFileName.c
1 
2 #include <stdio.h>
3 #include <winpr/crt.h>
4 #include <winpr/path.h>
5 #include <winpr/tchar.h>
6 #include <winpr/windows.h>
7 #include <winpr/library.h>
8 
9 int TestLibraryGetModuleFileName(int argc, char* argv[])
10 {
11  char ModuleFileName[4096];
12  DWORD len = 0;
13  WINPR_UNUSED(argc);
14  WINPR_UNUSED(argv);
15  /* Test insufficient buffer size behaviour */
16  SetLastError(ERROR_SUCCESS);
17  len = GetModuleFileNameA(NULL, ModuleFileName, 2);
18  if (len != 2)
19  {
20  printf("%s: GetModuleFileNameA unexpectedly returned %" PRIu32 " instead of 2\n", __func__,
21  len);
22  return -1;
23  }
24  if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
25  {
26  printf("%s: Invalid last error value: 0x%08" PRIX32
27  ". Expected 0x%08X (ERROR_INSUFFICIENT_BUFFER)\n",
28  __func__, GetLastError(), ERROR_INSUFFICIENT_BUFFER);
29  return -1;
30  }
31 
32  /* Test with real/sufficient buffer size */
33  SetLastError(ERROR_SUCCESS);
34  len = GetModuleFileNameA(NULL, ModuleFileName, sizeof(ModuleFileName));
35  if (len == 0)
36  {
37  printf("%s: GetModuleFileNameA failed with error 0x%08" PRIX32 "\n", __func__,
38  GetLastError());
39  return -1;
40  }
41  if (len == sizeof(ModuleFileName))
42  {
43  printf("%s: GetModuleFileNameA unexpectedly returned nSize\n", __func__);
44  return -1;
45  }
46  if (GetLastError() != ERROR_SUCCESS)
47  {
48  printf("%s: Invalid last error value: 0x%08" PRIX32 ". Expected 0x%08X (ERROR_SUCCESS)\n",
49  __func__, GetLastError(), ERROR_SUCCESS);
50  return -1;
51  }
52 
53  printf("GetModuleFileNameA: %s\n", ModuleFileName);
54 
55  return 0;
56 }