Not able to use the COM control in MS Excel user form after changing the GUID of the COM control and

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have created a COM control in C# and I registered it by using RegAsm. So I am able to use the COM control in the MS Excel UserForms.
I have used the below code.

<div style="color:black; background-color:white
<pre><span style="color:blue namespace ActiveXDotNet
{
[ComVisible(<span style="color:blue true)]
[Guid(<span style="color:#a31515 "0F3A2E9D-C79E-4b1b-9AF7-2D1487F29941")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
<span style="color:blue public <span style="color:blue interface AxMyControl
{
[ComVisible(<span style="color:blue true)]
String UserText { <span style="color:blue set; <span style="color:blue get; }
}

<span style="color:gray /// <span style="color:gray <summary>
<span style="color:gray ///<span style="color:green Summary description for myControl.
<span style="color:gray /// <span style="color:gray </summary>
[ComVisible(<span style="color:blue true)]
[ProgId(<span style="color:#a31515 "ActiveXDotNet.myControl")]
[Guid(<span style="color:#a31515 "0F2A2E9D-C79E-4b1b-9AF7-2D1487F29041")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(<span style="color:blue typeof(AxMyControl))]
<span style="color:blue public <span style="color:blue class myControl : System.Windows.Forms.UserControl, AxMyControl, IObjectSafety
{
<span style="color:blue #region Form Components
<span style="color:blue private System.Windows.Forms.GroupBox groupBox1;
<span style="color:blue private System.Windows.Forms.Label label1;
<span style="color:blue private System.Windows.Forms.TextBox txtUserText;
<span style="color:gray /// <span style="color:gray <summary><span style="color:green
<span style="color:gray ///<span style="color:green Required designer variable.
<span style="color:gray /// <span style="color:gray </summary>
<span style="color:blue private System.ComponentModel.Container components = <span style="color:blue null;
<span style="color:blue #endregion

<span style="color:blue private String mStr_UserText;

<span style="color:blue public String UserText
{
<span style="color:blue get { <span style="color:blue return mStr_UserText; }
<span style="color:blue set
{
mStr_UserText = value;
<span style="color:green //Update the text box control value also.
txtUserText.Text = value;
}
}

<span style="color:blue public <span style="color:blue enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
};

<span style="color:blue public <span style="color:blue int GetInterfaceSafetyOptions(<span style="color:blue ref Guid riid, <span style="color:blue out <span style="color:blue int pdwSupportedOptions, <span style="color:blue out <span style="color:blue int pdwEnabledOptions)
{
ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
pdwSupportedOptions = (<span style="color:blue int)m_options;
pdwEnabledOptions = (<span style="color:blue int)m_options;
<span style="color:blue return 0;
}

<span style="color:blue public <span style="color:blue int SetInterfaceSafetyOptions(<span style="color:blue ref Guid riid, <span style="color:blue int dwOptionSetMask, <span style="color:blue int dwEnabledOptions)
{
<span style="color:blue return 0;
}

[ComRegisterFunction]
<span style="color:blue static <span style="color:blue void ComRegister(Type t)
{
<span style="color:blue string keyName = <span style="color:#a31515 @"CLSID" + t.GUID.ToString(<span style="color:#a31515 "B");
<span style="color:blue using (RegistryKey key =
Registry.ClassesRoot.OpenSubKey(keyName, <span style="color:blue true))
{
key.CreateSubKey(<span style="color:#a31515 "Control").Close();
<span style="color:blue using (RegistryKey subkey = key.CreateSubKey(<span style="color:#a31515 "MiscStatus"))
{
<span style="color:green // 131456 decimal == 0x20180.
<span style="color:blue long val = (<span style="color:blue long)
(OLEMISC.OLEMISC_INSIDEOUT
| OLEMISC.OLEMISC_ACTIVATEWHENVISIBLE
| OLEMISC.OLEMISC_SETCLIENTSITEFIRST);
subkey.SetValue(<span style="color:#a31515 "", val);
}
<span style="color:blue using (RegistryKey subkey = key.CreateSubKey(<span style="color:#a31515 "TypeLib"))
{
Guid libid =
Marshal.GetTypeLibGuidForAssembly(t.Assembly);
subkey.SetValue(<span style="color:#a31515 "", libid.ToString(<span style="color:#a31515 "B"));
}
<span style="color:blue using (RegistryKey subkey = key.CreateSubKey(<span style="color:#a31515 "Version"))
{
Version ver = t.Assembly.GetName().Version;
<span style="color:blue string version =
<span style="color:blue string.Format(<span style="color:#a31515 "{0}.{1}", ver.Major, ver.Minor);
subkey.SetValue(<span style="color:#a31515 "", version);
}
<span style="color:green // Next create the CodeBase entry - needed if not string named and GACced.
<span style="color:blue using (RegistryKey inprocServer32 = key.OpenSubKey(<span style="color:#a31515 "InprocServer32", <span style="color:blue true))
{
inprocServer32.SetValue(<span style="color:#a31515 "CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
}
<span style="color:green // Finally close the main key
key.Close();
}
}

[ComUnregisterFunction]
<span style="color:blue static <span style="color:blue void ComUnregister(Type t)
{
<span style="color:green // Delete the entire CLSID{clsid} subtree for this component.
<span style="color:blue string keyName = <span style="color:#a31515 @"CLSID" + t.GUID.ToString(<span style="color:#a31515 "B");
Registry.ClassesRoot.DeleteSubKeyTree(keyName);
}

<span style="color:blue #region Default autogenerated code... none modified.
<span style="color:blue public myControl()
{
<span style="color:green // This call is required by the Windows.Forms Form Designer.
InitializeComponent();

<span style="color:green // TODO: Add any initialization after the InitForm call

}

<span style="color:gray /// <span style="color:gray <summary><span style="color:green
<span style="color:gray ///<span style="color:green Clean up any resources being used.
<span style="color:gray /// <span style="color:gray </summary>
<span style="color:blue protected <span style="color:blue override <span style="color:blue void Dispose(<span style="color:blue bool disposing)
{
<span style="color:blue if (disposing)
{
<span style="color:blue if (components != <span style="color:blue null)
{
components.Dispose();
}
}
<span style="color:blue base.Dispose(disposing);
}

<span style="color:blue #region Component Designer generated code
<span style="color:gray /// <span style="color:gray <summary><span style="color:green
<span style="color:gray ///<span style="color:green Required method for Designer support - do not modify
<span style="color:gray ///<span style="color:green the contents of this method with the code editor.
<span style="color:gray /// <span style="color:gray </summary>
<span style="color:blue private <span style="color:blue void InitializeComponent()
{
<span style="color:blue this.groupBox1 = <span style="color:blue new System.Windows.Forms.GroupBox();
<span style="color:blue this.label1 = <span style="color:blue new System.Windows.Forms.Label();
<span style="color:blue this.txtUserText = <span style="color:blue new System.Windows.Forms.TextBox();
<span style="color:blue this.groupBox1.SuspendLayout();
<span style="color:blue this.SuspendLayout();
<span style="color:green //
<span style="color:green // groupBox1
<span style="color:green //
<span style="color:blue this.groupBox1.Controls.AddRange(<span style="color:blue new System.Windows.Forms.Control[] {
<span style="color:blue this.txtUserText,
<span style="color:blue this.label1});
<span style="color:blue this.groupBox1.Location = <span style="color:blue new System.Drawing.Point(8, 8);
<span style="color:blue this.groupBox1.Name = <span style="color:#a31515 "groupBox1";
<span style="color:blue this.groupBox1.Size = <span style="color:blue new System.Drawing.Size(272, 56);
<span style="color:blue this.groupBox1.TabIndex = 0;
<span style="color:blue this.groupBox1.TabStop = <span style="color:blue false;
<span style="color:blue this.groupBox1.Text = <span style="color:#a31515 "my ActiveX Control Simulation";
<span style="color:green //
<span style="color:green // label1
<span style="color:green //
<span style="color:blue this.label1.Location = <span style="color:blue new System.Drawing.Point(8, 24);
<span style="color:blue this.label1.Name = <span style="color:#a31515 "label1";
<span style="color:blue this.label1.Size = <span style="color:blue new System.Drawing.Size(56, 16);
<span style="color:blue this.label1.TabIndex = 0;
<span style="color:blue this.label1.Text = <span style="color:#a31515 "User Text:";
<span style="color:green //
<span style="color:green // txtUserText
<span style="color:green //
<span style="color:blue this.txtUserText.Enabled = <span style="color:blue false;
<span style="color:blue this.txtUserText.Location = <span style="color:blue new System.Drawing.Point(64, 20);
<span style="color:blue this.txtUserText.Name = <span style="color:#a31515 "txtUserText";
<span style="color:blue this.txtUserText.Size = <span style="color:blue new System.Drawing.Size(200, 20);
<span style="color:blue this.txtUserText.TabIndex = 1;
<span style="color:blue this.txtUserText.Text = <span style="color:#a31515 "";
<span style="color:green //
<span style="color:green // myControl
<span style="color:green //
<span style="color:blue this.Controls.AddRange(<span style="color:blue new System.Windows.Forms.Control[] {
<span style="color:blue this.groupBox1});
<span style="color:blue this.Name = <span style="color:#a31515 "myControl";
<span style="color:blue this.Size = <span style="color:blue new System.Drawing.Size(288, 72);
<span style="color:blue this.groupBox1.ResumeLayout(<span style="color:blue false);
<span style="color:blue this.ResumeLayout(<span style="color:blue false);

}
<span style="color:blue #endregion
<span style="color:blue #endregion

}
[/code]



The above code worked for me, then i unregistered the COM control and replaced the GUIDs of COM Interface and COM Class as below. I have created the GUIDs online at http://www.guidgenerator.com/online-guid-generator.aspx http://www.guidgenerator.com/online-guid-generator.aspx
Again registered the modified COM control again
<br/>


[ComVisible(true)]
[Guid("38dd15dc-569f-461e-a0fb-8055300ab3bc")] // [Guid("0F3A2E9D-C79E-4b1b-9AF7-2D1487F29941")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface AxMyControl
{
[ComVisible(true)]
String UserText { set; get; }
}

/// <summary>
/// Summary description for myControl.
/// </summary>
[ComVisible(true)]
[ProgId("ActiveXDotNet.myControl")]
[Guid("4f43168c-7beb-4d4b-801a-4196f47cab09")] // [Guid("0F2A2E9D-C79E-4b1b-9AF7-2D1487F29041")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(AxMyControl))]


After replacing the GUIDs, I am unable to use the COM control in the MS Excel UserForms, when i try to place the COM control i am getting the below error
"Element not found."
Even i tried to create a new control with new GUIDs, still i am getting the same error.
Whats wrong with the new GUID? Can anyone help me what i am doing wrong?
I have uploaded my test sample for your reference in the below path
https://ksvimalraj.opendrive.com/files/?NF81NDk2OTFfYzlGRGc" target="_blank https://ksvimalraj.opendrive.com/files/?NF81NDk2OTFfYzlGRGc


<
Please remember to click âMark as Answerâ on the post that helps you, and to click âUnmark as Answerâ if a marked post does not actually answer your question. This will help other members to find the solution easily.<br/>

View the full article
 
Back
Top