473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# control causes VB6 application to crash on exit

Hi,
We have a VB6 application that needs to use a new control written in
..NET v1.1 C#, imported as a COM control.
We have tried adding the C# control to the VB6 control both dynamically
(using Controls.Add) and using the VB6 designer. In both approaches
the behavior was identical: the control was functional and accessible,
but the application crashed on exit with the message <app name> "has
encountered a problem and needs to close. We are sorry for the
inconvenience." etc.

I have searched the web, with no luck. Any ideas will be greatly
appreciated. The C# code is embedded below.

Thanks
Asaf.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
[assembly:ClassInterface(ClassInterfaceType.AutoDua l)]
namespace MyCtrlLib
{
///
/// Summary description for UserControl1.
///
[ GuidAttribute("F321BAC9-5019-4c6f-BADF-8D104A499870") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceI sDual)]
public interface ICSharpCOMInterface
{
void MethodA();
void MethodB(int a);
}

[ComVisible(false)]
public delegate void CSharpEventHandlerA ();
[ComVisible(false)]
public delegate void CSharpEventHandlerB (int a);

[GuidAttribute("03018F99-263B-417e-94F3-F367471F6679"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIDispatch)]
public interface ICSharpCOMEvents
{
[DispId(1)]void EventA();
[DispId(2)]void EventB(int a);
}

[GuidAttribute("E022271B-63C8-4878-8C9E-712E776C4785")]
[ProgIdAttribute("MyCtrlLib.CSharpUserControl")]
//[ComSourceInterfacesAttribute(typeof (ICSharpCOMEvents))]
[ComSourceInterfacesAttribute(typeof(MyCtrlLib.ICSh arpCOMEvents))]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class CSharpUserControl : System.Windows.Forms.UserControl,
ICSharpCOMInterface
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

[Category("Action")]
public event CSharpEventHandlerA EventA;
[Category("Action")]
public event CSharpEventHandlerB EventB;

public CSharpUserControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
MessageBox.Show("Dispose");
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

public void MethodA()
{
this.textBox1.Text = "MethodA";
OnMyEventA();
}

public void MethodB(int a)
{
OnMyEventB(a);
}

protected void OnMyEventA()
{
if (EventA != null)
{
this.textBox1.Text = "Event fired!";
EventA();
}
}

protected void OnMyEventB(int a)
{
if (EventB != null)
EventB(a);
}
[ComRegisterFunction()]
public static void RegisterClass ( Type t )
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need
it
string sb = @"CLSID\" + t.GUID.ToString("B");
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;
ctrl.Close ( ) ;

using( RegistryKey subkey = k.CreateSubKey("MiscStatus") )
{
subkey.SetValue("", "131473");
}

using( RegistryKey subkey = k.CreateSubKey("MiscStatus\\1") )
{
subkey.SetValue("", "197009");
}

// Next create the CodeBase entry - needed if not string named and
GACced
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);
inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;

// Now create the Version entry
RegistryKey vers = k.CreateSubKey("Version");
vers.SetValue("", "1.0");
vers.Close();

RegistryKey inst = k.CreateSubKey ( "Insertable" ) ;
inst.Close ( ) ;

// And the Type Lib Entry
using( RegistryKey subkey = k.CreateSubKey("TypeLib") )
{
Guid libid = Marshal.GetTypeLibGuidForAssembly(t.Assembly);

subkey.SetValue("", libid.ToString("B"));
}
// Finally close the main key
k.Close ( ) ;
}
[ComUnregisterFunction()]
public static void UnregisterClass ( string key )
{
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// Delete the 'Control' key, but don't throw an exception if it does
not exist
k.DeleteSubKey ( "Control" , false ) ;

// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);

// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey ( "CodeBase" , false ) ;

k.DeleteSubKey ( "TypeLib" , false ) ;

k.DeleteSubKey ( "Version" , false ) ;

// Finally close the main key
k.Close ( ) ;
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(192, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// CSharpUserControl
//
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "CSharpUserControl";
this.Size = new System.Drawing.Size(208, 64);
this.ResumeLayout(false);
}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
OnMyEventA();
}
}
}

Jan 1 '06 #1
3 2656
Seems to me that you'd be better off on a C# forum, as the problems seems to
be related to your control rather than to VB6.

BTW I'd be suspicious of any attempt to do manual disposing...

"Asaf" <as***@marvell.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,
We have a VB6 application that needs to use a new control written in
.NET v1.1 C#, imported as a COM control.
We have tried adding the C# control to the VB6 control both dynamically
(using Controls.Add) and using the VB6 designer. In both approaches
the behavior was identical: the control was functional and accessible,
but the application crashed on exit with the message <app name> "has
encountered a problem and needs to close. We are sorry for the
inconvenience." etc.

