Jump to content
  • How to call a 32-bit DLL from an extension that runs on a 64-bit Spotfire@ platform


    32-bit and 64-bit platforms

    64-bit (x64) platform, as opposed to 32-bit (x86), is considered the default platform for Spotfire, mainly because of the ability to address up to 8TB of memory, considerably higher than the maximum of 2 GB for 32-bit processes.

    On a 64-bit platform, Spotfire is always launched in a 64-bit process. This is usually not a problem for a custom extension as long as the extension is compiled for a neutral platform target (Any CPU). If the custom extension uses a third party native 32-bit DLL to run on a 64-bit platform, the 32-bit DLL must be executed in a separate 32-bit process. In this case, the custom extension must communicate with the third party DLL through inter-process communication (IPC). In other words, 64-bit and 32-bit processes can exchange data using IPC techniques such as out-of-process COM, sockets, message services or memory mapped files. For more information on IPC, refer to MSDN on Interprocess Communications.

    Implementing the IPC-based communication between your custom extension and the third party component may require considerable work. Check if a 64-bit version of the third party component is available. If it is, the recommended solution is to include both the 32-bit and the 64-bit version in the distribution, using different names for the DLLs, and perform a platform invocation call to the correct DLL based on the current architecture. The following test determines if the current architecture is 32-bit:

     Marshal.SizeOf(typeof(IntPtr)) == 4;
     

    For more information, refer to MSDN on Platform Invoke Services (PInvoke), especially the Platform Invoke Tutorial.

    Note: When running a third party component in a separate process it is not possible, for technical reasons, to have any UI interaction (such as an ActiveX control) in that component. If such interaction is required, make sure to do it in the custom extension executed in the main process.

    Writing 32 and 64 Bit Capable Extensions on 32 and 64 bit specific dll:s in multi-bitness environments

    Some third party components specifically supply 32 and 64 bit specific dll:s and require to be loaded in the correct environment. It is possible to create and extension in spotfire and have it load either 32 or 64 bit third party components at runtime. The recipe for doing this is as follows.

    1. Include both 32 and 64 bit dll:s into your spotfire project (e.g. spk-file) in the package builder as resources.
    2. In your project code, create and register using the global services registry a service that
      • Contains a method that will load either the 32 or the 64 bit assembly into memory depending on runtime bitness.

        In the AddIn class:

        protected override void RegisterGlobalServices(ServiceRegistrar registrar)
        {
            base.RegisterGlobalServices(registrar);            
            registrar.Register(new MyAssemblyService());
        }
         protected override void OnGlobalServicesRegistered(ServiceProvider serviceProvider)
        {
        	base.OnGlobalServicesRegistered(serviceProvider);
        	ModulesService modulesService = serviceProvider.GetService<ModulesService>();
        	MyAssemblyService myAssemblyService = serviceProvider.GetService<MyAssemblyService>();
        	myAssemblyService.InitialiseModulesService(modulesService);            
        }
         

        Typically the actuall assembly loading method on the MyAssemblyService will be run on the  OnGlobalServices event 

      • Initialization code in the MyAssemblyService class:
        internal void InitialiseModulesService(ModulesService modulesService)
        {
        	if (modulesService != null)
        	{
        		if (Environment.Is64BitProcess)
        		{
        			myDllResourcePath = modulesService.GetResourcePath("MyResourceName.x64");
        		}
        		else
        		{
        			myDllResourcePath = modulesService.GetResourcePath("MyResourceName.x86");
        		}
        	}
        
        	var dllResolver = new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        	AppDomain.CurrentDomain.AssemblyResolve += dllResolver;
        }
         

    Once the service has been registered by the Spotfire add-in system, and the initialization method executed, the dll will have been loaded with the correct bitness for your executing environment.


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...