Print Page | Close Window

Package the ocx into the .Net executable

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Command Bars
Forum Description: Topics Related to Codejock Command Bars
URL: http://forum.codejock.com/forum_posts.asp?TID=2435
Printed Date: 07 July 2024 at 7:00pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Package the ocx into the .Net executable
Posted By: Wael
Subject: Package the ocx into the .Net executable
Date Posted: 22 June 2005 at 10:52pm

Hi I am new to using Activex with .Net and you probablly can tell from asking the following question:-

Is it possible to package [i.e embedd] the CommandBars.ocx into my .net application exe file?

if yes and I hope its a yes:-

Is it possible to register the ocx once the application is started and unregisters once the application has been terminated.

I am aware microsoft has a new tool called ILMerge that allows the merging of .net dll assembilies with exe's but it does not support ocx.

I have found one other tool but have not tested it because my project is in csharp and it seems to have been developed for VB.NET.

VB-PowerWrap 4.0  http://www.moonlight-software.com/ - http://www.moonlight-software.com/

I hope there is a way to do this, so please let me know if you have any ideas.

Thanks in advance.




Replies:
Posted By: Wael
Date Posted: 25 June 2005 at 1:19am

well in case anyone is interested in the progress of this issue, Here is something maybe someone can work with, the following allows you to register/unregister the ocx dynamically. But the catch is that the ocx file will be treated as an external file and not embedded. hopfully there is still a way to do this while the file is being embededd.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;
using System.Runtime.InteropServices;
using System.Threading;
public class ocxRegServer
{
 private byte[] m_sDllFile;
 private static ModuleBuilder myModuleBuilder;
 private Type m_tDllReg;

 public ocxRegServer(byte[] dllFile)
 {
  m_sDllFile = dllFile;
  CreateDllRegType();
 }

 public void Register()
 {
  InternalRegServer(false);
 }

 public void UnRegister()
 {
  InternalRegServer(true);
 }

 private void InternalRegServer(bool fUnreg)
 {
  string sMemberName = fUnreg ? "DllUnregisterServer" : "DllRegisterServer";

  int hr = (int) m_tDllReg.InvokeMember (  sMemberName,
       BindingFlags.Invok eMethod,
       null,
       Activator.CreateIn stance(m_tDllReg),
       null );
  if (hr != 0)
  {
   Marshal.ThrowExceptionForHR(hr);
  }
 }

 private void CreateDllRegType()
 {
  string path;
  string myAsmName = "DllUnregisterServer";
  string myAsmFileName = myAsmName + ".dll";

  if (myModuleBuilder == null)
  {
   AppDomain appDomain = Thread.GetDomain();

   AssemblyName myAssemblyName = new AssemblyName();
   myAssemblyName.Name = myAsmName;

   AssemblyBuilder myAssemblyBuilder = appDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuild erAccess.RunAndSave);

   myModuleBuilder =  myAssemblyBuilder.DefineDynamicModule(myAsmFileName, &n bsp; myAsmFileName);

   // this line will embed the embedded CommandBars.ocx into the dynamically created assembly file
   // I am not really sure if this can be useful!.
   myModuleBuilder.DefineUnmanagedResource(m_ sDllFile);

   myAssemblyBuilder.Save(myAsmFileName);
  }

  // Add class to module
  TypeBuilder tb = myModuleBuilder.DefineType("DllRegServerClass" + Guid.NewGuid().ToString("N"));

  MethodBuilder meb;

  // Add PInvoke methods to class
  meb = tb.DefinePInvokeMethod(   "DllRegisterServer",
      "CommandBars.ocx",
      MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
      CallingConventions.Stand ard,
      typeof (int),
      null,
      CallingConvention.StdCal l,
      CharSet.Auto);

  // Apply preservesig metadata attribute so we can handle return HRESULT ourselves
  meb.SetImplementationFlags(MethodImplAttributes. PreserveSig | meb.GetMethodImplementationFlags());

  meb = tb.DefinePInvokeMethod(   "DllUnregisterServer",
      "CommandBars.ocx",
      MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
      CallingConventions.Stand ard,
      typeof (int),
      null,
      CallingConvention.StdCal l,
      CharSet.Auto);

  // Apply preservesig metadata attribute so we can handle return HRESULT ourselves
  meb.SetImplementationFlags(MethodImplAttributes. PreserveSig | meb.GetMethodImplementationFlags());

  // Create the type
  m_tDllReg = tb.CreateType();
 }
}

// here is the code that goes into Form1 for usage:-

private void InitializeComponent()
{
 string strNameSpace= System.Reflection.Assembly.GetExecutingAssembly().GetName(). Name.ToString();

 // get the resource into a stream
 Stream str = System.Reflection.Assembly.GetExecutingAssembly().GetManifes tResourceStream( strNameSpace + "."+ "CommandBars.ocx" );
 if ( str == null )
 {
  return;
 }

 byte[] bStr = new Byte[str.Length];

 ocxRegServer reg = new ocxRegServer(bStr);
 
 if (fUnreg)
 {
  reg.UnRegister();
 }
 else
 {
  reg.Register();
 }
 
 this.components = new System.ComponentModel.Container();
 this.Size = new System.Drawing.Size(300, 300);
 this.Text = "Form1"; 
}




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net