Михаил Власов
 
  
  Зарегистрирован: 14.02.2005 Сообщения: 580 Откуда: ИНТЕГРО
  | 
		
			
				 Добавлено: Вт 08 Окт 2013 11:17    Заголовок сообщения: Использование MapX в программах на c# (Call to OS function failed) | 
				     | 
			 
			
				
  | 
			 
			
				Иногда при использовании MapX в программах на C# возникает ошибка "Call to OS function failed".
 
 
Для исправления этой ситуации рекомендуем внести следующие изменения в проект на C#:
 
 
1. Включить в проект файл MapXHostScope.cs следующего содержания:
 
 	  | Код: | 	 		  using System;
 
using System.IO;
 
using System.Runtime.InteropServices;
 
using System.Security;
 
using System.Security.Permissions;
 
using System.Windows.Forms;
 
 
namespace MapXHosting
 
{
 
      [SuppressUnmanagedCodeSecurity]
 
      internal class MapXHostingScope : IDisposable
 
      {
 
         private static ACTCTX FActCtx;
 
         private static IntPtr FActCtxHandle;
 
         private static bool FActCtxCreationSucceeded = false;
 
         
 
         private uint FActCtxActivationCookie;
 
 
         public MapXHostingScope()
 
         {
 
            FActCtxActivationCookie = 0;
 
            if (!OSFeature.Feature.IsPresent(OSFeature.Themes)) {
 
               return;
 
            }
 
            if (!EnsureActivateContextCreated()) {
 
               return;
 
            }
 
            if (!ActivateActCtx(FActCtxHandle, out FActCtxActivationCookie)) {
 
               FActCtxActivationCookie = 0;
 
            }
 
         }
 
 
         ~MapXHostingScope()
 
         {
 
            Dispose(false);
 
         }
 
 
         void IDisposable.Dispose()
 
         {
 
            Dispose(true);
 
         }
 
 
         private void Dispose(bool disposing)
 
         {
 
            if (FActCtxActivationCookie == 0) {
 
               return;
 
            }
 
            if (DeactivateActCtx(0, FActCtxActivationCookie)) {
 
               FActCtxActivationCookie = 0;
 
            }
 
         }
 
 
 
 
 
         private static bool EnsureActivateContextCreated()
 
         {
 
            lock (typeof(MapXHostingScope)) {
 
               if (!FActCtxCreationSucceeded) {
 
                  string net_installation_location = null;
 
 
                  var fiop = new FileIOPermission(PermissionState.None) {AllFiles = FileIOPermissionAccess.PathDiscovery};
 
                  fiop.Assert();
 
                  try {
 
                     net_installation_location = typeof(Object).Assembly.Location;
 
                  }
 
                  finally {
 
                     CodeAccessPermission.RevertAssert();
 
                  }
 
 
                  var install_dir = Path.GetDirectoryName(net_installation_location);
 
                  if(install_dir != null) {
 
                     const string manifest_name = "XPThemes.manifest";
 
                     var mscorlib_manifest_location = Path.Combine(install_dir, manifest_name);
 
 
                     FActCtx = new ACTCTX {
 
                        cbSize = Marshal.SizeOf(typeof (ACTCTX)),
 
                        lpSource = mscorlib_manifest_location,
 
                        lpAssemblyDirectory = install_dir,
 
                        dwFlags = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
 
                     };
 
 
                     FActCtxHandle = CreateActCtx(ref FActCtx);
 
                     FActCtxCreationSucceeded = (FActCtxHandle != new IntPtr(-1));
 
                  }
 
               }
 
 
               return FActCtxCreationSucceeded;
 
            }
 
         }
 
 
         [DllImport("Kernel32.dll")]
 
         private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
 
         [DllImport("Kernel32.dll")]
 
         private extern static bool ActivateActCtx(IntPtr hActCtx, out uint lpCookie);
 
         [DllImport("Kernel32.dll")]
 
         private extern static bool DeactivateActCtx(uint dwFlags, uint lpCookie);
 
 
         private const int ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x004;
 
 
         private struct ACTCTX
 
         {
 
            public int cbSize;
 
            public uint dwFlags;
 
            public string lpSource;
 
            public ushort wProcessorArchitecture;
 
            public ushort wLangId;
 
            public string lpAssemblyDirectory;
 
            public string lpResourceName;
 
            public string lpApplicationName;
 
         }
 
      }
 
} | 	  
 
2. В главной процедуре main обрамить вызов Application.Run следующим кодом:
 
 	  | Код: | 	 		  using(new MapXHostingScope()) {
 
   Application.Run(new Form1());
 
} | 	  
 
 
После этого MapX должен работать под темами без ошибок. _________________ С уважением, Михаил Власов. | 
			 
		  |