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
|
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/windows.h>
#include <winpr/library.h>
int TestLibraryGetModuleFileName(int argc, char* argv[])
{
char ModuleFileName[4096];
DWORD len = 0;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
/* Test insufficient buffer size behaviour */
SetLastError(ERROR_SUCCESS);
len = GetModuleFileNameA(NULL, ModuleFileName, 2);
if (len != 2)
{
printf("%s: GetModuleFileNameA unexpectedly returned %" PRIu32 " instead of 2\n", __func__,
len);
return -1;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
printf("%s: Invalid last error value: 0x%08" PRIX32
". Expected 0x%08X (ERROR_INSUFFICIENT_BUFFER)\n",
__func__, GetLastError(), ERROR_INSUFFICIENT_BUFFER);
return -1;
}
/* Test with real/sufficient buffer size */
SetLastError(ERROR_SUCCESS);
len = GetModuleFileNameA(NULL, ModuleFileName, sizeof(ModuleFileName));
if (len == 0)
{
printf("%s: GetModuleFileNameA failed with error 0x%08" PRIX32 "\n", __func__,
GetLastError());
return -1;
}
if (len == sizeof(ModuleFileName))
{
printf("%s: GetModuleFileNameA unexpectedly returned nSize\n", __func__);
return -1;
}
if (GetLastError() != ERROR_SUCCESS)
{
printf("%s: Invalid last error value: 0x%08" PRIX32 ". Expected 0x%08X (ERROR_SUCCESS)\n",
__func__, GetLastError(), ERROR_SUCCESS);
return -1;
}
printf("GetModuleFileNameA: %s\n", ModuleFileName);
return 0;
}
|