VB6 exe call C# exe

jdy0803

Member
Joined
May 20, 2013
Messages
17
Programming Experience
10+
My VB6 program should call some device interface program.
I should make this device interface program using C#.
Once VB6 program call C# execution file, C# program should run on the top most until it terminates.
1. How to make run C# program on the top most?
Should it be done in VB6? or C#?
2. I should make VB6 pass some parameter to the C# and C# return some parameter to the VB6.
How to do this?
 
Are you determined that it be a separate program? It sounds to me that you might be better off creating a DLL in C# and configuring it to be accessible from COM applications. Your VB6 program can then integrate it the same way it would any other COM DLL.
 
No, I want to make separate execution file using C#
FYI, passing parameter is like this.

VB6 to C# : 1. offset 2. gain 3.min 4. max
C# to VB6 : 1. file name 2. status
 
Making the C# app appear on top of other windows is as simple as setting the TopMost property of your form to True.

Receiving data in the application would be done via the commandline, so your Main method would look something like this:
static void Main(string[] args)
{
    var offset = 0;
    var gain = 0;
    var min = 0;
    var max = 0;

    if (args.Length > 0)
    {
        int.TryParse(args[0], out offset);
    }

    if (args.Length > 1)
    {
        int.TryParse(args[1], out gain);
    }

    if (args.Length > 2)
    {
        int.TryParse(args[2], out min);
    }

    if (args.Length > 3)
    {
        int.TryParse(args[3], out max);
    }

    // Use variables here.
}
That code will user zero for missing or invalid arguments, but you can vary as appropriate. You would then pass the appropriate values on the commandline when you call Shell in VB6. That's a VB6 issue though, so I won't go into any more detail about it here.

It's not really possible to pass data back other than a single number via an exit code though. For anything more complex than that, you'll have to define some sort of communication protocol, the simplest of which would probably be for the C# app to write to a file in a prescribed location. Reading that file in the VB6 app is again an issue not for discussion here.
 
Thanks for detailed answer.
My C# program will be Windows Forms Application, not Console Application.
Even in this case, I should do simillar way, is it right?
 
Thanks for detailed answer.
My C# program will be Windows Forms Application, not Console Application.
Even in this case, I should do simillar way, is it right?
Given the fact that I suggested that you set the TopMost property of a form, it's fairly clear that I assume that it was a Windows Forms app. There are other options for getting the commandline arguments though, whether or not it's a Windows Forms app. You can also call Environment.GetCommandLineArgs if you want the arguments somewhere other than the Main method, e.g. the Load event handler of your main form.
 
Given the fact that I suggested that you set the TopMost property of a form, it's fairly clear that I assume that it was a Windows Forms app. There are other options for getting the commandline arguments though, whether or not it's a Windows Forms app. You can also call Environment.GetCommandLineArgs if you want the arguments somewhere other than the Main method, e.g. the Load event handler of your main form.
Thanks.
I'll try Environment.GetCommandLineArgs.
 
Back
Top Bottom