I have searched the web, with no luck. Any ideas will be greatly
appreciated. The C# code is embedded below.

Thanks
Asaf.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
[assembly:ClassInterface(ClassInterfaceType.AutoDua l)]
namespace MyCtrlLib
{
///
/// Summary description for UserControl1.
///
[ GuidAttribute("F321BAC9-5019-4c6f-BADF-8D104A499870") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceI sDual)]
public interface ICSharpCOMInterface
{
void MethodA();
void MethodB(int a);
}

[ComVisible(false)]
public delegate void CSharpEventHandlerA ();
[ComVisible(false)]
public delegate void CSharpEventHandlerB (int a);

[GuidAttribute("03018F99-263B-417e-94F3-F367471F6679"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIDispatch)]
public interface ICSharpCOMEvents
{
[DispId(1)]void EventA();
[DispId(2)]void EventB(int a);
}

[GuidAttribute("E022271B-63C8-4878-8C9E-712E776C4785")]
[ProgIdAttribute("MyCtrlLib.CSharpUserControl")]
//[ComSourceInterfacesAttribute(typeof (ICSharpCOMEvents))]
[ComSourceInterfacesAttribute(typeof(MyCtrlLib.ICSh arpCOMEvents))]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class CSharpUserControl : System.Windows.Forms.UserControl,
ICSharpCOMInterface
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

[Category("Action")]
public event CSharpEventHandlerA EventA;
[Category("Action")]
public event CSharpEventHandlerB EventB;

public CSharpUserControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
MessageBox.Show("Dispose");
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

public void MethodA()
{
this.textBox1.Text = "MethodA";
OnMyEventA();
}

public void MethodB(int a)
{
OnMyEventB(a);
}

protected void OnMyEventA()
{
if (EventA != null)
{
this.textBox1.Text = "Event fired!";
EventA();
}
}

protected void OnMyEventB(int a)
{
if (EventB != null)
EventB(a);
}
[ComRegisterFunction()]
public static void RegisterClass ( Type t )
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need
it
string sb = @"CLSID\" + t.GUID.ToString("B");
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;
ctrl.Close ( ) ;

using( RegistryKey subkey = k.CreateSubKey("MiscStatus") )
{
subkey.SetValue("", "131473");
}

using( RegistryKey subkey = k.CreateSubKey("MiscStatus\\1") )
{
subkey.SetValue("", "197009");
}

// Next create the CodeBase entry - needed if not string named and
GACced
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);
inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;

// Now create the Version entry
RegistryKey vers = k.CreateSubKey("Version");
vers.SetValue("", "1.0");
vers.Close();

RegistryKey inst = k.CreateSubKey ( "Insertable" ) ;
inst.Close ( ) ;

// And the Type Lib Entry
using( RegistryKey subkey = k.CreateSubKey("TypeLib") )
{
Guid libid = Marshal.GetTypeLibGuidForAssembly(t.Assembly);

subkey.SetValue("", libid.ToString("B"));
}
// Finally close the main key
k.Close ( ) ;
}
[ComUnregisterFunction()]
public static void UnregisterClass ( string key )
{
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// Delete the 'Control' key, but don't throw an exception if it does
not exist
k.DeleteSubKey ( "Control" , false ) ;

// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);

// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey ( "CodeBase" , false ) ;

k.DeleteSubKey ( "TypeLib" , false ) ;

k.DeleteSubKey ( "Version" , false ) ;

// Finally close the main key
k.Close ( ) ;
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(192, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// CSharpUserControl
//
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "CSharpUserControl";
this.Size = new System.Drawing.Size(208, 64);
this.ResumeLayout(false);
}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
OnMyEventA();
}
}
}

Jan 1 '06 #2
Asaf,

Unfortunately, ActiveX controls are not able to be exported from .NET to
automation-compatable clients.

There is an unsupported hack to do this, but I wouldn't recommend it,
since it is not supported in the least.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Asaf" <as***@marvell.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,
We have a VB6 application that needs to use a new control written in
.NET v1.1 C#, imported as a COM control.
We have tried adding the C# control to the VB6 control both dynamically
(using Controls.Add) and using the VB6 designer. In both approaches
the behavior was identical: the control was functional and accessible,
but the application crashed on exit with the message <app name> "has
encountered a problem and needs to close. We are sorry for the
inconvenience." etc.

