Click to See Complete Forum and Search --> : Assembly.Load and /clr


martho
January 10th, 2007, 09:21 AM
Hello! I want to load an Assembly written in managed C++ with Assembly.Load and run it.

C#-Code:

Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
object o = a.CreateInstance(method.Name);
String[] aStr = { };
Object[] aParam = { aStr };
method.Invoke(o, aParam);


I did a small example-exe in managed C++ to get loaded from this code. It works fine when the loaded .exe has been compiled with /clr:pure or /clr:save. When using an .exe compiled with /clr only, I got an exception in Assembly.Load: Unverifiable code failed policy check (exception from HRESULT: 0x80131402).

Has anyone a solution for this or is it just not possible to load an assembly compiled with /clr?

Zaccheus
January 10th, 2007, 02:48 PM
Compiling with /clr introduces native code into the assembly. Like the error says, the process (or code) which is loading your assembly has a policy which states that non-verifyable code (that includes all native code) should not be loaded - so you should check if you can change that policy.

Do you need to use /clr (i.e. not 'safe') ? Code compiled with /clr:safe is always verifyable.

martho
January 11th, 2007, 02:37 AM
Hi and thanks for your reply. In the assembly I try to load, I connect to a native 3rd-party dll, which I got the header-files for. The headers use a lot of __cdecl-calls to access the dll, so it can only be compiled with /clr.

Anybody got an idea where to change this policy? I tried:
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
but it didn't make any difference.

Zaccheus
January 11th, 2007, 09:14 AM
Hi and thanks for your reply. In the assembly I try to load, I connect to a native 3rd-party dll, which I got the header-files for. The headers use a lot of __cdecl-calls to access the dll, so it can only be compiled with /clr.
Can you use P/INVOKE instead?

Anybody got an idea where to change this policy? I tried:
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
but it didn't make any difference.
That's what I would have tried as well.