Using CoCreateObjectDotNet to Access and Use .NET Assemblies in InstallScript

http://kb.flexerasoftware.com/selfservice/viewContent.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-2009 ID: Q112220

 

STEP 1. Create the DLL


using System;
using System.Runtime.InteropServices;

namespace ViewerCentral
{
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[Guid("4E0697CB-209A-40c5-939D-709924CC9AFB")]
public class IISUtils: I_IISUtils
{
public IISUtils() {}

public string GetStringSites()
{
return "Test";
}
}

[Guid("B464E841-D49A-4f5b-8701-0C40544CF110")]
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!

 

發佈了29 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章