1 /*
2  * PROJECT:     Shim test application
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test application for the shim engine
5  * COPYRIGHT:   Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <ntndk.h>
12 #include <winbase.h>
13 
14 NTSYSAPI ULONG NTAPI vDbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ PCCH Format, _In_ va_list ap);
15 #define DPFLTR_ERROR_LEVEL 0
16 
17 void xprintf(const char *fmt, ...)
18 {
19     va_list ap;
20 
21     va_start(ap, fmt);
22     vprintf(fmt, ap);
23     vDbgPrintEx(-1, DPFLTR_ERROR_LEVEL, fmt, ap);
24     va_end(ap);
25 }
26 
27 void test_normal_imports()
28 {
29     char buf[100];
30     DWORD dwSize;
31     HMODULE mod;
32     BOOL (WINAPI *p_GetComputerNameA)(LPSTR lpBuffer, LPDWORD lpnSize);
33 
34     /* Call it directly */
35     ZeroMemory(buf, sizeof(buf));
36     dwSize = sizeof(buf);
37     if (!GetComputerNameA(buf, &dwSize))
38     {
39         xprintf("GetComputerNameA failed: %d\n", GetLastError());
40     }
41     else
42     {
43         xprintf("GetComputerNameA returned: '%s'\n", buf);
44     }
45 
46     /* Call it using GetProcAddress */
47     mod = GetModuleHandleA("kernel32.dll");
48     p_GetComputerNameA = (void*)GetProcAddress(mod, "GetComputerNameA");
49     if (p_GetComputerNameA == NULL)
50     {
51         xprintf("GetProcAddress failed: %d\n", GetLastError());
52     }
53     else
54     {
55         ZeroMemory(buf, sizeof(buf));
56         dwSize = sizeof(buf);
57         if (!p_GetComputerNameA(buf, &dwSize))
58         {
59             xprintf("p_GetComputerNameA failed: %d\n", GetLastError());
60         }
61         else
62         {
63             xprintf("p_GetComputerNameA returned: '%s'\n", buf);
64         }
65     }
66 }
67 
68 INT WINAPI SHStringFromGUIDA(REFGUID,LPSTR,INT);
69 void test_ordinal_imports()
70 {
71     GUID guid = { 0x11223344, 0x5566, 0x7788 };
72     char buf[100];
73     int r;
74     HMODULE mod;
75     INT (WINAPI *p_SHStringFromGUIDA)(REFGUID guid, LPSTR lpszDest, INT cchMax);
76 
77     /* Call it directly */
78     ZeroMemory(buf, sizeof(buf));
79     r = SHStringFromGUIDA(&guid, buf, _countof(buf)-1);
80     if (r)
81     {
82         xprintf("SHStringFromGUIDA failed (%d)\n", r);
83     }
84     else
85     {
86         xprintf("SHStringFromGUIDA returned: '%s'\n", buf);
87     }
88 
89     /* Call it using GetProcAddress */
90     mod = GetModuleHandleA("shlwapi.dll");
91     p_SHStringFromGUIDA = (void*)GetProcAddress(mod, (LPCSTR)23);
92     if (p_SHStringFromGUIDA == NULL)
93     {
94         xprintf("GetProcAddress failed: %d\n", GetLastError());
95     }
96     else
97     {
98         ZeroMemory(buf, sizeof(buf));
99         r = p_SHStringFromGUIDA(&guid, buf, _countof(buf)-1);
100         if (r)
101         {
102             xprintf("p_SHStringFromGUIDA failed (%d)\n", r);
103         }
104         else
105         {
106             xprintf("p_SHStringFromGUIDA returned: '%s'\n", buf);
107         }
108     }
109 }
110 
111 
112 int main(int argc, char* argv[])
113 {
114     xprintf("Normal import (kernel32!GetComputerNameA)\n");
115     test_normal_imports();
116     xprintf("\nOrdinal import (shlwapi!#23)\n");
117     test_ordinal_imports();
118 
119     return 0;
120 }
121