I have searched the web, with no luck. Any ideas will be greatly
appreciated. The C# code is embedded below.

Thanks
Asaf.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
[assembly:ClassInterface(ClassInterfaceType.AutoDua l)]
namespace MyCtrlLib
{
///
/// Summary description for UserControl1.
///
[ GuidAttribute("F321BAC9-5019-4c6f-BADF-8D104A499870") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceI sDual)]
public interface ICSharpCOMInterface
{
void MethodA();
void MethodB(int a);
}

[ComVisible(false)]
public delegate void CSharpEventHandlerA ();
[ComVisible(false)]
public delegate void CSharpEventHandlerB (int a);

[GuidAttribute("03018F99-263B-417e-94F3-F367471F6679"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIDispatch)]
public interface ICSharpCOMEvents
{
[DispId(1)]void EventA();
[DispId(2)]void EventB(int a);
}

[GuidAttribute("E022271B-63C8-4878-8C9E-712E776C4785")]
[ProgIdAttribute("MyCtrlLib.CSharpUserControl")]
//[ComSourceInterfacesAttribute(typeof (ICSharpCOMEvents))]
[ComSourceInterfacesAttribute(typeof(MyCtrlLib.ICSh arpCOMEvents))]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class CSharpUserControl : System.Windows.Forms.UserControl,
ICSharpCOMInterface
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

[Category("Action")]
public event CSharpEventHandlerA EventA;
[Category("Action")]
public event CSharpEventHandlerB EventB;

public CSharpUserControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
MessageBox.Show("Dispose");
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

public void MethodA()
{
this.textBox1.Text = "MethodA";
OnMyEventA();
}

public void MethodB(int a)
{
OnMyEventB(a);
}

protected void OnMyEventA()
{
if (EventA != null)
{
this.textBox1.Text = "Event fired!";
EventA();
}
}

protected void OnMyEventB(int a)
{
if (EventB != null)
EventB(a);
}
[ComRegisterFunction()]
public static void RegisterClass ( Type t )
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need
it
string sb = @"CLSID\" + t.GUID.ToString("B");
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;
ctrl.Close ( ) ;

using( RegistryKey subkey = k.CreateSubKey("MiscStatus") )
{
subkey.SetValue("", "131473");
}

using( RegistryKey subkey = k.CreateSubKey("MiscStatus\\1") )
{
subkey.SetValue("", "197009");
}

// Next create the CodeBase entry - needed if not string named and
GACced
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);
inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;

// Now create the Version entry
RegistryKey vers = k.CreateSubKey("Version");
vers.SetValue("", "1.0");
vers.Close();

RegistryKey inst = k.CreateSubKey ( "Insertable" ) ;
inst.Close ( ) ;

// And the Type Lib Entry
using( RegistryKey subkey = k.CreateSubKey("TypeLib") )
{
Guid libid = Marshal.GetTypeLibGuidForAssembly(t.Assembly);

subkey.SetValue("", libid.ToString("B"));
}
// Finally close the main key
k.Close ( ) ;
}
[ComUnregisterFunction()]
public static void UnregisterClass ( string key )
{
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );
// Delete the 'Control' key, but don't throw an exception if it does
not exist
k.DeleteSubKey ( "Control" , false ) ;

// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true
);

// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey ( "CodeBase" , false ) ;

k.DeleteSubKey ( "TypeLib" , false ) ;

k.DeleteSubKey ( "Version" , false ) ;

// Finally close the main key
k.Close ( ) ;
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(192, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// CSharpUserControl
//
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "CSharpUserControl";
this.Size = new System.Drawing.Size(208, 64);
this.ResumeLayout(false);
}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
OnMyEventA();
}
}
}

Jan 1 '06 #3
Thanks
Asaf

Jan 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vetrivel | last post by:
Application architecture : Develop interface between two existing systems, a. Enterprise CRM system b. Web based intranet system. Environment : Intranet Server : IIS and ASP. Script :...
1
by: Adam Hearn | last post by:
Sorry if this difficult to understand but I'm pulling my hair out and could do with some good ideas please... I've developed an application which in .NET as a Windows service which is simply...
0
by: MarkD | last post by:
I have an ASP.NET (VB.NET) application that calls all VB6 COM DLL via Interop. The DLL uses functionality contained in a Custom OCX Control (Also VB6) that in turn contains a standard TreeView...
1
by: Adam Hearn | last post by:
Sorry if this difficult to understand but I'm pulling my hair out and could do with some good ideas please... I've developed an application which in .NET as a Windows service which is simply...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.