Skip to content

Commit

Permalink
Sample code to mock Static methods in sealed classes
Browse files Browse the repository at this point in the history
Sample code showing a very basic version of how TypeMock works
  • Loading branch information
mattwarren committed Aug 11, 2014
1 parent 270ef3b commit 9f804ce
Show file tree
Hide file tree
Showing 16 changed files with 601 additions and 296 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2012 for Windows Desktop
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DDDProfiler", "DDDProfiler\DDDProfiler.vcxproj", "{4433C836-63ED-47B5-ABD4-192945EF2AFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProfilerHost", "ProfilerHost\ProfilerHost.csproj", "{70125EF7-1BBA-4508-8900-1D176D725368}"
ProjectSection(ProjectDependencies) = postProject
{4433C836-63ED-47B5-ABD4-192945EF2AFA} = {4433C836-63ED-47B5-ABD4-192945EF2AFA}
{7ADB7181-7442-4115-9E92-F703A0357C56} = {7ADB7181-7442-4115-9E92-F703A0357C56}
{82F88AB6-1CF9-4C5F-A711-AD298FC2BD6F} = {82F88AB6-1CF9-4C5F-A711-AD298FC2BD6F}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProfilerTarget", "ProfilerTarget\ProfilerTarget.csproj", "{7ADB7181-7442-4115-9E92-F703A0357C56}"
EndProject
Expand Down
360 changes: 249 additions & 111 deletions step5_main_injected_method_object_array/DDDProfiler/CodeInjection.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ END_COM_MAP()

private:
CComQIPtr<ICorProfilerInfo3> m_profilerInfo3;
mdMemberRef m_targetMethodRefLog;
mdMemberRef m_targetMethodRefLogAfter;
mdMemberRef m_targetMethodRefMocked;
mdMemberRef m_targetMethodRefShouldMock;
mdTypeRef m_objectTypeRef;

std::wstring GetMethodName(FunctionID functionId, ModuleID& moduleId, mdToken& funcToken);
mdMemberRef m_targetMethodRef;
HRESULT GetInjectedRef(ModuleID moduleId, mdModuleRef &mscorlibRef);
HRESULT GetMsCorlibRef(ModuleID moduleId, mdModuleRef &mscorlibRef);
mdTypeRef m_objectTypeRef;

HRESULT STDMETHODCALLTYPE AddLoggingToMethod(ModuleID moduleId, FunctionID functionId, mdToken funcToken);
HRESULT STDMETHODCALLTYPE AddMockingToMethod(ModuleID moduleId, FunctionID functionId, mdToken funcToken, std::wstring methodName);
public:
virtual HRESULT STDMETHODCALLTYPE Initialize(
/* [in] */ IUnknown *pICorProfilerInfoUnk);

virtual HRESULT STDMETHODCALLTYPE Shutdown( void);
virtual HRESULT STDMETHODCALLTYPE Shutdown(void);

virtual HRESULT STDMETHODCALLTYPE ModuleAttachedToAssembly(
/* [in] */ ModuleID moduleId,
Expand All @@ -60,8 +67,6 @@ END_COM_MAP()
virtual HRESULT STDMETHODCALLTYPE JITCompilationStarted(
/* [in] */ FunctionID functionId,
/* [in] */ BOOL fIsSafeToBlock);


};

OBJECT_ENTRY_AUTO(__uuidof(CodeInjection), CCodeInjection)
24 changes: 10 additions & 14 deletions step5_main_injected_method_object_array/DDDProfiler/DDDProfiler.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
// DDDProfiler.cpp : Implementation of DLL Exports.


#include "stdafx.h"
#include "resource.h"
#include "DDDProfiler_i.h"
#include "dllmain.h"
#include "xdlldata.h"


// Used to determine whether the DLL can be unloaded by OLE.
STDAPI DllCanUnloadNow(void)
{
#ifdef _MERGE_PROXYSTUB
#ifdef _MERGE_PROXYSTUB
HRESULT hr = PrxDllCanUnloadNow();
if (hr != S_OK)
return hr;
#endif
return _AtlModule.DllCanUnloadNow();
}
return _AtlModule.DllCanUnloadNow();
}

// Returns a class factory to create an object of the requested type.
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
#ifdef _MERGE_PROXYSTUB
#ifdef _MERGE_PROXYSTUB
if (PrxDllGetClassObject(rclsid, riid, ppv) == S_OK)
return S_OK;
#endif
return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}

// DllRegisterServer - Adds entries to the system registry.
STDAPI DllRegisterServer(void)
{
// registers object, typelib and all interfaces in typelib
HRESULT hr = _AtlModule.DllRegisterServer();
#ifdef _MERGE_PROXYSTUB
#ifdef _MERGE_PROXYSTUB
if (FAILED(hr))
return hr;
hr = PrxDllRegisterServer();
#endif
return hr;
return hr;
}

// DllUnregisterServer - Removes entries from the system registry.
STDAPI DllUnregisterServer(void)
{
HRESULT hr = _AtlModule.DllUnregisterServer();
#ifdef _MERGE_PROXYSTUB
#ifdef _MERGE_PROXYSTUB
if (FAILED(hr))
return hr;
hr = PrxDllRegisterServer();
if (FAILED(hr))
return hr;
hr = PrxDllUnregisterServer();
#endif
return hr;
return hr;
}

// DllInstall - Adds/Removes entries to the system registry per user per machine.
Expand Down Expand Up @@ -85,6 +83,4 @@ STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
}

return hr;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -20,12 +20,14 @@
<UseDebugLibraries>true</UseDebugLibraries>
<UseOfAtl>Dynamic</UseOfAtl>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<UseOfAtl>Dynamic</UseOfAtl>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ Instruction::Instruction(CanonicalName operation)
m_origOffset = -1;
}

Instruction::Instruction(CanonicalName operation, Instruction *branchLocation)
{
m_operation = operation;
m_operand = 0;
m_offset = -1;
m_isBranch = true;
m_origOffset = -1;
// Seems like we can just push 0 here, it's the m_branches that gets used?!?!
// But if we don't add it something to m_branchOffsets, it'll throw an exception!!!
m_branchOffsets.push_back(0);
m_branches.push_back(branchLocation);
}

Instruction::~Instruction(void)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ class Instruction
public:
Instruction(CanonicalName operation, ULONGLONG operand);
Instruction(CanonicalName operation);
Instruction(CanonicalName operation, Instruction *branchLocation);
~Instruction(void);
protected:
Instruction(void);
Instruction& operator = (const Instruction& b);

#ifdef TEST_FRAMEWORK
//#ifdef TEST_FRAMEWORK
public:
#else
private:
#endif
//#else
//private:
//#endif
long m_offset;
CanonicalName m_operation;
ULONGLONG m_operand;
Expand All @@ -40,6 +41,4 @@ class Instruction
public:

friend class Method;
};


};
Loading

0 comments on commit 9f804ce

Please sign in to comment.