2004-10-02 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / regtests / shared / regtests.h
index 631c84e..03620c0 100755 (executable)
@@ -7,6 +7,7 @@
  *      06-07-2003  CSH  Created
  */
 #include <stdio.h>
+#include <string.h>
 #include <windows.h>
 
 /* Valid values for Command parameter of TestRoutine */
@@ -84,3 +85,103 @@ extern VOID PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName);
 /* Routines provided by the driver */
 extern PVOID AllocateMemory(ULONG Size);
 extern VOID FreeMemory(PVOID Base);
+
+
+typedef struct _API_DESCRIPTION
+{
+  PCHAR FileName;
+  PCHAR FunctionName;
+  PVOID FunctionAddress;
+  PVOID MockFunctionAddress;
+} API_DESCRIPTION, *PAPI_DESCRIPTION;
+
+extern API_DESCRIPTION ExternalDependencies[];
+extern ULONG MaxExternalDependency;
+
+static inline PVOID
+FrameworkGetFunction(PAPI_DESCRIPTION ApiDescription)
+{
+  HMODULE hModule;
+  PVOID Function;
+
+  hModule = GetModuleHandleA(ApiDescription->FileName);
+  if (hModule != NULL) 
+    {
+      Function = GetProcAddress(hModule, ApiDescription->FunctionName);
+    }
+  else
+         {
+      hModule = LoadLibraryA(ApiDescription->FileName);
+      if (hModule != NULL)
+        {
+          Function = GetProcAddress(hModule, ApiDescription->FunctionName);
+          //FreeLibrary(hModule);
+        }
+    }
+  return Function;
+}
+
+static inline PVOID STDCALL
+FrameworkGetHookInternal(ULONG index)
+{
+  PVOID address;
+
+  if (index > MaxExternalDependency)
+    return NULL;
+
+  if (ExternalDependencies[index].MockFunctionAddress != NULL)
+    return ExternalDependencies[index].MockFunctionAddress;
+
+  if (ExternalDependencies[index].FunctionAddress != NULL)
+    return ExternalDependencies[index].FunctionAddress;
+
+  address = FrameworkGetFunction(&ExternalDependencies[index]);
+  ExternalDependencies[index].FunctionAddress = address;
+  return address;
+}
+
+
+static inline VOID
+_SetHook(PCHAR name,
+  PVOID address)
+{
+  PAPI_DESCRIPTION api;
+  ULONG index;
+
+  for (index = 0; index <= MaxExternalDependency; index++)
+    {
+      api = &ExternalDependencies[index];
+      if (strcmp(api->FunctionName, name) == 0)
+        {
+          api->FunctionAddress = address;
+          return;
+        }
+    }
+}
+
+typedef struct _HOOK
+{
+  PCHAR FunctionName;
+  PVOID FunctionAddress;
+} HOOK, *PHOOK;
+
+static inline VOID
+_SetHooks(PHOOK hookTable)
+{
+  PHOOK hook;
+
+  hook = &hookTable[0];
+  _SetHook(hook->FunctionName,
+    hook->FunctionAddress);
+}
+
+static inline VOID
+_UnsetHooks(PHOOK hookTable)
+{
+  PHOOK hook;
+
+  hook = &hookTable[0];
+  _SetHook(hook->FunctionName,
+    NULL);
+}