Skip to content

Instantly share code, notes, and snippets.

@GilesBathgate
Last active July 10, 2023 15:31
Show Gist options
  • Save GilesBathgate/84e1e37235f7669c32860a56247e26d4 to your computer and use it in GitHub Desktop.
Save GilesBathgate/84e1e37235f7669c32860a56247e26d4 to your computer and use it in GitHub Desktop.
module interop.net;
import core.sys.windows.windows;
import core.sys.windows.com;
import core.sys.windows.objidl;
import core.sys.windows.oaidl;
import core.sys.windows.wtypes;
pragma(lib, "mscoree.lib");
extern (Windows)
{
enum CLSID_CorRuntimeHost = GUID(0xcb2f6723, 0xab3a, 0x11d2, [0x9c, 0x40,
0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e]);
enum IID_ICorRuntimeHost = GUID(0xcb2f6722, 0xab3a, 0x11d2, [0x9c, 0x40,
0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e]);
enum CLSID_CLRMetaHost = GUID(0x9280188d, 0xe8e, 0x4867, [0xb3, 0xc, 0x7f,
0xa8, 0x38, 0x84, 0xe8, 0xde]);
enum IID_ICLRMetaHost = GUID(0xD332DB9E, 0xB9B3, 0x4125, [0x82, 0x07, 0xA1,
0x48, 0x84, 0xF5, 0x32, 0x16]);
enum IID_ICLRRuntimeInfo = GUID(0xBD39D1D2, 0xBA2F, 0x486a, [0x89, 0xB0,
0xB4, 0xB0, 0xCB, 0x46, 0x68, 0x91]);
enum IID_AppDomain = GUID(0x05F696DC, 0x2B29, 0x3663, [0xAD, 0x8B, 0xC4,
0x38, 0x9C, 0xF2, 0xA7, 0x13]);
HRESULT CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID* ppInterface);
alias void function(void* pRuntimeInfo, void* pfnCallbackThreadSet,
void* pfnCallbackThreadUnset) RuntimeLoadedCallbackFnPtr;
}
interface ICLRMetaHost : IUnknown
{
extern (Windows):
HRESULT GetRuntime(LPCWSTR pwzVersion, REFIID riid, LPVOID* ppRuntime);
HRESULT GetVersionFromFile(LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD* pcchBuffer);
HRESULT EnumerateInstalledRuntimes(IEnumUnknown** ppEnumerator);
HRESULT EnumerateLoadedRuntimes(HANDLE hndProcess, IEnumUnknown** ppEnumerator);
HRESULT RequestRuntimeLoadedNotification(RuntimeLoadedCallbackFnPtr pCallbackFunction);
HRESULT QueryLegacyV2RuntimeBinding(REFIID riid, LPVOID* ppUnk);
HRESULT ExitProcess(INT32 iExitCode);
}
interface ICLRRuntimeInfo : IUnknown
{
extern (Windows):
HRESULT GetVersionString(LPWSTR pwzBuffer, DWORD* pcchBuffer);
HRESULT GetRuntimeDirectory(LPWSTR pwzBuffer, DWORD* pcchBuffer);
HRESULT IsLoaded(HANDLE hndProcess, BOOL* pbLoaded);
HRESULT LoadErrorString(UINT iResourceID, LPWSTR pwzBuffer, DWORD* pcchBuffer, LONG iLocaleID);
HRESULT LoadLibrary(LPCWSTR pwzDllName, HMODULE* phndModule);
HRESULT GetProcAddress(LPCSTR pszProcName, LPVOID* ppProc);
HRESULT GetInterface(REFCLSID rclsid, REFIID riid, LPVOID* ppUnk);
HRESULT IsLoadable(BOOL* pbLoadable);
HRESULT SetDefaultStartupFlags(DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile);
HRESULT GetDefaultStartupFlags(DWORD* pdwStartupFlags,
LPWSTR pwzHostConfigFile, DWORD* pcchHostConfigFile);
HRESULT BindAsLegacyV2Runtime();
HRESULT IsStarted(BOOL* pbStarted, DWORD* pdwStartupFlags);
}
interface ICorConfiguration : IUnknown
{
}
alias void* HDOMAINENUM;
interface ICorRuntimeHost : IUnknown
{
extern (Windows):
HRESULT CreateLogicalThreadState();
HRESULT DeleteLogicalThreadState();
HRESULT SwitchInLogicalThreadState(DWORD* pFiberCookie);
HRESULT SwitchOutLogicalThreadState(DWORD** pFiberCookie);
HRESULT LocksHeldByLogicalThread(DWORD* pCount);
HRESULT MapFile(HANDLE hFile, HMODULE* hMapAddress);
HRESULT GetConfiguration(ICorConfiguration** pConfiguration);
HRESULT Start();
HRESULT Stop();
HRESULT CreateDomain(LPCWSTR pwzFriendlyName, IUnknown* pIdentityArray,
IUnknown** pAppDomain);
HRESULT GetDefaultDomain(IUnknown** pAppDomain);
HRESULT EnumDomains(HDOMAINENUM* hEnum);
HRESULT NextDomain(HDOMAINENUM hEnum, IUnknown** pAppDomain);
HRESULT CloseEnum(HDOMAINENUM hEnum);
HRESULT CreateDomainEx(LPCWSTR pwzFriendlyName, IUnknown* pSetup,
IUnknown* pEvidence, IUnknown** pAppDomain);
HRESULT CreateDomainSetup(IUnknown** pAppDomainSetup);
HRESULT CreateEvidence(IUnknown** pEvidence);
HRESULT UnloadDomain(IUnknown* pAppDomain);
HRESULT CurrentDomain(IUnknown** pAppDomain);
}
//{17156360-2F1A-384A-BC52-FDE93C215C5B}
interface Assembly : IDispatch
{
extern (Windows):
HRESULT ToString();
HRESULT Equals();
HRESULT GetHashCode();
HRESULT GetType();
HRESULT get_CodeBase();
HRESULT get_EscapedCodeBase();
HRESULT GetName();
HRESULT GetName_2();
HRESULT get_FullName();
HRESULT get_EntryPoint();
HRESULT GetType_2(BSTR name, IUnknown* retVal);
HRESULT GetType_3();
HRESULT GetExportedTypes();
HRESULT GetTypes();
HRESULT GetManifestResourceStream();
HRESULT GetManifestResourceStream_2();
HRESULT GetFile();
HRESULT GetFiles();
HRESULT GetFiles_2();
HRESULT GetManifestResourceNames();
HRESULT GetManifestResourceInfo();
HRESULT get_Location();
HRESULT get_Evidence();
HRESULT GetCustomAttributes();
HRESULT GetCustomAttributes_2();
HRESULT IsDefined();
HRESULT GetObjectData();
HRESULT add_ModuleResolve();
HRESULT remove_ModuleResolve();
HRESULT GetType_4();
HRESULT GetSatelliteAssembly();
HRESULT GetSatelliteAssembly_2();
HRESULT LoadModule();
HRESULT LoadModule_2();
HRESULT CreateInstance(BSTR bstrClassName, VARIANT* vtObject);
}
interface _AppDomain : IUnknown
{
extern (Windows):
HRESULT GetTypeInfoCount();
HRESULT GetTypeInfo();
HRESULT GetIDsOfNames();
HRESULT Invoke();
HRESULT get_ToString();
HRESULT Equals();
HRESULT GetHashCode();
HRESULT GetType();
HRESULT InitializeLifetimeService();
HRESULT GetLifetimeService();
HRESULT Get_Evidence();
HRESULT add_DomainUnload();
HRESULT remove_DomainUnload();
HRESULT add_AssemblyLoad();
HRESULT remove_AssemblyLoad();
HRESULT add_ProcessExit();
HRESULT remove_ProcessExit();
HRESULT add_TypeResolve();
HRESULT remove_TypeResolve();
HRESULT add_ResourceResolve();
HRESULT remove_ResourceResolve();
HRESULT add_AssemblyResolve();
HRESULT remove_AssemblyResolve();
HRESULT add_UnhandledException();
HRESULT remove_UnhandledException();
HRESULT DefineDynamicAssembly();
HRESULT DefineDynamicAssembly_2();
HRESULT DefineDynamicAssembly_3();
HRESULT DefineDynamicAssembly_4();
HRESULT DefineDynamicAssembly_5();
HRESULT DefineDynamicAssembly_6();
HRESULT DefineDynamicAssembly_7();
HRESULT DefineDynamicAssembly_8();
HRESULT DefineDynamicAssembly_9();
HRESULT CreateInstance();
HRESULT CreateInstanceFrom();
HRESULT CreateInstance_2();
HRESULT CreateInstanceFrom_2();
HRESULT CreateInstance_3();
HRESULT CreateInstanceFrom_3();
HRESULT Load();
HRESULT Load_2(BSTR assemblyString, Assembly* retVal);
}
enum BindingFlags
{
Default = 0,
IgnoreCase = 1,
DeclaredOnly = 2,
Instance = 4,
Static = 8,
Public = 16,
NonPublic = 32,
FlattenHierarchy = 64,
InvokeMethod = 256,
CreateInstance = 512,
GetField = 1024,
SetField = 2048,
GetProperty = 4096,
SetProperty = 8192,
PutDispProperty = 16384,
PutRefDispProperty = 32768,
ExactBinding = 65536,
SuppressChangeType = 131072,
OptionalParamBinding = 262144,
IgnoreReturn = 16777216
}
interface _Binder : IDispatch
{
}
interface _Type : IUnknown
{
HRESULT GetTypeInfoCount();
HRESULT GetTypeInfo();
HRESULT GetIDsOfNames();
HRESULT Invoke();
HRESULT ToString();
HRESULT Equals();
HRESULT GetHashCode();
HRESULT GetType();
HRESULT get_MemberType();
HRESULT get_name();
HRESULT get_DeclaringType();
HRESULT get_ReflectedType();
HRESULT GetCustomAttributes();
HRESULT GetCustomAttributes_2();
HRESULT IsDefined();
HRESULT get_Guid();
HRESULT get_Module();
HRESULT get_Assembly();
HRESULT get_TypeHandle();
HRESULT get_FullName();
HRESULT get_Namespace();
HRESULT get_AssemblyQualifiedName();
HRESULT GetArrayRank();
HRESULT get_BaseType();
HRESULT GetConstructors();
HRESULT GetInterface();
HRESULT GetInterfaces();
HRESULT FindInterfaces();
HRESULT GetEvent();
HRESULT GetEvents();
HRESULT GetEvents_2();
HRESULT GetNestedTypes();
HRESULT GetNestedType();
HRESULT GetMember();
HRESULT GetDefaultMembers();
HRESULT FindMembers();
HRESULT GetElementType();
HRESULT IsSubclassOf();
HRESULT IsInstanceOfType();
HRESULT IsAssignableFrom();
HRESULT GetInterfaceMap();
HRESULT GetMethod();
HRESULT GetMethod_2();
HRESULT GetMethods();
HRESULT GetField();
HRESULT GetFields();
HRESULT GetProperty();
HRESULT GetProperty_2();
HRESULT GetProperties();
HRESULT GetMember_2();
HRESULT GetMembers();
HRESULT InvokeMember();
HRESULT get_UnderlyingSystemType();
HRESULT InvokeMember_2();
HRESULT InvokeMember_3(BSTR name, BindingFlags invokeAttr, _Binder* Binder,
VARIANT Target, VARIANT args, VARIANT* pRetVal);
}
import std.utf;
import std.conv;
void main()
{
auto clsid = CLSID_CLRMetaHost;
auto iid = IID_ICLRMetaHost;
ICLRMetaHost pMetaHost;
auto hr = CLRCreateInstance(&clsid, &iid, cast(void**)&pMetaHost);
ICLRRuntimeInfo pRuntimeInfo;
iid = IID_ICLRRuntimeInfo;
auto pszVersion = "v4.0.30319".toUTF16z;
hr = pMetaHost.GetRuntime(pszVersion, &iid, cast(void**)&pRuntimeInfo);
BOOL fLoadable;
hr = pRuntimeInfo.IsLoadable(&fLoadable);
if (!fLoadable)
return;
ICorRuntimeHost pCorRuntimeHost;
clsid = CLSID_CorRuntimeHost;
iid = IID_ICorRuntimeHost;
hr = pRuntimeInfo.GetInterface(&clsid, &iid, cast(void**)&pCorRuntimeHost);
hr = pCorRuntimeHost.Start();
IUnknown spAppDomainThunk;
hr = pCorRuntimeHost.GetDefaultDomain(cast(IUnknown**)&spAppDomainThunk);
_AppDomain spDefaultAppDomain;
iid = IID_AppDomain;
hr = spAppDomainThunk.QueryInterface(&iid, cast(void**)&spDefaultAppDomain);
import interop.bstring;
Assembly assembly;
auto assemblyName = bstring(
"System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
hr = spDefaultAppDomain.Load_2(assemblyName, cast(Assembly*)&assembly);
auto name = bstring("System.Windows.Forms.Form");
_Type type;
hr = assembly.GetType_2(name, cast(IUnknown*)&type);
VARIANT object;
hr = assembly.CreateInstance(name, cast(VARIANT*)&object);
VARIANT result;
VARIANT empty;
type.InvokeMember_3(bstring("ShowDialog"),
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, object, empty, cast(VARIANT*)&result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment