Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API Constant value too large in COBOL call 1

Status
Not open for further replies.

tonyc1

Programmer
Jan 10, 2002
16
GB
I'm using COBOL 97 to call windows API function CreateFile to create a handle to an existing windows pipe. I want to use the FILE_FLAG_OVERLAPPED constant value in the dwFlagsandAttributes parameter. But.....
The value of FILE_FLAG_OVERLAPPED is &H4000000, in decimal its 1073741824. If I use VB to call the API it accepts that value but in COBOL it will not compile saying that the value is too large. I've tried a couple of things:
1. putting the value in as part of the call (BY VALUE 1073741824)
2. creating a field (as PIC S9(9) COMP-5 COBOL equivalant to long) to hold the value.
Thanks
 
Hi Tony

The correct Hex value is 67108864.
You can set the field as:

01 VAR1 PIC S9(9) COMP-5 value 67108864.

The calculation is easy:
call your windows calculator, set the calculator in hex mode, put the number 4000000, then choose "dec" mode..you will see the result as 67108864, that can be assigned to a COMP-5 field.

Hope in this help

Gianni
 
Thanks for the prompt reply Gianni but I think there is still a problem, unless I am missing something basic about Hex values in API. Using the API text viewer to look at the WinAPI32 text file FILE_FLAG_OVERLAPPED is value &H40000000 - seven zeroes not six thus using the windows calculator gives 1073741824 which does not fit.
 
ooops it was my fault I only put six zeroes in my first post sorry to mislead you but any ideas?
 
If this is about Fujitsu cobol:

03 FIELD-NAME PIC 9(09) COMP-5 VALUE 1073741824.
The statement above will generate a compiler error.

But:

03 FIELD-NAME PIC 9(09) COMP-5.
...
MOVE 1073741824 TO FIELD-NAME.
will generate a compiler warning only, but executes correctly.

Marcel
 
Sorry, this is about Fujitsu Cobol, it is the Fujitsu Cobol discussion. I was thinking for a moment it was in the general cobol discussion. Marcel
 
Thanks I will try that and let you know how it goes
 
To move slightly away from the original question how do i combine 2 values in a COBOL API call? EG I'm calling createfile and want to specify desiredaccess as GENERIC_READ (&H80000000)and GENERIC_WRITE (&H40000000). I think you can use both values in C++ and VB (not sure how in VB either!)
 
I seem to have the value working ok now thanks Marcel but still not sure about combining values. A value of 11 for desired access seems to work but why i don't know
Thanks
 
tony,

