EDN Admin
Well-known member
I hope Im posting this on the correct forum.
I have written a C# DLL which I exposed to COM in order to support the needs of one of our legacy VB6 clients. I have shown the C# DLL code below. That legacy client also includes a GUI configuration tool. The VB6 client successfully loads (CreateObject)
the COM DLL. When a "specify connection" button is clicked on the VB6 clients GUI, it successfully invokes the Connection() method in the COM DLL, which in turn launches a simple windows form (well call it the "specify email address form") on which the user
can specify an email address in a text box and click OK to close the form. The form saves the entered email address to a string property, which upon return to the DLLs Connection() method, is returned to the VB6 client, which in turn saves it to the
SQL Server 2005 database. The entire process is successful, including saving of returned data to database. However, despite the COM DLL method having returned its data back to the VB6 client, the "specify email address form" which was launched by the
COM DLL remains up and in a frozen state for several moments after which it, along with the VB6 client crashes.
For experimentation, Ive entirely commented out the launch of the "specify email address form" in the COM DLL Connection() method and instead just returned a hard-coded email address string. When I did this, the freeze described above did not
occur, which leads me to suspect what I am doing with the windows form.
I suspect having limited experience with windows forms programming Im missing one/several things. Any help would be greatly appreciated. Again, Ive included the code below.
<div style="color:Black;background-color:White; <pre>
<span style="color:Gray; ///<span style="color:Green; / code for the COM interface and class
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; namespace SgvMsgEmail
{
[Guid(<span style="color:#A31515; "D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
<span style="color:Blue; public <span style="color:Blue; interface ISgvMessage
{
[DispId(1)]
<span style="color:Blue; void SgvOutputMessage(<span style="color:Blue; long Channel, <span style="color:Blue; long AlarmID, <span style="color:Blue; string TextMessage);
[DispId(2)]
<span style="color:Blue; void Configure();
[DispId(3)]
<span style="color:Blue; void AboutDevice();
[DispId(4)]
<span style="color:Blue; string Connection();
[DispId(5)]
<span style="color:Blue; string Name { <span style="color:Blue; get; }
[DispId(6)]
<span style="color:Blue; string Description { <span style="color:Blue; get; }
}
[Guid(<span style="color:#A31515; "13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")]
[ComVisible(<span style="color:Blue; true), ClassInterface(ClassInterfaceType.None)]
<span style="color:Green; //[ProgId("Tester.Numbers")]
<span style="color:Blue; public <span style="color:Blue; class SgvMessage : ISgvMessage
{
<span style="color:Blue; private <span style="color:Blue; const <span style="color:Blue; string myName = <span style="color:#A31515; "Sgv Message Email";
<span style="color:Blue; private <span style="color:Blue; const <span style="color:Blue; string myDescription = <span style="color:#A31515; "Sgv Message Router Email Client";
<span style="color:Blue; public SgvMessage() {
Name = myName;
Description = myDescription;
}
<span style="color:Blue; public <span style="color:Blue; void SgvOutputMessage(<span style="color:Blue; long Channel, <span style="color:Blue; long AlarmID, <span style="color:Blue; string TextMessage)
{
}
<span style="color:Blue; public <span style="color:Blue; void Configure()
{
frmConnection frmConnect = <span style="color:Blue; new frmConnection();
frmConnect.Show();
}
<span style="color:Blue; public <span style="color:Blue; void AboutDevice()
{
}
<span style="color:Blue; public <span style="color:Blue; string Connection()
{
frmConnection frmConnect = <span style="color:Blue; new frmConnection();
Application.Run(frmConnect);
<span style="color:Blue; return frmConnect.Connection;
}
<span style="color:Blue; public <span style="color:Blue; string Name { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Description { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string ConnectionString { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
}
}
<span style="color:Gray; ///<span style="color:Green; / code for the windows form
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; namespace SgvMsgEmail
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class frmConnection : Form
{
<span style="color:Blue; public frmConnection()
{
InitializeComponent();
}
<span style="color:Blue; public <span style="color:Blue; string Connection { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; private <span style="color:Blue; void btnOK_Click(<span style="color:Blue; object sender, EventArgs e)
{
Connection = textEmailAddress.Text;
Close();
}
}
}
[/code]
<br/>
<br/>
View the full article
I have written a C# DLL which I exposed to COM in order to support the needs of one of our legacy VB6 clients. I have shown the C# DLL code below. That legacy client also includes a GUI configuration tool. The VB6 client successfully loads (CreateObject)
the COM DLL. When a "specify connection" button is clicked on the VB6 clients GUI, it successfully invokes the Connection() method in the COM DLL, which in turn launches a simple windows form (well call it the "specify email address form") on which the user
can specify an email address in a text box and click OK to close the form. The form saves the entered email address to a string property, which upon return to the DLLs Connection() method, is returned to the VB6 client, which in turn saves it to the
SQL Server 2005 database. The entire process is successful, including saving of returned data to database. However, despite the COM DLL method having returned its data back to the VB6 client, the "specify email address form" which was launched by the
COM DLL remains up and in a frozen state for several moments after which it, along with the VB6 client crashes.
For experimentation, Ive entirely commented out the launch of the "specify email address form" in the COM DLL Connection() method and instead just returned a hard-coded email address string. When I did this, the freeze described above did not
occur, which leads me to suspect what I am doing with the windows form.
I suspect having limited experience with windows forms programming Im missing one/several things. Any help would be greatly appreciated. Again, Ive included the code below.
<div style="color:Black;background-color:White; <pre>
<span style="color:Gray; ///<span style="color:Green; / code for the COM interface and class
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; namespace SgvMsgEmail
{
[Guid(<span style="color:#A31515; "D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
<span style="color:Blue; public <span style="color:Blue; interface ISgvMessage
{
[DispId(1)]
<span style="color:Blue; void SgvOutputMessage(<span style="color:Blue; long Channel, <span style="color:Blue; long AlarmID, <span style="color:Blue; string TextMessage);
[DispId(2)]
<span style="color:Blue; void Configure();
[DispId(3)]
<span style="color:Blue; void AboutDevice();
[DispId(4)]
<span style="color:Blue; string Connection();
[DispId(5)]
<span style="color:Blue; string Name { <span style="color:Blue; get; }
[DispId(6)]
<span style="color:Blue; string Description { <span style="color:Blue; get; }
}
[Guid(<span style="color:#A31515; "13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")]
[ComVisible(<span style="color:Blue; true), ClassInterface(ClassInterfaceType.None)]
<span style="color:Green; //[ProgId("Tester.Numbers")]
<span style="color:Blue; public <span style="color:Blue; class SgvMessage : ISgvMessage
{
<span style="color:Blue; private <span style="color:Blue; const <span style="color:Blue; string myName = <span style="color:#A31515; "Sgv Message Email";
<span style="color:Blue; private <span style="color:Blue; const <span style="color:Blue; string myDescription = <span style="color:#A31515; "Sgv Message Router Email Client";
<span style="color:Blue; public SgvMessage() {
Name = myName;
Description = myDescription;
}
<span style="color:Blue; public <span style="color:Blue; void SgvOutputMessage(<span style="color:Blue; long Channel, <span style="color:Blue; long AlarmID, <span style="color:Blue; string TextMessage)
{
}
<span style="color:Blue; public <span style="color:Blue; void Configure()
{
frmConnection frmConnect = <span style="color:Blue; new frmConnection();
frmConnect.Show();
}
<span style="color:Blue; public <span style="color:Blue; void AboutDevice()
{
}
<span style="color:Blue; public <span style="color:Blue; string Connection()
{
frmConnection frmConnect = <span style="color:Blue; new frmConnection();
Application.Run(frmConnect);
<span style="color:Blue; return frmConnect.Connection;
}
<span style="color:Blue; public <span style="color:Blue; string Name { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Description { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string ConnectionString { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
}
}
<span style="color:Gray; ///<span style="color:Green; / code for the windows form
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; namespace SgvMsgEmail
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class frmConnection : Form
{
<span style="color:Blue; public frmConnection()
{
InitializeComponent();
}
<span style="color:Blue; public <span style="color:Blue; string Connection { <span style="color:Blue; get; <span style="color:Blue; private <span style="color:Blue; set; }
<span style="color:Blue; private <span style="color:Blue; void btnOK_Click(<span style="color:Blue; object sender, EventArgs e)
{
Connection = textEmailAddress.Text;
Close();
}
}
}
[/code]
<br/>
<br/>
View the full article