aboutsummaryrefslogtreecommitdiffhomepage
path: root/ext/detours/samples/commem
diff options
context:
space:
mode:
Diffstat (limited to 'ext/detours/samples/commem')
-rw-r--r--ext/detours/samples/commem/Makefile48
-rw-r--r--ext/detours/samples/commem/commem.cpp114
2 files changed, 162 insertions, 0 deletions
diff --git a/ext/detours/samples/commem/Makefile b/ext/detours/samples/commem/Makefile
new file mode 100644
index 0000000..70cdd59
--- /dev/null
+++ b/ext/detours/samples/commem/Makefile
@@ -0,0 +1,48 @@
+##############################################################################
+##
+## Makefile for Detours Test Programs.
+##
+## Microsoft Research Detours Package
+##
+## Copyright (c) Microsoft Corporation. All rights reserved.
+##
+
+!include ..\common.mak
+
+LIBS=$(LIBS) kernel32.lib
+
+all: dirs \
+ $(BIND)\commem.exe \
+!IF $(DETOURS_SOURCE_BROWSING)==1
+ $(OBJD)\commem.bsc
+!ENDIF
+
+clean:
+ -del *~ *.obj *.sbr 2> nul
+ -del $(BIND)\commem.* 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)
+
+$(BIND)\commem.obj : commem.cpp
+
+$(BIND)\commem.exe : $(OBJD)\commem.obj $(DEPS)
+ cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb $(OBJD)\commem.obj \
+ /link $(LINKFLAGS) $(LIBS) ole32.lib /subsystem:console
+
+$(OBJD)\commem.bsc : $(OBJD)\commem.obj
+ bscmake /v /n /o $@ $(OBJD)\commem.sbr
+
+##############################################################################
+
+test: $(BIND)\commem.exe
+ @echo.
+ $(BIND)\commem.exe
+ @echo.
+
+################################################################# End of File.
diff --git a/ext/detours/samples/commem/commem.cpp b/ext/detours/samples/commem/commem.cpp
new file mode 100644
index 0000000..6779843
--- /dev/null
+++ b/ext/detours/samples/commem/commem.cpp
@@ -0,0 +1,114 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// Detour functions of a COM interface (commem.cpp of commem.exe)
+//
+// Microsoft Research Detours Package
+//
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+//
+//
+#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>
+
+//////////////////////////////////////////////////////////////////////////////
+//
+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;
+ }
+
+ printf("commem: %p->IStreamWrite(pv=%p, cb=%ld)\n", This, pv, cb);
+ hr = RealIStreamWrite(This, pv, cb, pcbWritten);
+ printf("commem: %p->IStreamWrite -> %08lx (pcbWritten=%ld)\n", This, hr, *pcbWritten);
+
+ return hr;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+//
+int main(int argc, char **argv)
+{
+ HRESULT hr;
+
+ (void)argc;
+ (void)argv;
+
+ LPSTREAM pStream = NULL;
+ ULARGE_INTEGER ul;
+ LARGE_INTEGER li;
+
+ CoInitialize(NULL);
+
+ hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
+
+ RealIStreamWrite = pStream->lpVtbl->Write;
+
+ ul.QuadPart = 512;
+ hr = pStream->lpVtbl->SetSize(pStream, ul);
+ li.QuadPart = 0;
+ hr = pStream->lpVtbl->Seek(pStream, li, STREAM_SEEK_SET, NULL);
+
+ printf("commem: Calling Write w/o before attach.\n");
+
+ li.QuadPart = 0;
+ hr = pStream->lpVtbl->Write(pStream, &ul, sizeof(ul), NULL);
+
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ DetourAttach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
+ DetourTransactionCommit();
+
+ printf("commem: Calling Write w/o after attach.\n");
+
+ li.QuadPart = 1;
+ hr = pStream->lpVtbl->Write(pStream, &li, sizeof(li), NULL);
+
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ DetourDetach(&(PVOID&)RealIStreamWrite, MineIStreamWrite);
+ DetourTransactionCommit();
+
+ printf("commem: Calling Write w/o after detach.\n");
+
+ li.QuadPart = 2;
+ hr = pStream->lpVtbl->Write(pStream, &li, sizeof(li), NULL);
+
+ hr = pStream->lpVtbl->Release(pStream);
+ pStream = NULL;
+
+ CoUninitialize();
+
+ return 0;
+}
+