Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Spying on .NET Applications


Spying on .NET Applications

Listing 1 The Bind routine


//Bind routine works in the address space of the target process.
//It uses CLR hosting interfaces to inject an agent into the CLR environment.
enum HookErrors Bind()
{
    TRACE("Bind()\n");
    ICorRuntimeHost *l_pHost = NULL;
    HCORENUM l_hEnum = NULL;
    IUnknown * l_pUnknown = NULL;
    mscorlib::_AppDomain * l_pDomain = NULL;
    enum HookErrors l_eError = eNoErrors;
    try
    {
        LPWSTR l_pszVersion = NULL;
        HINSTANCE l_hMscoree = GetModuleHandle("mscoree");
        if(l_hMscoree == NULL)
        {
            TRACE("GetModuleHandle(mscoree) returned NULL\n");
            return eNoClrRuntime;
        }
        T_CorBindToRuntimeEx * l_pCorBindToRuntimeEx = (T_CorBindToRuntimeEx *)
            GetProcAddress(l_hMscoree, "CorBindToRuntimeEx");
        if(l_pCorBindToRuntimeEx == NULL)
        {
            TRACE("GetProcAddress(CorBindToRuntimeEx) returned NULL\n");
            return eCannotGetAddrCorBindToRuntimeEx;
        }
        HRESULT l_hResult = l_pCorBindToRuntimeEx(l_pszVersion, 
                                NULL, 
                                NULL,
                                CLSID_CorRuntimeHost, 
                                IID_ICorRuntimeHost, 
                                (void **)&l_pHost);
        if(FAILED(l_hResult) || !l_pHost)
        {
            TRACE("CorBindToRuntimeEx() failed. Result %d\n", l_hResult);
            return eCannotGetRuntimeHost;
        }
        l_hResult = l_pHost->EnumDomains(&l_hEnum);
        if (SUCCEEDED(l_hResult))
        {
            while(SUCCEEDED(l_hResult = 
                l_pHost->NextDomain(l_hEnum, &l_pUnknown)) && l_pUnknown) 
            {//l_pUnknown is System.AppDomain interface
                l_pUnknown->QueryInterface(__uuidof(mscorlib::_AppDomain), 
                    (void**)&l_pDomain);

                if(l_pDomain) 
                {
                    _bstr_t l_Path(g_szAssemblyPath);
                    _bstr_t l_Name(g_szAgentClass);

                    SAFEARRAY * l_pArray;
                    SAFEARRAYBOUND l_Bounds[1];
                    l_Bounds[0].lLbound = 0;
                    l_Bounds[0].cElements = 1;
                    l_pArray = SafeArrayCreate(VT_VARIANT, 1, l_Bounds);
                    if(l_pArray == NULL)
                    {
                        TRACE("SafeArrayCreate failed\n");
                        l_eError = eSafeArrayFailure;
                    }
                    else
                    {
                        long l_Index;
                        _variant_t l_Element(g_szServerURL);
                        l_Index = 0;
                        l_hResult = SafeArrayPutElement
                            (l_pArray, &l_Index, &l_Element);
                        if(FAILED(l_hResult))
                        {
                            TRACE("SafeArrayPutElement failed. Result %d\n",
                                l_hResult);
                            l_eError = eSafeArrayFailure;
                        }
                        else
                        {
                            TRACE("Before CreateInstanceFrom_3\n");
                            mscorlib::_ObjectHandlePtr l_Handle = 
                                l_pDomain->CreateInstanceFrom_3(l_Path,  			  l_Name,
                                    VARIANT_TRUE, 
                                    mscorlib::BindingFlags_Default,
                                    NULL/*Binder*/,
                                    l_pArray, NULL/*culture*/, 
                                    NULL/*activationAttributes*/,
                                    NULL/*securityAttributes*/);
                            TRACE("CreateInstanceFrom_3 ret %x\n", l_Handle);
                            l_eError = l_Handle? 
                                eNoErrors : eCreateInstanceFailed;
                        }
                        SafeArrayDestroy(l_pArray);
                    }
                    l_pDomain->Release();
                    l_pDomain = NULL;
                }//if(l_pDomain) 
                l_pUnknown->Release();
                l_pUnknown = NULL;
            }//while enum
            l_pHost->CloseEnum(l_hEnum);
            l_hEnum = NULL;
        }//if (SUCCEEDED(l_hResult))
        else
        {
            TRACE("EnumDomains() failed. Result %d\n", l_hResult);
            l_eError = eEnumDomainsFailed;
        }
    }
    catch(...)
    {
        TRACE("Exception in Bind()\n");
        l_eError = eException;
    }
    try
    {
        if(l_pHost)
        {
            if(l_pDomain)
                l_pDomain->Release();
            if(l_pUnknown)
                l_pUnknown->Release();
            if(l_hEnum)
                l_pHost->CloseEnum(l_hEnum);
            l_pHost->Release();
        }
    }
    catch(...)
    {
    }
    TRACE("Bind() returns %d\n", l_eError);
    return l_eError;
}//Bind



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.