ReactOS  0.4.11-dev-433-g473ca91
apphelp.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Application compatibility module
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: apphelp entrypoint / generic interface functions
5  * COPYRIGHT: Copyright 2011 André Hentschel
6  * Copyright 2013 Mislav Blaževic
7  * Copyright 2015-2018 Mark Jansen (mark.jansen@reactos.org)
8  */
9 
10 #define WIN32_NO_STATUS
11 #include "windef.h"
12 #include "winbase.h"
13 #include "winver.h"
14 #include "strsafe.h"
15 #include "apphelp.h"
16 #include "ndk/rtlfuncs.h"
17 #include "ndk/kdtypes.h"
18 
19 
20 /* from dpfilter.h */
21 #define DPFLTR_APPCOMPAT_ID 123
22 
23 #ifndef NT_SUCCESS
24 #define NT_SUCCESS(StatCode) ((NTSTATUS)(StatCode) >= 0)
25 #endif
26 
27 ULONG g_ShimDebugLevel = 0xffffffff;
29 
31 {
32  static const UNICODE_STRING DebugKey = RTL_CONSTANT_STRING(L"SHIM_DEBUG_LEVEL");
33  UNICODE_STRING DebugValue;
35  ULONG NewLevel = SHIM_ERR;
36  WCHAR Buffer[40];
37 
38  RtlInitEmptyUnicodeString(&DebugValue, Buffer, sizeof(Buffer));
39 
40  Status = RtlQueryEnvironmentVariable_U(NULL, &DebugKey, &DebugValue);
41 
42  if (NT_SUCCESS(Status))
43  {
44  if (!NT_SUCCESS(RtlUnicodeStringToInteger(&DebugValue, 10, &NewLevel)))
45  NewLevel = SHIM_ERR;
46  }
47  g_ShimDebugLevel = NewLevel;
48 }
49 
50 
52 {
53  switch (reason)
54  {
55  case DLL_PROCESS_ATTACH:
58  SdbpHeapInit();
59  break;
60  case DLL_PROCESS_DETACH:
62  break;
63  }
64  return TRUE;
65 }
66 
68 {
69  SHIM_WARN("stub: ptr=%p, path='%S'\r\n", ptr, path);
70  return TRUE;
71 }
72 
73 
74 BOOL WINAPI ApphelpCheckShellObject(REFCLSID ObjectCLSID, BOOL bShimIfNecessary, ULONGLONG *pullFlags)
75 {
76  WCHAR GuidString[100];
77  if (!ObjectCLSID || !SdbGUIDToString(ObjectCLSID, GuidString, 100))
78  GuidString[0] = L'\0';
79  SHIM_WARN("stub: ObjectCLSID='%S', bShimIfNecessary=%d, pullFlags=%p)\n", GuidString, bShimIfNecessary, pullFlags);
80 
81  if (pullFlags)
82  *pullFlags = 0;
83 
84  return TRUE;
85 }
86 
99 {
100  char Buffer[512];
101  va_list ArgList;
102  char* Current = Buffer;
103  const char* LevelStr;
104  size_t Length = sizeof(Buffer);
105 
106  if (g_ShimDebugLevel == 0xffffffff)
108 
109  if (Level > g_ShimDebugLevel)
110  return FALSE;
111 
112  switch (Level)
113  {
114  case SHIM_ERR:
115  LevelStr = "Err ";
116  Level = DPFLTR_MASK | (1 << DPFLTR_ERROR_LEVEL);
117  break;
118  case SHIM_WARN:
119  LevelStr = "Warn";
120  Level = DPFLTR_MASK | (1 << DPFLTR_WARNING_LEVEL);
121  break;
122  case SHIM_INFO:
123  LevelStr = "Info";
124  Level = DPFLTR_MASK | (1 << DPFLTR_INFO_LEVEL);
125  break;
126  default:
127  LevelStr = "User";
128  Level = DPFLTR_MASK | (1 << DPFLTR_INFO_LEVEL);
129  break;
130  }
131  StringCchPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, "[%s][%-20s] ", LevelStr, FunctionName);
132 
133  va_start(ArgList, Format);
134  StringCchVPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, Format, ArgList);
135  va_end(ArgList);
136 
137 #if defined(APPCOMPAT_USE_DBGPRINTEX) && APPCOMPAT_USE_DBGPRINTEX
138  return NT_SUCCESS(DbgPrintEx(DPFLTR_APPCOMPAT_ID, Level, "%s", Buffer));
139 #else
140  DbgPrint("%s", Buffer);
141  return TRUE;
142 #endif
143 }
144 
145 
146 #define APPHELP_DONTWRITE_REASON 2
147 #define APPHELP_CLEARBITS 0x100 /* TODO: Investigate */
148 #define APPHELP_IGNORE_ENVIRONMENT 0x400
149 
150 #define APPHELP_VALID_RESULT 0x10000
151 #define APPHELP_RESULT_NOTFOUND 0x20000
152 #define APPHELP_RESULT_FOUND 0x40000
153 
174 BOOL
175 WINAPI
191 {
193  HSDB hsdb = NULL;
194  DWORD dwFlags = 0;
195 
196  if (SxsData)
197  *SxsData = NULL;
198  if (SxsDataSize)
199  *SxsDataSize = 0;
200  if (FusionFlags)
201  *FusionFlags = 0;
202  if (SomeFlag1)
203  *SomeFlag1 = 0;
204  if (SomeFlag2)
205  *SomeFlag2 = 0;
206  if (Reason)
207  dwFlags = *Reason;
208 
209  dwFlags &= ~APPHELP_CLEARBITS;
210 
211  *SdbQueryAppCompatData = result = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SDBQUERYRESULT));
212  if (SdbQueryAppCompatDataSize)
213  *SdbQueryAppCompatDataSize = sizeof(*result);
214 
215 
217  if (hsdb)
218  {
219  BOOL FoundMatch;
220  DWORD MatchingExeFlags = 0;
221 
222  if (dwFlags & APPHELP_IGNORE_ENVIRONMENT)
223  MatchingExeFlags |= SDBGMEF_IGNORE_ENVIRONMENT;
224 
225  FoundMatch = SdbGetMatchingExe(hsdb, ApplicationName, NULL, Environment, MatchingExeFlags, result);
226  if (FileHandle != INVALID_HANDLE_VALUE)
227  {
228  dwFlags |= APPHELP_VALID_RESULT;
229  dwFlags |= (FoundMatch ? APPHELP_RESULT_FOUND : APPHELP_RESULT_NOTFOUND);
230  }
231 
232  SdbReleaseDatabase(hsdb);
233  }
234 
235  if (Reason && !(dwFlags & APPHELP_DONTWRITE_REASON))
236  *Reason = dwFlags;
237 
238 
239  /* We should _ALWAYS_ return TRUE here, unless we want to block an application from starting! */
240  return TRUE;
241 }
242 
DWORD *typedef PVOID
Definition: winlogon.h:61
static PWSTR GuidString
Definition: apphelp.c:91
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define APPHELP_DONTWRITE_REASON
Definition: apphelp.c:146
#define _In_opt_z_
Definition: no_sal2.h:221
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG PVOID PULONG PULONG FusionFlags
Definition: env.c:45
#define TRUE
Definition: types.h:120
#define SDBGMEF_IGNORE_ENVIRONMENT
Definition: apphelp.h:59
void SdbpHeapDeinit(void)
Definition: sdbapi.c:42
enum _SHIM_LOG_LEVEL SHIM_LOG_LEVEL
#define DbgPrint
Definition: loader.c:25
static IN LPSTR IN PVOID Unk2
Definition: ldrinit.c:46
STRSAFEAPI StringCchVPrintfExA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags, STRSAFE_LPCSTR pszFormat, va_list argList)
Definition: strsafe.h:650
#define INVALID_HANDLE_VALUE
Definition: apphelp.c:45
void SdbpHeapInit(void)
Definition: sdbapi.c:34
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define REFCLSID
Definition: guiddef.h:112
const char * PCSTR
Definition: typedefs.h:51
#define DPFLTR_INFO_LEVEL
Definition: kdtypes.h:33
HSDB WINAPI SdbInitDatabase(DWORD, LPCWSTR)
Definition: hsdb.c:369
#define DPFLTR_MASK
Definition: kdtypes.h:34
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
NTSYSAPI ULONG __cdecl DbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ _Printf_format_string_ PCSTR Format,...)
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
BOOL WINAPI SdbGetMatchingExe(HSDB hsdb, LPCWSTR path, LPCWSTR module_name, LPCWSTR env, DWORD flags, PSDBQUERYRESULT result)
Definition: hsdb.c:436
_IRQL_requires_same_ typedef _In_ ULONG _In_ UCHAR Level
Definition: wmitypes.h:55
#define APPHELP_CLEARBITS
Definition: apphelp.c:147
uint16_t * PWCHAR
Definition: typedefs.h:54
STRSAFEAPI StringCchPrintfExA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags, STRSAFE_LPCSTR pszFormat,...)
Definition: strsafe.h:575
ULONG Reason
Definition: ntimage.h:533
DWORD DWORD
Definition: winlogon.h:84
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define DPFLTR_WARNING_LEVEL
Definition: kdtypes.h:31
#define _In_opt_
Definition: no_sal2.h:213
Definition: apphelp.h:27
#define SDB_DATABASE_MAIN_SHIM
Definition: apphelp.h:24
#define va_end(ap)
Definition: acmsvcex.h:90
#define FALSE
Definition: types.h:117
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG PVOID PULONG PULONG PULONG64 SomeFlag1
Definition: env.c:45
BOOL WINAPI ApphelpCheckShellObject(REFCLSID ObjectCLSID, BOOL bShimIfNecessary, ULONGLONG *pullFlags)
Definition: apphelp.c:74
static PVOID ptr
Definition: dispmode.c:27
r reserved
Definition: btrfs.c:2640
BOOL WINAPI SdbGUIDToString(CONST GUID *Guid, PWSTR GuidString, SIZE_T Length)
Definition: sdbapi.c:397
NTSYSAPI NTSTATUS NTAPI RtlQueryEnvironmentVariable_U(_In_opt_ PWSTR Environment, _In_ PCUNICODE_STRING Name, _Out_ PUNICODE_STRING Value)
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
Definition: bufpool.h:45
BOOL WINAPIV ShimDbgPrint(SHIM_LOG_LEVEL Level, PCSTR FunctionName, PCSTR Format,...)
Definition: apphelp.c:98
#define DLL_PROCESS_DETACH
Definition: compat.h:119
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
#define _Out_opt_
Definition: no_sal2.h:339
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
uint64_t ULONGLONG
Definition: typedefs.h:65
#define APPHELP_RESULT_NOTFOUND
Definition: apphelp.c:151
static IN LPSTR Unk1
Definition: ldrinit.c:46
#define HID_DOS_PATHS
Definition: apphelp.h:19
HANDLE HINSTANCE
Definition: typedefs.h:75
static WCHAR reason[MAX_STRING_RESOURCE_LEN]
Definition: object.c:1904
BOOL WINAPI ApphelpCheckInstallShieldPackage(void *ptr, LPCWSTR path)
Definition: apphelp.c:67
static const WCHAR L[]
Definition: oid.c:1087
VOID UINTN Length
Definition: acefiex.h:744
#define APPHELP_IGNORE_ENVIRONMENT
Definition: apphelp.c:148
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
ULONG g_ShimDebugLevel
Definition: apphelp.c:27
void WINAPI SdbReleaseDatabase(HSDB)
Definition: hsdb.c:417
BOOL WINAPI ApphelpCheckRunAppEx(_In_ HANDLE FileHandle, _In_opt_ PVOID Unk1, _In_opt_ PVOID Unk2, _In_opt_z_ PWCHAR ApplicationName, _In_opt_ PVOID Environment, _In_opt_ USHORT ExeType, _Inout_opt_ PULONG Reason, _Out_opt_ PVOID *SdbQueryAppCompatData, _Out_opt_ PULONG SdbQueryAppCompatDataSize, _Out_opt_ PVOID *SxsData, _Out_opt_ PULONG SxsDataSize, _Out_opt_ PULONG FusionFlags, _Out_opt_ PULONG64 SomeFlag1, _Out_opt_ PULONG SomeFlag2)
Definition: apphelp.c:176
#define WINAPI
Definition: msvc.h:20
#define WINAPIV
Definition: sdbpapi.h:64
Status
Definition: gdiplustypes.h:24
#define _In_
Definition: no_sal2.h:204
static HANDLE FileHandle
Definition: cabinet.c:48
void ApphelppInitDebugLevel(void)
Definition: apphelp.c:30
PVOID PVOID PWCHAR ApplicationName
Definition: env.c:45
DWORD *typedef HANDLE
Definition: winlogon.h:61
PVOID PVOID PWCHAR PVOID USHORT ExeType
Definition: env.c:45
#define DPFLTR_ERROR_LEVEL
Definition: main.cpp:32
unsigned short USHORT
Definition: pedump.c:61
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG PVOID PULONG SxsDataSize
Definition: env.c:45
Definition: services.c:325
#define DPFLTR_APPCOMPAT_ID
Definition: apphelp.c:21
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG PVOID PULONG PULONG PULONG64 PULONG SomeFlag2
Definition: env.c:45
unsigned int * PULONG
Definition: retypes.h:1
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
HANDLE HMODULE
Definition: typedefs.h:75
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 const char UINT32 const char * FunctionName
Definition: acpixf.h:1256
#define STRSAFE_NULL_ON_FAILURE
Definition: ntstrsafe.h:47
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToInteger(PUNICODE_STRING String, ULONG Base, PULONG Value)
unsigned int ULONG
Definition: retypes.h:1
#define APPHELP_RESULT_FOUND
Definition: apphelp.c:152
static HINSTANCE hinst
Definition: edit.c:551
unsigned __int64 * PULONG64
Definition: imports.h:198
HMODULE g_hInstance
Definition: apphelp.c:28
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG PVOID * SxsData
Definition: env.c:45
GLuint64EXT * result
Definition: glext.h:11304
#define APPHELP_VALID_RESULT
Definition: apphelp.c:150
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID * SdbQueryAppCompatData
Definition: env.c:45
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
Definition: apphelp.c:51
#define _Inout_opt_
Definition: no_sal2.h:258
PVOID PVOID PWCHAR PVOID USHORT PULONG PVOID PULONG SdbQueryAppCompatDataSize
Definition: env.c:45
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14