Results 1 to 3 of 3
  1. #1
    Timm4D's Avatar
    Join Date
    Oct 2018
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    1

    Problem with CLR / It don't work with Runtime v4.0.30319

    I have a C++ dll which is calling my C# code after an injection with CLR.

    So this is my C++ (dll) code:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <metahost.h>
    #include <atlbase.h>
    #include <atlcom.h>
    #pragma comment(lib, "mscoree.lib")
    
    #define IfFailRet(expr)                { hr = (expr); if(FAILED(hr)) return (hr); }
    #define IfNullFail(expr)                { if (!expr) return (E_FAIL); }
    
    extern "C" int __declspec(dllexport) CALLBACK CallClrMethod(
        const WCHAR *AssemblyName,
        const WCHAR *TypeName,
        const WCHAR *MethodName,
        const WCHAR *args,
        LPDWORD pdwResult
    )
    {
        int hr = S_OK;
        CComPtr<ICLRMetaHost> spHost;
        hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&spHost));
        CComPtr<ICLRRuntimeInfo> spRuntimeInfo;
        CComPtr<IEnumUnknown> pRunTimes;
        IfFailRet(spHost->EnumerateInstalledRuntimes(&pRunTimes));
        CComPtr<IUnknown> pUnkRuntime;
        while (S_OK == pRunTimes->Next(1, &pUnkRuntime, 0))
        {
            CComQIPtr<ICLRRuntimeInfo> pp(pUnkRuntime);
            if (pUnkRuntime != nullptr)
            {
                spRuntimeInfo = pp;
                break;
            }
        }
        IfNullFail(spRuntimeInfo);
    
        BOOL bStarted;
        DWORD dwStartupFlags;
        hr = spRuntimeInfo->IsStarted(&bStarted, &dwStartupFlags);
        if (hr != S_OK) // sometimes 0x80004001  not implemented  
        {
            spRuntimeInfo = nullptr; //v4.0.30319 //v2.0.50727
            hr = spHost->GetRuntime(L"v2.0.50727", IID_PPV_ARGS(&spRuntimeInfo));
            bStarted = false;
        }
    
        CComPtr<ICLRRuntimeHost> spRuntimeHost;
        IfFailRet(spRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&spRuntimeHost)));
        if (!bStarted)
        {
            hr = spRuntimeHost->Start();
        }
        hr = spRuntimeHost->ExecuteInDefaultAppDomain(
            AssemblyName,
            TypeName,
            MethodName,
            args,
            pdwResult);
        return hr;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        DWORD dwResult;
        HRESULT hr = CallClrMethod(
            L"D:\\Dev\\CSharpDll\\CSharpDll\\bin\\x64\\Debug\\CSharpDll.dll",
            L"CSharpDll.MainClass",
            L"EntryPoint",
            L"Im successfully called from a C++ dll",
            &dwResult);
        return 0;
    }
    
    BOOL APIENTRY DllMain(HMODULE hModule,
        DWORD  ul_reason_for_call,
        LPVOID lpReserved
    )
    {
        switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
            CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)_tmain, hModule, NULL, NULL);
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
        case DLL_PROCESS_DETACH:
            break;
        }
        return TRUE;
    }
    And this is my C# (dll) code:

    Code:
    using System.Windows.Forms;
    
    namespace CSharpDll
    {
        public class MainClass
        {
            public static int EntryPoint(string MessageFromCppDll)
            {
                MessageBox.Show(MessageFromCppDll);
    
                Form form = new MainForm();
                form.ShowDialog();
    
                return 0;
            }
        }
    }
    So it works very well for the most programs and games, but not at all. On some programs it happens nothing. The injected c++ dll try to run the c# code, but it happen nothing. My theory is that the c++ dll will create no instance of the c# dll.

    But there is one option to fix that: If I change the runtime from v4.0.30319 to v2.0.50727 it works fine.

    It don't work:

    Code:
    hr = spHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&spRuntimeInfo));
    It work:

    Code:
    hr = spHost->GetRuntime(L"v2.0.50727", IID_PPV_ARGS(&spRuntimeInfo));
    And now we come to my "main" problem: I have to decrease the .NET version from 4+ to 3.5 because the CLR V2 only support .NET 2 - 3.5. And this brings problems with my C# (dll) code.

    So now I come to my question(s):

    1. Why is this not working on all programs and games? (I mean with runtime v4.0.30319). With CLR RunTimeHost v2.0.50727 it works.
    2. And how can I fix it or change something in my code to run it.

    If you guys have any ideas or code examples, I would be very grateful.

  2. #2
    MikeRohsoft's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Los Santos
    Posts
    797
    Reputation
    593
    Thanks
    26,314
    I found this, maybe it helps?
    Code:
    IEnumUnknown *installedRuntimes;
    hr = pMetaHost->EnumerateInstalledRuntimes(&installedRuntimes);
    
    ICLRRuntimeInfo *runtimeInfo = NULL;
    ULONG fetched = 0;
    while ((hr = installedRuntimes->Next(1, (IUnknown **)&runtimeInfo, &fetched)) == S_OK && fetched > 0) {
        wchar_t versionString[20];
        DWORD versionStringSize = 20;
        hr = runtimeInfo->GetVersionString(versionString, &versionStringSize);
    
        // Look for the 4.0 runtime
        if (versionStringSize >= 2 && versionString[1] == '4') {
            break;
        }
    }

  3. The Following User Says Thank You to MikeRohsoft For This Useful Post:

    Timm4D (10-25-2018)

  4. #3
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    Might depend on the operating system you're using or trying to run this on. AFAIK Win 7 etc. only have the 3.5 framework installed, later Windows versions come with higher framework versions.

    You could possibly check the Windows Event Log to see if there are any errors logged by the OS/applications

Similar Threads

  1. Survivor Disconnect don't work with the update? (lose item)
    By Firruzedda in forum Dead by Daylight Discussion & Help
    Replies: 0
    Last Post: 03-14-2017, 05:47 AM
  2. [Solved] Keys Don't Work with Crate HALP!
    By KillerViking in forum Realm of the Mad God Private Servers Help
    Replies: 2
    Last Post: 07-11-2016, 06:28 PM
  3. [SOLVED]I play in COD MW2 With Steam The console don't work..
    By F-I-R-E in forum Call of Duty Modern Warfare 2 Help
    Replies: 9
    Last Post: 02-05-2011, 03:53 PM
  4. the old hacks don't work anymore with the new update!
    By zeroknight2000 in forum Combat Arms Hacks & Cheats
    Replies: 5
    Last Post: 08-23-2008, 01:03 AM