Required permissions cannot be acquired error using Assembly.LoadFrom(String) in winforms

0

I have a winforms application that loads some dll (that I have also written and installed together with the app) at runtime with Assembly.LoadFrom(String). The dll is inside a sub directory of the path where the exe is placed.

Occasionally, the app cannot start in a particular computer and comes out with a exception "Could not load file or assembly 'mydll, Version=1.2.4.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Failed to grant minimum permission requests. (Exception from HRESULT: 0x80131417)" and an inner exception "Required permissions cannot be acquired" at "System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission)".

The code is straight forward: Assembly^ myAssembly = Assembly::LoadFrom(path);

The dlls need to be loaded at runtime as they are optional.

I have been researching this long but all I find is related to asp.net which does not help me much. I am not using any asp.net. It only has happened in very, very, few machines to which I have not access neither I do really know the user privileges but the app does not need Admin privileges in any operation and runs with limited user privileges.

Is there a way I can protect the app from this and load the DLL? Obviously, I can handle the exception but, ultimately, I need the dll to be loaded.

winforms
c++-cli
asked on Stack Overflow Oct 9, 2014 by Aznarepse

2 Answers

0

Can't say if this answer will do you any good or not, but I use an alternate approach to the loading mechanism:

String dllFolder = "folder of DLLs";
DirectoryInfo di = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dllFolder));
FileInfo[] fia = di.GetFilesByExactExtension("dll", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in fia)
{
   Byte[] bytes = File.ReadAllBytes(fi.FullName);
   if (bytes.Length > 0)
   {
       Assembly theDLL = Assembly.Load(bytes);
       if (theDLL != null)
       {
           //...optionally do something more with the instance
       }
   }
}

I've had mixed results with the "load it straight from a file via Load()" method.

GetFilesByExactExtension() is an extension method, btw.

answered on Stack Overflow Oct 9, 2014 by DonBoitnott
0

You could try :

Assembly^ myAssembly = Assembly::LoadFrom(Path::Combine(AppDomain::CurrentDomain->BaseDirectory, path))
answered on Stack Overflow Oct 9, 2014 by ArthurCPPCLI

User contributions licensed under CC BY-SA 3.0