Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > ActiveX COM > Command Bars
  New Posts New Posts RSS Feed - Package the ocx into the .Net executable
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Package the ocx into the .Net executable

 Post Reply Post Reply
Author
Message
Wael View Drop Down
Newbie
Newbie


Joined: 22 June 2005
Location: United Kingdom
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Wael Quote  Post ReplyReply Direct Link To This Post Topic: Package the ocx into the .Net executable
    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/

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

Thanks in advance.



Edited by Wael
Back to Top
Wael View Drop Down
Newbie
Newbie


Joined: 22 June 2005
Location: United Kingdom
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Wael Quote  Post ReplyReply Direct Link To This Post 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"; 
}

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.125 seconds.