none
GetType on an CLR object through CLR Hosting fails with access violation 0xC0000005 RRS feed

  • Question

  • Hi,

    I am using the CLR hosting API (2.0 and 4.0) to call arbitrary managed .NET 4 (C#) assemblies from native code (C/C++). I have stumbled across a stubborn problem. I tried hard to reproduce the issue with a standalone simplest code, but to no avail. Below is instead a concise code extract giving hopefully enough relevant context. Things I should highlight are:

    • The loaded assembly MyFacade has several other libraries referenced.
    • I test several objects in MyFacade and dependencies, and can get the type through pObject->GetType(&pType).
    • A SimpleHyperCube object is apparently created, but getting the type fails with the following error
    • "First-chance exception at 0x00000000 in cmdline_rClr.exe: 0xC0000005: Access violation at location 0x0000000000000000. HRESULT:0x40028E8 ". The call stack (using VS2011 beta) is not helpful.
    • The penultimate object queried successfully is quite closely related (another imlpementation of the same interface definition).
    • Perhaps relevant: if I set the Managed Debugger Assistant, 'LoadFromContext' warns me that MyFacade.dll (and references) have been loaded using the LoadFrom context, and that this can result in unexpected behavior for serialization, casting and dependency resolution.

    Things were going well until this issue; I really hope for an enlightened advice... All suggestions (actions, workarounds) are welcome. The main questions I have are:

    • Is the use of Assembly.LoadFrom indeed the root cause of the problem?
    • If so, as I cannot expect assemblies to be in the application path, how do I load them into the default context instead?
    • If I were to use C# to retrieve the type, as a workaround, how do I "cast" an _ObjectPtr to a _TypePtr in C++?
    • As the call stack in VS2011 is not helpful, should I consider tools like WinDbg to help me help myself?

    Regards, J-M

    void testGetType(char * typenameHelper, char * methodName)
    {
    	_ObjectPtr pObject = NULL;
    	variant_t result;
    	_TypePtr pType;
    	BSTR toStr;
    	HRESULT hr;
    	hr = rclr_ms_call_static_method_tname(typenameHelper, methodName, NULL, 0, &result); // result = MyHelper.methodName();
    	hr = rclr_ms_get_raw_object(&result, &pObject); // e.g. using result->pdispVal->QueryInterface(__uuidof(_Object), (void**)ppObject);
    	hr = pObject->GetType(&pType); // Access violation occurs here for one of the objects tested
    	hr = pType->get_FullName(&toStr);
    	bstr_t strType(toStr);
    	printf("Full name of the type: %s\r\n", bstrToCString(&strType));
    
    }
    


    char * facadeTypename = "MyFacadeAssembly.MyHelper,MyFacadeAssembly";
    char * assemblyFname = "C:/path/to/MyFacadeAssembly/bin/Debug/MyFacadeAssembly.dll";
    rclr_load_assembly(&assemblyFname); // loaded using Assembly.LoadFrom(String)
    
    testGetType(facadeTypename, "CreateBasicRngFactory"); // calls public static BasicRngFactory CreateBasicRngFactory()
    testGetType(facadeTypename, "CreateSceParameterDefinition");
    testGetType(facadeTypename, "CreateTimeSeries"); 
    testGetType(facadeTypename, "CreateMpiSysConfig"); 
    // Then, the following object seems created but GetType fails.
    testGetType(facadeTypename, "CreateTestHyperCube"); // public static SimpleHyperCube CreateTestHyperCube()
    





    Thursday, May 3, 2012 8:05 AM

Answers

  • This kind of exception usually indicates a managed memory corruption, for example, the object header of 'pObject' might be over-writed by previous managed object, and filled with invalid memory address. A memory dump of this excpetion will be useful to find out the currupted object from managed heap, but dump analysis is out of support of forum, you can visit the below link to see the various paid support options that are available to better meet your needs.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone  


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Regards,
    Eric Yang
    Microsoft Online Community Support

    • Marked as answer by jm75 Saturday, May 19, 2012 5:42 AM
    Friday, May 18, 2012 9:32 AM

All replies

  • Hi,

    Please check out this post.

    http://www.codeproject.com/Questions/221933/Consume-Managed-DLL-from-Unmanaged-Cplusplus-EXE-S


    Regards, http://shwetamannjain.blogspot.com

    • Proposed as answer by Shweta Jain (Lodha) Friday, May 4, 2012 1:46 PM
    • Unproposed as answer by jm75 Sunday, May 6, 2012 12:14 AM
    Friday, May 4, 2012 1:46 PM
  • Thanks you Shweta,

    Interesting link, and let me to further new information on exposing manage code to native one. Found your blog interesting too, bookmarked...

    While the error messages are similar, they occur in different context. If the issue is identical, it is unclear to me why. The codeproject sample was downright using the wrong type and could not even start the runtime. Basically, using .NET 4.0 hosting interfaces but wrongly expecting the .NET 2.0/3.x
    ICorRuntimeHost.

    As it happens, I do use ICorRuntimeHost too, as per the useful reply from 'eryang' to this post in the same forum

    I am happy to do away with ICorRuntimeHost to see if the issue goes away, but the ICLRRuntimeHost interface just does not seem to provide the same functionalities. If it does, it is woefully underdocumented.

    The lack of documentation is another discussion topic, albeit related. I am still stuck.

    Cheers,

    J-M


    Sunday, May 6, 2012 12:33 AM
  • This kind of exception usually indicates a managed memory corruption, for example, the object header of 'pObject' might be over-writed by previous managed object, and filled with invalid memory address. A memory dump of this excpetion will be useful to find out the currupted object from managed heap, but dump analysis is out of support of forum, you can visit the below link to see the various paid support options that are available to better meet your needs.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone  


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Regards,
    Eric Yang
    Microsoft Online Community Support

    • Marked as answer by jm75 Saturday, May 19, 2012 5:42 AM
    Friday, May 18, 2012 9:32 AM