C++ ExecuteInDefaultAppDomain from memory? menu

Shout-Out

User Tag List

Results 1 to 6 of 6
  1. #1
    jimjonesOC's Avatar Member
    Reputation
    1
    Join Date
    Jul 2014
    Posts
    5
    Thanks G/R
    4/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C++ ExecuteInDefaultAppDomain from memory?

    Hi,

    I want to load a .NET Dll into a process. I am using this code which works fine:

    Code:
    #include <Windows.h>
    #include "MSCorEE.h"
    #include <metahost.h>
    #pragma unmanaged
    
    #pragma comment(lib, "mscoree.lib")
    
    void StartDotNet();
    
    BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReeserved)
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    		CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&StartDotNet, 0, 0, NULL);
    		break;
    	case DLL_THREAD_ATTACH:
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
    	return TRUE;
    }
    
    void StartDotNet()
    {
    	HRESULT hr;
    	ICLRRuntimeHost *pClrHost = NULL;
    	ICLRMetaHost *pMetaHost = NULL;
    	hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
    	ICLRRuntimeInfo * lpRuntimeInfo = NULL;
    	hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*)&lpRuntimeInfo);
    	ICLRRuntimeHost * lpRuntimeHost = NULL;
    	hr = lpRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID *)&lpRuntimeHost);
    	hr = lpRuntimeHost->Start();
    	DWORD dwRet = 0;
    
    	hr = lpRuntimeHost->ExecuteInDefaultAppDomain(
    		L"C:\\test\\Test.dll",
    		L"Test.Class1", L"EntryPoint", L"it works", &dwRet);
    
    	lpRuntimeHost->Release();
    }
    Since I don't know where users extracts the hack and the Test.dll I cannot use a constant path like "C:\\test\\Test.dll". Using a relative path means I have to place the Dll in the game folder which would work but I prefer a solution where the hack dlls stay in their own folder.

    Is there any way to load the Test.dll from memory and use it in ExecuteInDefaultAppDomain?
    Or are there even better approaches?

    Thanks!

    C++ ExecuteInDefaultAppDomain from memory?
  2. #2
    Corthezz's Avatar Elite User Authenticator enabled
    Reputation
    386
    Join Date
    Nov 2011
    Posts
    325
    Thanks G/R
    191/98
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BOOL WINAPI DllMain(HMODULE hDll, DWORD dwReason, LPVOID lpReserved)

    hDll stores the path to the dll.
    Check my blog: https://zzuks.blogspot.com

  3. Thanks jimjonesOC, ev0 (2 members gave Thanks to Corthezz for this useful post)
  4. #3
    jimjonesOC's Avatar Member
    Reputation
    1
    Join Date
    Jul 2014
    Posts
    5
    Thanks G/R
    4/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, this works!

  5. #4
    lolp1's Avatar Site Donator CoreCoins Purchaser
    Reputation
    190
    Join Date
    Feb 2013
    Posts
    210
    Thanks G/R
    43/77
    Trade Feedback
    3 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not exactly sure your goals, but if you are using C++ only to inject your .net module it's not really needed. I wrote this for just this reason - http://www.ownedcore.com/forums/worl...ls-easier.html (Injection Sharp - Inject your .net coded internals easier.). Simply point and click, the only issue is that version is broken for x64 bit due to the injector I wrote sucking, the dll works fine in x64.

    You can also just make your own version - by using this(credits iceflake/scorpia for the class? I forget now):

    https://github.com/lolp1/DomainWrapp.../DomainWrapper

    In PatchedDomainLoader, you can either hardcore this value to yours:
    Code:
       internal static string ApplicationToHostName = "WhiteVex";
     [MarshalAs(UnmanagedType.LPWStr)] internal static string ApplicationToHostDirectory = @"C:\Users\lol[1\Documents\Visual Studio 2015\Projects\WhiteVex\WhiteVex\bin\Debug\";
    Or you can have it dynamicly set and then load the module - by calling this export with the desired settings:

    Code:
     [DllExport("LoadDomainSettings", CallingConvention.Cdecl)]
            public static void LoadDomainHostSettings(string loadDirectory, string applicationName)
            {
                ApplicationToHostDirectory = loadDirectory;
                ApplicationToHostName = applicationName;
            }
    I personally have a version built into all of my C# internal stuff to simply auto-inject it's self into the selected process, and just skip the whole gui or hard coding of any kind now. I am really lazy..

  6. Thanks ev0 (1 members gave Thanks to lolp1 for this useful post)
  7. #5
    jimjonesOC's Avatar Member
    Reputation
    1
    Join Date
    Jul 2014
    Posts
    5
    Thanks G/R
    4/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't tested it yet but does it also work for injecting a managed DLL into an unmanaged process?
    How can I execute a method of the injected DLL after injection?

  8. #6
    lolp1's Avatar Site Donator CoreCoins Purchaser
    Reputation
    190
    Join Date
    Feb 2013
    Posts
    210
    Thanks G/R
    43/77
    Trade Feedback
    3 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by jimjonesOC View Post
    I haven't tested it yet but does it also work for injecting a managed DLL into an unmanaged process?
    Yes it does. In fact, that is all it works for as that is what it's designed for.

    Originally Posted by jimjonesOC View Post
    IHow can I execute a method of the injected DLL after injection?
    If the method is a dll export, which the above are you can call them easily with our with out third party tools. I personally wrote TealVex for mainly this reason (calling dll exports easy) - http://www.ownedcore.com/forums/worl...ted-stuff.html

    MemorySharp and ExtremeInjector also do the job.

    https://github.com/ZenLulz/MemorySharp

    An example of how I do it in InjectionSharp using the MemorySharp lib:

    Code:
          private void injectButton_Click(object sender, EventArgs e)
            {
                try
                {
                    SetUpMemorySharpInstance();
                    var applicationName = FormattedApplicationName();
                    var applicationToHostPath = FormatedApplicationToHostPath();
                    var dllName = domainHostDllNameBox.Text;
                    var dllPath = FormatedDomainHostDllPath(applicationToHostPath);
                    MemorySharp.Modules.Inject(dllPath);
                    // Call exported method to set settings.
                    MemorySharp[dllName]["LoadDomainSettings"].Execute(CallingConventions.Cdecl, applicationToHostPath,
                        applicationName);
                    // Host.
                    MemorySharp[dllName]["HostDomain"].Execute(CallingConventions.Cdecl);
                }
                catch (Exception exception)
                {
                    HandleException(exception);
                }
            }
    ExtremeInjector supports calling DllExports upon injection:
    [Release] Extreme Injector v3.5.2 by master131

    If you're x64 bit I suggest extreme injector or writing your own quick solution using the above. if x32 injection sharp is as easy as it gets lol.

  9. Thanks ev0, jimjonesOC (2 members gave Thanks to lolp1 for this useful post)

Similar Threads

  1. Cruising the spell cache from memory
    By amadmonk in forum WoW Memory Editing
    Replies: 5
    Last Post: 12-08-2009, 08:38 PM
  2. Pulling Entities from memory
    By suicidity in forum MMO Exploits|Hacks
    Replies: 20
    Last Post: 01-29-2009, 11:40 AM
  3. [Guide][VB.NET] Read a string from memory
    By Gothian in forum Programming
    Replies: 14
    Last Post: 08-18-2008, 04:39 PM
  4. [Guide][VB.NET] Reading a String From Memory
    By Gothian in forum WoW Memory Editing
    Replies: 14
    Last Post: 01-18-2008, 12:08 PM
All times are GMT -5. The time now is 05:48 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search