2010-11-11 112 views
0

所以我有以下代码:,一个安全异常

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetDllDirectory(string dllPath); 

在功能:

  SetDllDirectory(@"G:\Sean\Debug\"); 

      Assembly loadedDLL = Assembly.LoadFrom(@"G:\Sean\Debug\BonderControlPanelSim.dll", AppDomain.CurrentDomain.Evidence); 
      Type rtsObj = loadedDLL.GetType("Oe.Te.Ranorex.Instrument.BonderControlPanelSim"); 
      Object obj = Activator.CreateInstance(rtsObj); 

      rtsObj.InvokeMember("Initialize", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[] { "COM3", 1, 2 }); 
      rtsObj.InvokeMember("PushStart", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[] { 3 }); 
      rtsObj.InvokeMember("Shutdown", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, null); 

然而,当我运行一个小的应用程序从眼前这个功能与DLL相同的文件夹,没有问题。当我将可执行文件移动到另一台具有G驱动器映射的计算机时,出现安全异常。

<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
version="1" 
Flags="UnmanagedCode"/> 

不知道该如何处理。在Assembly.LoadFrom我通过证据。

在第一个rtsObj.InvokeMember引发异常。

感谢您的帮助!

编辑:应用程序实际上无法在其他机器上启动。如果我的机器上只有它自己的可执行文件,它将启动。

回答

1

驻留在远程网络共享上的.NET程序集被默认的.NET安全策略认为不足以让它们执行P/Invokes。您需要更改安全策略,将程序集复制到本地文件夹(不是映射网络驱动器),或者完全删除SetDllDirectory P/Invoke调用。

+0

的PInvoke的是另一个DLL需要在此应用 – 2010-11-11 18:31:44

+1

很好用的多数民众赞成在一个非托管的组件,如果组件从网络共享调用,那么它也将草草收场时,它试图为P失败/ Invoke的。您将**拥有**来更改.NET安全策略,或将程序集复制到机器本地卷以供运行时允许任何* P /调用。 – cdhowie 2010-11-11 18:35:27

1

您可以更改安全策略以允许CLR从远程源打开。

http://blogs.msdn.com/b/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx

// From the Article 
// Since this application only trusts a handful of LoadFrom operations, 
// we'll put them all into the same AppDomain which is a simple sandbox 
// with a full trust grant set. The application itself will not enable 
// loadFromRemoteSources, but instead channel all of the trusted loads 
// into this domain. 
PermissionSet trustedLoadFromRemoteSourceGrantSet 
    = new PermissionSet(PermissionState.Unrestricted); 

AppDomainSetup trustedLoadFromRemoteSourcesSetup = new AppDomainSetup(); 
trustedLoadFromRemoteSourcesSetup.ApplicationBase = 
    AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 

AppDomain trustedRemoteLoadDomain = 
    AppDomain.CreateDomain("Trusted LoadFromRemoteSources Domain", 
          null, 
          trustedLoadFromRemoteSourcesSetup, 
          trustedLoadFromRemoteSourcesGrantSet); 

// Now all trusted remote LoadFroms can be done in the trustedRemoteLoadDomain, 
// and communicated with via a MarshalByRefObject. 
+0

我试过这个......它没有工作。该应用程序甚至不会在其他机器上启动 – 2010-11-11 18:27:07