|
| 1 | +#include "AssemblyStore.hpp" |
| 2 | +#include <objbase.h> |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +#include <shlwapi.h> |
| 7 | +#pragma comment(lib, "Shlwapi.lib") |
| 8 | + |
| 9 | + |
| 10 | +MyAssemblyStore::MyAssemblyStore(void) |
| 11 | +{ |
| 12 | + count = 0; |
| 13 | + m_targetAssembly = nullptr; |
| 14 | +}; |
| 15 | + |
| 16 | + |
| 17 | +MyAssemblyStore::~MyAssemblyStore(void) |
| 18 | +{ |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | +HRESULT STDMETHODCALLTYPE MyAssemblyStore::QueryInterface(REFIID vTableGuid, void** ppv) |
| 23 | +{ |
| 24 | + if (!IsEqualIID(vTableGuid, IID_IUnknown) && !IsEqualIID(vTableGuid, IID_IHostAssemblyStore)) |
| 25 | + { |
| 26 | + *ppv = 0; |
| 27 | + return E_NOINTERFACE; |
| 28 | + } |
| 29 | + *ppv = this; |
| 30 | + this->AddRef(); |
| 31 | + return S_OK; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +ULONG STDMETHODCALLTYPE MyAssemblyStore::AddRef() |
| 36 | +{ |
| 37 | + return(++((MyAssemblyStore*)this)->count); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +ULONG STDMETHODCALLTYPE MyAssemblyStore::Release() |
| 42 | +{ |
| 43 | + if (--((MyAssemblyStore*)this)->count == 0) |
| 44 | + { |
| 45 | + GlobalFree(this); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + return ((MyAssemblyStore*)this)->count; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +int TargetAssembly::updateTargetAssembly(ICLRAssemblyIdentityManager* identityManager, const std::string& data) |
| 53 | +{ |
| 54 | + m_assembly=data; |
| 55 | + |
| 56 | + if(m_assemblyStream!=nullptr) |
| 57 | + m_assemblyStream->Release(); |
| 58 | + |
| 59 | + m_assemblyStream = SHCreateMemStream((BYTE *)m_assembly.data(), m_assembly.size()); |
| 60 | + identityManager->GetBindingIdentityFromStream(m_assemblyStream, CLR_ASSEMBLY_IDENTITY_FLAGS_DEFAULT, m_assemblyInfo, &m_identityBufferSize); |
| 61 | + |
| 62 | + // std::cout << "updateTargetAssembly " << m_assembly.size() << std::endl; |
| 63 | + // std::wcout << "assemblyInfo " << m_assemblyInfo << std::endl; |
| 64 | + m_id++; |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +int MyAssemblyStore::updateTargetAssembly(ICLRAssemblyIdentityManager* identityManager, const std::string& data) |
| 71 | +{ |
| 72 | + m_targetAssembly->updateTargetAssembly(identityManager, data); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +HRESULT STDMETHODCALLTYPE MyAssemblyStore::ProvideAssembly(AssemblyBindInfo* pBindInfo, UINT64* pAssemblyId, UINT64* pContext, IStream** ppStmAssemblyImage, IStream** ppStmPDB) |
| 80 | +{ |
| 81 | + // std::cout << "MyAssemblyStore::ProvideAssembly " << std::endl; |
| 82 | + // std::wcout << "pBindInfo->lpPostPolicyIdentity " << pBindInfo->lpPostPolicyIdentity << std::endl; |
| 83 | + // std::wcout << "m_targetAssembly->getAssemblyInfo() " << m_targetAssembly->getAssemblyInfo() << std::endl; |
| 84 | + |
| 85 | + // Check if the identity of the assembly being loaded is the one we want |
| 86 | + if (m_targetAssembly!=nullptr && wcscmp(m_targetAssembly->getAssemblyInfo(), pBindInfo->lpPostPolicyIdentity) == 0) |
| 87 | + { |
| 88 | + //This isn't used for anything here so just set it to 0 |
| 89 | + *pContext = 0; |
| 90 | + |
| 91 | + UINT64 id = m_targetAssembly->getId(); |
| 92 | + *pAssemblyId = id; |
| 93 | + |
| 94 | + //Create an IStream using our in-memory assembly bytes and return it to the CLR |
| 95 | + *ppStmAssemblyImage = SHCreateMemStream((BYTE *)m_targetAssembly->getAssembly(), m_targetAssembly->getAssemblySize()); |
| 96 | + return S_OK; |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + // std::wcout <<" !!!!!!!!!!!!!!!!! FAILLLLLLLLLLLLLLED !!!!!!!!!!!" << std::endl; |
| 101 | + |
| 102 | + // If it's not our assembly then tell the CLR to handle it |
| 103 | + return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +// This shouldn't really get called but if it does we'll just tell the CLR to find it |
| 108 | +HRESULT STDMETHODCALLTYPE MyAssemblyStore::ProvideModule(ModuleBindInfo* pBindInfo, DWORD* pdwModuleId, IStream** ppStmModuleImage, IStream** ppStmPDB) |
| 109 | +{ |
| 110 | + // std::cout << "MyAssemblyStore::ProvideModule" << std::endl; |
| 111 | + |
| 112 | + //Tell the CLR to handle this |
| 113 | + return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); |
| 114 | +} |
0 commit comments