Highlighted
Absent Member.
Absent Member.
4097 views

CMD synchronous call from VS COBOL managed code

Jump to solution

[Migrated content. Thread originally posted on 16 November 2011]

Hello,

Is there any way to call synchronously a VS COBOL native program from a VS COBOL managed code?

If I use CBL_EXEC_RUN_INIT with flag = 1, the cmd window is not shown and the parent program gets stuck. But, if I use flag = 101, the newly created window is independent from its parent.

Regards
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
I believe that you are still having some issues with understanding Main Application vs Class Library usage and static vs instance methods.

I am attaching an example which uses two projects, the first being a Windows Form Application and the second one being a Class Library.

The Windows Form Application displays a single form with 3 buttons on it. Pressing one of the buttons will cause a call into the Class Library project to either:

1. Create a new instance of a Form class in the Class Library and invoke its ShowDialog method.
2. Call a static method in the Class Library while passing a string parameter.
3. Doing a COBOL Call to a procedural COBOL program in the Class Library.

I hope that this will clear up some of the questions you might have regarding calling managed code from other managed code. You should not have two full Windows Forms Application projects in your Solution. You should instead have one driving application which calls out to various other Class Libraries.

I hope this helps...

View solution in original post

0 Likes
10 Replies
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Another issue I found is that I need to call a managed code project from another one.

In order to do so, I included the called project into the solution of the calling one. I spotted a brief explanation about this in the documentation:

If a program in your managed project is calling a program in another managed project you need to add a project or a file reference in the calling project to the project containing the program to be called. To do this:

In Solution Explorer, select the calling project.
Click Project > Project Properties.
Click the References tab.
Click Add.
Choose Projects or Browse to add a project or a file reference.

Once this is done, you can use the classes and methods from the referenced project in the calling project.

For information on calling native code from a managed program see Interoperating with Unmanaged COBOL.


My calling project is named "MNGMenuWin" and I want to call "VsCOBOLR4OPos.Main". I tried
call "VsCOBOLR4OPos.Main" with no results.

Regards
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Hi, Voyager

there is a sample about integrating RM COBOL files in the list of documents on this website, where you can see how to call a native COBOL program from a managed one.

A recent addendum even explains how to handle debugging in that case.

best regards


Joachim Blome
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Thank you.

After checking out this example, I have got my code running: Example
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
To call an unmanaged program from a managed program the following items must be true:

1. The managed code project must have the same configuration as the native project, it cannot be AnyCpu. In your example your native project is built as x86 so you must change the managed project to be x86 also.

2. The native .dll must be in the same folder as the managed .EXE doing the call. The easiest way to do this is to change the Output Path on the COBOL Property page to place it in the same folder as the EXE project.
e.g. C:\voyager\WinMto003\WinMto003\bin\x86\debug

3. When you do the call it is looking for a .dll name that contains the entry point in the call statement.
In your native project the .dll name is RMNat003.dll but the actual entry point of the program is called PROGRAM1 because this is the name of the .cbl source and you are not using the preservecase directive.

In order to call an entry point in a .dll that is different than the .dll name itself you have to first load the .dll and then call the entry point:


       method-id btnRead_Click final private.
       01 proc-pointer    procedure-pointer.
       procedure division using by value sender as object e as type System.EventArgs.
           initialize param-003.
           set rcodigo-of-003 to self::txtCodigo::Text.
           set type-of-operation to 1.
           set proc-pointer to entry "RMNat003"
           call "PROGRAM1" using param-003.
           set self::txtNombre::Text to name-of-003.


If you changed the name of Program1.cbl to RMNat003.cbl then you would not need to do this extra step.

Your original question said that you wished to start a native program from a managed application and run synchronously. Is that really what you are trying to accompish here?

Also, are you still having an issue calling a managed program from another managed program?
Perhaps you can clarify your actual requirements here?

Thanks.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Hello,

Thanks for your deep explanation on calling managed code from unmanaged.

Please find my answers below.

Regards

Chris Glazier originally wrote:
Your original question said that you wished to start a native program from a managed application and run synchronously. Is that really what you are trying to accompish here?

Yes, I want to open a "modal" cmd window with the execution of native program inside.

Chris Glazier originally wrote:
Also, are you still having an issue calling a managed program from another managed program?
Perhaps you can clarify your actual requirements here?

Yes, the calling app cannot find the target, and I do not know what I am doing wrong.

The following line has a mark at the end that says "COBCH0987: Could not f8ind method 'WinMto00' with this signature - will generate dynamic call":
call "WinMto003".
The entry point in 'WinMto003' is 'Main.cbl', with this ID:
class-id WinMto003.Main public.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Having defined this class:
       class-id WinMto003.Main public.
       
       method-id Main static
           attribute System.STAThread.
       local-storage section.
       01 mainForm type WinMto003.Form1.
       procedure division.
       
           set mainForm to new WinMto003.Form1()
           invoke type System.Windows.Forms.Application::EnableVisualStyles()
           invoke type System.Windows.Forms.Application::Run(mainForm)
           goback.
       
       end method.
       
       end class.

... I have been trying to call it having these definition:
       01 winMto type WinMto003.Main.

This way:
              set winMto to new WinMto003.Main().
              invoke self::winMto.

I have no warnings, but the program crashes when executing 'invoke'.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
I am a little unsure of what you are trying to accomplish here with managed code calling managed code as this looks like you are trying to invoke one main project from another main project, is that correct?

You should use CBL_EXEC_RUN_UNIT if you wish to have two main applications running at once.

Otherwise, you should really only have one main application that is built as an .exe and then your other projects should be built as class libraries which are .dlls.

You then add a reference to the class library project in the main project and then you create a new instance of the class and invoke its methods.

What is it that you are really trying to accomplish with your code here, is it your intention to have several main applications running simultaneously?

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Chris Glazier originally wrote:
Your original question said that you wished to start a native program from a managed application and run synchronously. Is that really what you are trying to accompish here?


Calling managed code from a managed project is working now, but I am still not sure how to do that.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
Uff... I want to implement at least one call from managed to managed without using CAL_EXEC_RUN_UNIT. In order to do so, I have converted the called program into a DLL, but I do not know how to call it:

       class-id WinMto003.Main public.
       
       method-id Main static
           attribute System.STAThread.
       local-storage section.
       01 mainForm type WinMto003.Form1.
       procedure division.
       
           set mainForm to new WinMto003.Form1()
           invoke type System.Windows.Forms.Application::EnableVisualStyles()
           invoke type System.Windows.Forms.Application::Run(mainForm)
           goback.
       
       end method.
       
       end class.

I suspect that WinMto003.Form1 can be execute because inherits from System.Windows.Forms.Application. But what about WinMto003.Main?

The following lines do not work, too:

01 winMto type WinMto003.Main.
...
set winMto to new WinMto003.Main().
invoke self::winMto.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: CMD synchronous call from VS COBOL managed code

Jump to solution
I believe that you are still having some issues with understanding Main Application vs Class Library usage and static vs instance methods.

I am attaching an example which uses two projects, the first being a Windows Form Application and the second one being a Class Library.

The Windows Form Application displays a single form with 3 buttons on it. Pressing one of the buttons will cause a call into the Class Library project to either:

1. Create a new instance of a Form class in the Class Library and invoke its ShowDialog method.
2. Call a static method in the Class Library while passing a string parameter.
3. Doing a COBOL Call to a procedural COBOL program in the Class Library.

I hope that this will clear up some of the questions you might have regarding calling managed code from other managed code. You should not have two full Windows Forms Application projects in your Solution. You should instead have one driving application which calls out to various other Class Libraries.

I hope this helps...

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.