ITeye博客

Using CoCreateObjectDotNet to Access and Use .net Assemblies in InstallScript

igf80igf · 2012-01-20 02:48 · 技术杂绘 · 非技术 · Markdown

Using CoCreateObjectDotNet to Access and Use .net Assemblies in InstallScript 2011年04月18日   http://kb.flexerasoftware.com/selfservice/viewCont ent.do?externalID=Q112220 Q112220: CODE: Using CoCreateObjectDotNet to Access and Use .NET Assemblies in InstallScript InstallShield 11.5 Premier, InstallShield 11.5 Professional, InstallShield 12 Premier, InstallShield 12 Professional, InstallShield 2008 Premier, InstallShield 2008 Professional Basic MSI, InstallScript MSI CoCreateObjectDotNet calls functions within assemblies that are creating using .NET. CoCreateObjectDotNet creates an object that references a method of a .NET assembly. Each method you wish to use will require a separate call into CoCreateObjectDotNet. Note: Assemblies built with Visual Studio 2005 require that the attribute ComVisible be set to true for each class that will be called from CoCreateObjectDotNet. The code below demonstrates a sample implementation of CoCreateObjectDotNet assuming you've created a DLL using .NET 2.0 that has a class named OutputInfo with the method DisplaySystemInfo(). try   set dotNet = CoCreateObjectDotNet(SUPPORTDIR ^ "SampleClass.dll","SampleClass.OutputInfo");   dotNet.DisplaySystemInfo();   catch   SprintfBox(INFORMATION,"CoCreateObjectDotNet Error","ERROR: %s NUMBER: %d",Err.Description,Err.Number);   endcatch;
  For additional help with the ComVisible attribute, please reference MSDN article ComVisibleAttribute Class. For additional information on InstallScript, see the InstallScript Language Reference in the InstallShield Help Library. Last Modified Date: 11-30-2009ID: Q112220 STEP 1. Create the DLL   using System;   using System.Runtime.InteropServices;   namespace ViewerCentral   {         public class IISUtils: I_IISUtils
  {   public IISUtils() {}   public string GetStringSites()   {   return "Test";   }   }      public interface I_IISUtils   {   string GetStringSites();   }   }   Several notes here:   - Don't return to InstallScript complex classes or objects, but only simple types that can be recognized, like ( int, string, etc.).   - NO registration for COM Interop is necessary, however, as you see the DLL must be in the COM Interrop format.   STEP 2. Use DLL in InstallScript   1. Add your DLL to Support Files (I have added it to Language Independent).   2. Define a path to the DLL:   #define DLL_FILE SUPPORTDIR ^ "IISUtils.dll"   3. Use the DLL by calling one of its methods.   object oIISUtils;   string szSites;
  set oIISUtils = CoCreateObjectDotNet( DLL_FILE, "ViewerCentral.IISUtils" );
  szSites = oIISUtils.GetStringSites();
  Hope this helps pull this out faster. This certainly worked for me. Good Luck!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。