a DotNet program in combination with VB6 program

boes

Newcomer
Joined
Jun 21, 2002
Messages
17
Location
Belgium
I am building an application in .Net that works in combination with a VB6 program. I start the VB6 program out of my .Net program This VB6 program saves several data to a database. I want to see those changes in my .Net program a soon as I end my VB6 program. Example: I have a form in my .Net program. On this form I have a datagrid. Pushing a button the form of my VB6 program is showed (my VB6 program is started). There I save some data that has to be showed in the datagrid in my .Net program. When I close my VB6 form (actually end the VB6 program) I want to see the saved data in my datagrid of my .Net form.
Is it possible to program what I want?
 
You can shell out to another process and wait for it to return. When your VB6 application ends, you can query the database to do what you need to do.

From within VB.NET, use:
Visual Basic:
        ' The "True" param is what makes the Shell command wait
        Shell("project1.exe", AppWinStyle.NormalFocus, True)
        MessageBox.Show("done")

For C#, you'll have to use the VB Shell command.
First, add a reference to "Microsoft Visual Basic .NET Runtime" (the name in the Add References dialog). The name in the project, when you expand References, is "Microsoft.VisualBasic".

Then use this code to shell out:
C#:
Microsoft.VisualBasic.Interaction.Shell("project1.exe", Microsoft.VisualBasic.AppWinStyle.NormalFocus, true, -1);
MessageBox.Show("done");

-Nerseus
 
I would never use that function in the VB runtime to start a process and wait for it to finish. Instead I'd use System.Diagnostics.Process.Start, and listen for the event raised when the process has terminated.

Alternatively, the Executor class has a static method, ExecWait, which wraps that functionality for you.

[mshelp]ms-help://MS.NETFrameworkSDK/cpref/html/frlrfSystemCodeDomCompilerExecutorClassExecWaitTopic.htm[/mshelp]
 
Ah, thanks divil. I didn't realize the Process class wasn't JUST static methods, my first assumption. I didn't see a way for the program to check for termination.

Here's a code snippet to use the Process class to start a process. I couldn't figure out a built in way to WAIT for completion, as the original Shell method works. You may have to build in your own logic to simulate waiting on data. Your requirements sound like you may not need to actually wait as you're mainly interested in being notified when the VB6 app is done so you can take some action.

C#:
private void OnProcessExited(object sender, System.EventArgs e)
{
	Debug.WriteLine("done");
}

private void button1_Click(object sender, System.EventArgs e)
{
	System.Diagnostics.Process p1 = new System.Diagnostics.Process();
	p1.StartInfo.FileName = "project1.exe";
	p1.EnableRaisingEvents = true;
	p1.Exited += new System.EventHandler(this.OnProcessExited);
	p1.Start();
}

-nerseus
 
Hey thanks guys for the quick reply. Divil, I tried the ExecWait method but couldn't figure it out. ExecWait(cmd : String, tempFiles : TempFileCollection) The cmd is that the name of the program I want to start or a commandstring? I don't understand how to get the tempfilecollection programmed right? Can you give me an example on how to progam this ExecWait method. maybe you also need to know that I'm passing througd a commandstring to the VB6 application.
 
Hey Nerseus, I'm going to check your method tomorrow when I'm back at work. So with this line
p1.Exited += new System.EventHandler(this.OnProcessExited);
I should be able to go further in my .Net program after ending the VB6 program?
 
Ignore my suggestion unless you don't mind your VB6 process having no user interface - the ExecWait method supresses all windows being displayed.

If this is ok, here's a sample:

Visual Basic:
System.CodeDom.Compiler.Executor.ExecWait("notepad.exe", New System.CodeDom.Compiler.TempFileCollection())

Not a very good sample obviously since you won't be able to close notepad without using its interface (or the task manager).
 
I'm not sure what you mean by "I should be able to go further in my .Net program after ending the VB6 program?". Your code will continue running after the line:
p1.Start();

If you want your program to halt until the VB6 app terminates, you'll have to simulate the "halt" yourself (by disabling the form or something similar) or by using the Shell method. The line that hooks up the event handler (p1.Exited += new System...) won't halt at all. It's just hooking up the event so that when the process ends, it will call your function (OnProcessExited in my example).

-ner
 
Back
Top