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"; }
|