you can combine 2 values using CALL "CBL_OR". It is described in the "CBL Subrourines User's Guide", chapter 10 ("Logic Operator Routines".

example:
03 GENERIC-READ PIC 9(09) COMP-5.
03 GENERIC-WRITE PIC 9(09) COMP-5.
03 GENERIC-READ-WRITE PIC 9(09) COMP-5.

...

MOVE 2147483648 TO GENERIC-READ. *> 80000000h
MOVE 1073741824 TO GENERIC-WRITE. *> 40000000h
* COMBINE read and write
MOVE GENERIC-READ TO GENERIC-READ-WRITE.
CALL "CBL_OR" USING GENERIC-WRITE
GENERIC-READ-WRITE
4.
* Now GENERIC-READ-WRITE should contain the combination

But, if you are sure there is not a single bit which is "1" in both GENERIC-READ and GENERIC-WRITE (which is the case in this example), you can also simply add them together:
ADD GENERIC-READ GENERIC-WRITE GIVING GENERIC-READ-WRITE.
Marcel
 
Tony,

I am just thinking about this:
Cobol is not the best language for doing bit-operations the way you want. I should consider making a little C-dll which is called from Cobol and fills all your constants in one single pass.

The c-part should be (something like):
Code:
#include <windows.h>

DWORD Constants[] = { GENERIC_READ,
                      GENERIC_WRITE,
                      GENERIC_READ | GENERIC_WRITE,
                      FILE_SHARE_READ,
                      FILE_SHARE_WRITE,
                      FILE_SHARE_READ | FILE_SHARE_WRITE,
                      NULL,
                      ....,
                      12345678 };

__declspec( dllexport ) void FillMyCobolConstants
                ( DWORD *CobolConstants,
                  DWORD  cbCobolConstants )
{
  DWORD dwLength = cbCobolConstants < sizeof ( Constants )
                       ? cbCobolConstants
                       : sizeof ( Constants );
  memcpy ( CobolConstants, Constants, dwLength ); }
Comnpile the above in C to a dll and be sure FillMyCobolConstants is exported.

and the cobol part should be something like:
Code:
01  API-CONSTANTS.
    03  GENERIC-READ          PIC 9(09) COMP-5.
    03  GENERIC-WRITE         PIC 9(09) COMP-5.
    03  GENERIC-READ-WRITE    PIC 9(09) COMP-5.
    03  FILE-SHARE-READ       PIC 9(09) COMP-5.
    03  FILE-SHARE-WRITE      PIC 9(09) COMP-5.
    03  FILE-SHARE-READ-WRITE PIC 9(09) COMP-5.
    03  NULL-CONSTANT         PIC 9(09) COMP-5.
    03  ....
    03  CHECK-12345678        PIC 9(09) COMP-5.
...
CALL &quot;FillMyCobolConstants&quot; USING
        BY REFERENCE API-CONSTANTS
        BY VALUE LENGTH OF API-CONSTANTS.
IF  CHECK-12345678 NOT = 12345678
    DISPLAY &quot;Error in API-CONSTANT Table&quot;
    STOP RUN.
Be sure to include the appropriate .lib file in the linkage specification.
Let me know if you need more help with this.
Marcel
 
Thanks again Marcel for your help. Unfortunately I do not know C and I'm re-writing a legacy COBOL program and adding API calls to it.
I seem to have something working now but i will also try the methods you describe to see what it does.
Thanks again
 
Marcel,

just read your answer in search how to call a c-dll from cobol. How do you specify the name of the dll which exports FillMyCobolConstants? Is it in the &quot;...-section&quot; of your listing?

Thanks
Stephan
 
Stephan,

There are two possibilities:
1. Create an .obj file from the C-part and link it together with your cobol source. Result will be one .exe which can be executed without any other files needed (apart from the cobol runtime of course).
2. Create a .dll file from the C-part. This will also create a .lib file. Include the .lib when linking your cobol program. In this case you need sour program .exe as well as the .dll in order to run.

Marcel
 
Marcel,

thanks for the immediate response.

I've already got a .dll file which exports the needed functions. It is called from different environments, for example Smalltalk and VisualBasic. In all these environments you have to import the function with name of the .dll file and the name of the exported function.

In your listing above you call the function by
CALL &quot;FillMyCobolConstants&quot; USING
BY REFERENCE API-CONSTANTS
BY VALUE LENGTH OF API-CONSTANTS.

But where do you declare the name of the .dll file?

If possible, can you please give a short, but complete listing of a program, which calls one function of the .dll file?

Thanks
Stephan
 
Ah, I get the picture. You mean you have no .obj and no .lib file, just the .dll.

I have bad news and good news for you.

The bad news is I did not figure out (yet ??) how to do that in Fujitsu Cobol. I tend to believe it is not possible.

The good news is I need this feature myself at this very moment, so I'm working on it. At the moment I have a working solution, which only needs to be fine-tuned. It will work using a little C-program which is called from Cobol, which will call any exported dll-function the way you want to.

Once it is finished (should be tomorrow or the day after), I will send you a copy of the .obj, .lib and .dll if you mail to xxxxxx.kuiper@home.nl (xxxxxx = Marcel).




Marcel
 
Long numbers for API calls

Why not do this:

01 long-number pic 9(18) comp-5.
01 filler redefines long-number.
03 short-number pic 9(9) comp-5.
03 filler pic 9(9) comp-5.

......

move 2147483658 to long-number.
call &quot;apiroutine&quot; using by reference short-number etc

No need to depend on 'features' of different versions of COBOL etc.

Hi everybody - just found you all and it seems a lively place to be.

Has anyone managed to call Windows Registry functions successfully?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top