aboutsummaryrefslogtreecommitdiffhomepage
path: root/ext/detours/samples/comeasy
diff options
context:
space:
mode:
Diffstat (limited to 'ext/detours/samples/comeasy')
-rw-r--r--ext/detours/samples/comeasy/Makefile116
-rw-r--r--ext/detours/samples/comeasy/comeasy.cpp69
-rw-r--r--ext/detours/samples/comeasy/wrotei.cpp167
-rw-r--r--ext/detours/samples/comeasy/wrotei.rc17
4 files changed, 369 insertions, 0 deletions
diff --git a/ext/detours/samples/comeasy/Makefile b/ext/detours/samples/comeasy/Makefile
new file mode 100644
index 0000000..0c3a057
--- /dev/null
+++ b/ext/detours/samples/comeasy/Makefile
@@ -0,0 +1,116 @@
+##############################################################################
+##
+## API Extension to Measure time slept.
+##
+## Microsoft Research Detours Package
+##
+## Copyright (c) Microsoft Corporation. All rights reserved.
+##
+
+!include ..\common.mak
+
+LIBS=$(LIBS) kernel32.lib
+
+##############################################################################
+
+all: dirs \
+ $(BIND)\wrotei$(DETOURS_BITS).dll \
+ $(BIND)\comeasy.exe \
+!IF $(DETOURS_SOURCE_BROWSING)==1
+ $(OBJD)\wrotei$(DETOURS_BITS).bsc \
+ $(OBJD)\comeasy.bsc \
+!ENDIF
+ option
+
+##############################################################################
+
+clean:
+ -del $(BIND)\wrotei*.* 2>nul
+ -del $(BIND)\comeasy.* 2>nul
+ -del $(BIND)\wrotei.* *~ 2>nul
+ -rmdir /q /s $(OBJD) 2>nul
+
+realclean: clean
+ -rmdir /q /s $(OBJDS) 2>nul
+
+dirs:
+ @if not exist $(BIND) mkdir $(BIND) && echo. Created $(BIND)
+ @if not exist $(OBJD) mkdir $(OBJD) && echo. Created $(OBJD)
+
+##############################################################################
+
+$(OBJD)\wrotei.obj : wrotei.cpp
+
+$(OBJD)\wrotei.res : wrotei.rc
+
+$(BIND)\wrotei$(DETOURS_BITS).dll $(BIND)\wrotei$(DETOURS_BITS).lib: \
+ $(OBJD)\wrotei.obj $(OBJD)\wrotei.res $(DEPS)
+ cl /LD $(CFLAGS) /Fe$(@R).dll /Fd$(@R).pdb \
+ $(OBJD)\wrotei.obj $(OBJD)\wrotei.res \
+ /link $(LINKFLAGS) /subsystem:console \
+ /export:DetourFinishHelperProcess,@1,NONAME \
+ $(LIBS) ole32.lib
+
+$(OBJD)\wrotei$(DETOURS_BITS).bsc : $(OBJD)\wrotei.obj
+ bscmake /v /n /o $@ $(OBJD)\wrotei.sbr
+
+$(OBJD)\comeasy.obj : comeasy.cpp
+
+$(BIND)\comeasy.exe : $(OBJD)\comeasy.obj $(DEPS)
+ cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb \
+ $(OBJD)\comeasy.obj \
+ /link $(LINKFLAGS) $(LIBS) ole32.lib \
+ /subsystem:console /fixed:no
+
+$(OBJD)\comeasy.bsc : $(OBJD)\comeasy.obj
+ bscmake /v /n /o $@ $(OBJD)\comeasy.sbr
+
+############################################### Install non-bit-size binaries.
+
+!IF "$(DETOURS_OPTION_PROCESSOR)" != ""
+
+$(OPTD)\wrotei$(DETOURS_OPTION_BITS).dll:
+$(OPTD)\wrotei$(DETOURS_OPTION_BITS).pdb:
+
+$(BIND)\wrotei$(DETOURS_OPTION_BITS).dll : $(OPTD)\wrotei$(DETOURS_OPTION_BITS).dll
+ @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR).
+$(BIND)\wrotei$(DETOURS_OPTION_BITS).pdb : $(OPTD)\wrotei$(DETOURS_OPTION_BITS).pdb
+ @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR).
+
+option: \
+ $(BIND)\wrotei$(DETOURS_OPTION_BITS).dll \
+ $(BIND)\wrotei$(DETOURS_OPTION_BITS).pdb \
+
+!ELSE
+
+option:
+
+!ENDIF
+
+##############################################################################
+
+test: all
+ @echo -------- Reseting test binaries to initial state. -----------------------
+ $(BIND)\setdll.exe -r $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Should not load slept$(DETOURS_BITS).dll --------------------------------------
+ $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Adding wrotei$(DETOURS_BITS).dll to comeasy.exe ------------------------------
+ $(BIND)\setdll.exe -d:$(BIND)\wrotei$(DETOURS_BITS).dll $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Should load wrotei$(DETOURS_BITS).dll ----------------------------------------
+ $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Removing wrotei$(DETOURS_BITS).dll from comeasy.exe --------------------------
+ $(BIND)\setdll.exe -r $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Should not load wrotei$(DETOURS_BITS).dll ------------------------------------
+ $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Should load wrotei$(DETOURS_BITS).dll dynamically using withdll.exe ----------
+ $(BIND)\withdll.exe -d:$(BIND)\wrotei$(DETOURS_BITS).dll $(BIND)\comeasy.exe
+ @echo.
+ @echo -------- Test completed. ------------------------------------------------
+
+################################################################# End of File.
diff --git a/ext/detours/samples/comeasy/comeasy.cpp b/ext/detours/samples/comeasy/comeasy.cpp
new file mode 100644
index 0000000..e5c66e8
--- /dev/null
+++ b/ext/detours/samples/comeasy/comeasy.cpp
@@ -0,0 +1,69 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// Detour Test Program (comeasy.cpp of comeasy.exe)
+//
+// Microsoft Research Detours Package
+//
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+
+#include <ole2.h>
+#include <windows.h>
+#include <stdio.h>
+
+//////////////////////////////////////////////////////////////////////////////
+//
+int __cdecl main(int argc, char **argv)
+{
+ HRESULT hr;
+
+ (void)argc;
+ (void)argv;
+
+ LPSTREAM pStream = NULL;
+ ULARGE_INTEGER ul;
+ LARGE_INTEGER li;
+
+ printf("comeasy.exe: Starting (at %p).\n", main);
+
+ CoInitialize(NULL);
+
+ hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
+
+ ul.QuadPart = 512;
+ hr = pStream->SetSize(ul);
+
+ li.QuadPart = 0;
+ hr = pStream->Seek(li, STREAM_SEEK_SET, NULL);
+
+ printf("comeasy.exe: First write.\n");
+ fflush(stdout);
+
+ li.QuadPart = 0;
+ hr = pStream->Write(&ul, sizeof(ul), NULL);
+
+ printf("comeasy.exe: Second write.\n");
+ fflush(stdout);
+
+ li.QuadPart = 1;
+ hr = pStream->Write(&li, sizeof(li), NULL);
+
+ printf("comeasy.exe: Third write.\n");
+ fflush(stdout);
+
+ li.QuadPart = 2;
+ hr = pStream->Write(&li, sizeof(li), NULL);
+
+ pStream->Release();
+ pStream = NULL;
+
+ CoUninitialize();
+
+ printf("comeasy.exe: Exiting.\n\n");
+ fflush(stdout);
+
+ return 0;
+}
+
+//
+///////////////////////////////////////////////////////////////// End of File.
diff --git a/ext/detours/samples/comeasy/wrotei.cpp b/ext/detours/samples/comeasy/wrotei.cpp
new file mode 100644
index 0000000..7bd925d
--- /dev/null
+++ b/ext/detours/samples/comeasy/wrotei.cpp
@@ -0,0 +1,167 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// Detour Test Program (wrotei.cpp of wrotei.dll)
+//
+// Microsoft Research Detours Package
+//
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+// An example dynamically detouring a function.
+//
+#include <stdio.h>
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// WARNING:
+//
+// CINTERFACE must be defined so that the lpVtbl pointer is visible
+// on COM interfaces. However, once we've defined it, we must use
+// coding conventions when accessing interface members, for example:
+// i->lpVtbl->Write
+// instead of the C++ syntax:
+// i->Write.
+// We must also pass the implicit "this" parameter explicitly:
+// i->lpVtbl->Write(i, pb, 0, NULL)
+// instead of the C++ syntax:
+// i->Write(pb, 0, NULL)
+//
+#define CINTERFACE
+#include <ole2.h>
+#include <windows.h>
+#include <detours.h>
+
+//////////////////////////////////////////////////////////////////////////////
+//
+LONG dwWrote = 0;
+
+static int (WINAPI * TrueEntryPoint)(VOID) = NULL;
+static int (WINAPI * RawEntryPoint)(VOID) = NULL;
+
+//////////////////////////////////////////////////////////////////////////////
+//
+HRESULT (STDMETHODCALLTYPE *RealIStreamWrite)(IStream * This,
+ const void *pv,
+ ULONG cb,
+ ULONG *pcbWritten) = NULL;
+
+HRESULT STDMETHODCALLTYPE MineIStreamWrite(IStream * This,
+ const void *pv,
+ ULONG cb,
+ ULONG *pcbWritten)
+{
+ HRESULT hr;
+ ULONG cbWritten = 0;
+ if (pcbWritten == NULL) {
+ pcbWritten = &cbWritten;
+ }
+
+ hr = RealIStreamWrite(This, pv, cb, pcbWritten);
+
+ for (;;) {
+ LONG dwOld = dwWrote;
+ LONG dwNew = dwOld + *pcbWritten;
+
+ if (InterlockedCompareExchange(&dwWrote, dwNew, dwOld) == dwOld) {
+ break;
+ }
+ }
+
+ return hr;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+//
+int WINAPI TimedEntryPoint(VOID)
+{
+ // We couldn't call CoInitializeEx in DllMain,
+ // so we detour the vtable entries here...
+ LONG error;
+ LPSTREAM pStream = NULL;
+
+ // Create a temporary object so we can get a vtable.
+ CreateStreamOnHGlobal(NULL, TRUE, &pStream);
+
+ // Apply the detour to the vtable.
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ if (pStream != NULL) {
+ RealIStreamWrite = pStream->lpVtbl->Write;
+ DetourAttach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
+ }
+ error = DetourTransactionCommit();
+
+ if (pStream != NULL) {
+ pStream->lpVtbl->Release(pStream);
+ pStream = NULL;
+ }
+
+ if (error == NO_ERROR) {
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Detoured IStream::Wrote() from OnHGlobal.\n");
+ }
+ else {
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Error detouring IStram::Wrote(): %ld\n", error);
+ }
+
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Calling EntryPoint\n\n");
+ fflush(stdout);
+
+ return TrueEntryPoint();
+}
+
+BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
+{
+ LONG error;
+ (void)hinst;
+ (void)reserved;
+
+ if (DetourIsHelperProcess()) {
+ return TRUE;
+ }
+
+ if (dwReason == DLL_PROCESS_ATTACH) {
+ DetourRestoreAfterWith();
+
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Starting.\n");
+ fflush(stdout);
+
+ // NB: DllMain can't call LoadLibrary, so we hook the app entry point.
+ TrueEntryPoint = (int (WINAPI *)(VOID))DetourGetEntryPoint(NULL);
+ RawEntryPoint = TrueEntryPoint;
+
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ DetourAttach(&(PVOID&)TrueEntryPoint, TimedEntryPoint);
+ error = DetourTransactionCommit();
+
+ if (error == NO_ERROR) {
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Detoured EntryPoint().\n");
+ }
+ else {
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Error detouring EntryPoint(): %ld\n", error);
+ }
+ }
+ else if (dwReason == DLL_PROCESS_DETACH) {
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ if (RealIStreamWrite != NULL) {
+ DetourDetach(&(PVOID&)RealIStreamWrite, (PVOID)MineIStreamWrite);
+ }
+ DetourDetach(&(PVOID&)TrueEntryPoint, TimedEntryPoint);
+ error = DetourTransactionCommit();
+
+ printf("wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
+ " Removed IStream::Wrote() detours (%ld), wrote %ld bytes.\n",
+ error, dwWrote);
+
+ fflush(stdout);
+ }
+ return TRUE;
+}
+//
+///////////////////////////////////////////////////////////////// End of File.
diff --git a/ext/detours/samples/comeasy/wrotei.rc b/ext/detours/samples/comeasy/wrotei.rc
new file mode 100644
index 0000000..6a51c98
--- /dev/null
+++ b/ext/detours/samples/comeasy/wrotei.rc
@@ -0,0 +1,17 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// Version information for wrotei.rc.
+//
+// Microsoft Research Detours Package
+//
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+
+#include "detver.h"
+
+#define VER_INTERNALNAME_STR "wrotei" DETOURS_STRINGIFY(DETOURS_BITS)
+#define VER_ORIGINALFILENAME_STR "wrotei" DETOURS_STRINGIFY(DETOURS_BITS) ".dll"
+#define VER_FILEDESCRIPTION_STR "Detours COM Easy Sample"
+#define VER_COMPANYNAME_STR "Microsoft Corporation"
+
+#include "common.ver"