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!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值