0

I have created a C# code with output type as netmodule. This is being used in a C++ code.

I want to create an object of the same assembly which is already loaded. But, in a separate AppDomain. But when doing this, I am unable to load the assembly to create the object using CreateInstanceAndUnwrap method. When trying to do the same using a standalone C# code, it works fine.

C++:

TestClass __gc *testObj;
testObj = AppDomainProcessor::getInstance()->newTestObj((LPWSTR)domName);
//testObj->otherOperation...

C#

namespace TestNS {
public class AppDomainProcessor {
...
public TestClass newTestObj(String domName) {
AppDomain appDomain = AppDomain.CreateDommain(domName);


  TestClass testObj = (TestClass)appDomain.CreateInstanceAndUnwrap(typeof(TestClass).Assembly.FullName,typeof(TestClass).FullName);
//Could not load file or assembly 'MyManaged, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified
    return testObj;
    }
...
}

public class TestClass : MarshalByRefObject {
...
}
}

I printed the AssemblyName and found it as the name of the dll which is compiled from the C++ code. When trying from standalone C#, it was the name of the exe.

Is this a proper way of creating AppDomain when using C++ and C# together? Or I have made any mistake in creating the AppDomain. Please assist.

Santron Manibharathi
  • 628
  • 5
  • 12
  • 26
  • Are you getting an error message? If you are, copy/paste it into your question. – Robert Harvey Feb 07 '15 at 19:16
  • @RobertHarvey I have added the error in the comment near the code. – Santron Manibharathi Feb 07 '15 at 19:22
  • That error message appears to be a simple file-finding problem. – Robert Harvey Feb 07 '15 at 19:30
  • Well, if it was built correctly (big if) then the error message says that it can't find the assembly that also contains the code that threw the exception. That's ... special. Start gathering facts with Fuslogvw.exe first. – Hans Passant Feb 07 '15 at 19:33
  • *** Assembly Binder Log Entry (8/2/2015 @ 1:46:39 AM) *** The operation was successful. Bind result: hr = 0x0. The operation completed successfully. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under executable D:\TEST\jre\bin\java.exe --- A detailed error log follows. LOG: IJW explicit bind. File path:D:\TEST\lib\native\MyManaged.dll. LOG: IJW assembly bind returned file not found. – Santron Manibharathi Feb 07 '15 at 20:19
  • Got the above error print in fuslogvw.exe log. But the file MyManaged.dll is located in the same path as provided in error. Still I get file not found error. – Santron Manibharathi Feb 07 '15 at 20:20

1 Answers1

0

Finally got it working. May be it didn't work as I am using a C++ Managed DLL.

But even when I manually entered the DLL location and loaded using CreateInstanceFromAndUnwrap, It didn't work either. But when a Assembly resolving fails, it triggers the AssemblyResolve event. I am using the same Assembly which is executing to create the new AppDomain. Here is the code.

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
            return Assembly.GetExecutingAssembly();
        }

When unable to load, it returns the current Assembly and works fine.

Santron Manibharathi
  • 628
  • 5
  • 12
  • 26