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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows API calls 2

Status
Not open for further replies.

clegg

Programmer
Jan 25, 2001
98
GB
Does anyone know of any good resources that explain (preferably with examples) how to do API calls in COBOL. I'm using MF Netexpress 3.0 and to be honest the help files aren't much use! I've used API calls in VB but i'm not sure how the variables etc translate into COBOL.

Many thanks

Clegg
 
At the moment I'm trying to use GetComputerName. I'm not having much look - I keep getting 'attempt to access beyong bounds of memory' error.

However, help on using API's generally would be useful as I also want to browse the network and get information about logged on users, network printers etc.

Thanks again

Clegg
 
I have a sample from WB 4.0 which is too long to paste here but which I could zip and email to you if you like.

Do you have a special-names section with call-convention coded. eg.
SPECIAL-NAMES.
CALL-CONVENTION 74 IS WINAPI.

on USING parameters in the call you need to differentiate between BY REFERENCE and BY VALUE to be compatible with "C" calls.
HTH,
Clive
 
Hi Clive,

Yes I've got the special-names section exactly as you've outlined. If you could email me your example that would be a great help - mark@distnetsys.com

Many thanks

Clegg
 
Clegg,

This one works.
Perhaps you omit the BY REFERENCE clauses. This will almost certainly cause an access violation.

IDENTIFICATION DIVISION.
PROGRAM-ID. GETCOMPUTERNAMEPROGRAM.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CALL-CONVENTION 74 IS WINAPI.

DATA DIVISION.
WORKING-STORAGE SECTION.

01 COMPUTERNAME PIC X(50) VALUE SPACES.
01 NAME-LENGTH PIC X(04) COMP-5.
01 FUNCTIONRETURN PIC X(04) COMP-5.

PROCEDURE DIVISION.
MAIN SECTION.
M000.
SET NAME-LENGTH TO LENGTH OF COMPUTERNAME.
CALL WINAPI "GetComputerNameA"
USING BY REFERENCE COMPUTERNAME
BY REFERENCE NAME-LENGTH
RETURNING FUNCTIONRETURN.
IF FUNCTIONRETURN = 0
DISPLAY "GetComputerName failed"
ELSE
DISPLAY COMPUTERNAME.
STOP RUN.


Marcel
 
That looks like just what you need. Let me know if you still want the sample stuff.
 
I had the following code:

move length of lpbuffer to nSize.
call Win32api GetComputerName using
by reference lpbuffer
by value nsize
returning lretval
end-call.

I changed the 'by value' to 'by reference' and hey presto it works.

My question is this - how do you know whether to use by reference or by value given that the declaration when looked at through VB is:

Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Any guidance would be appreciated - and many thanks for the solution.

Clegg
 
Generally, when calling any "C" routine you have to know what the C header says I think.
 
If you look in the platform SDK documentation, you find:

BOOL GetComputerName(
LPTSTR lpBuffer, // computer name
LPDWORD lpnSize // size of name buffer
);

lpnSize is of type LPDWORD, that is a pointer to a DWORD. So you need to pass an address and not a value, so BY REFERENCE.

If you compare that with this one:

DWORD GetCurrentDirectory(
DWORD nBufferLength, // size of directory buffer
LPTSTR lpBuffer // directory buffer
);

you can see nBufferLength is of type DWORD, this is a value. So in this case you should have in your code:

CALL "GetCurrentDirectoryA" USING BY VALUE NBUFFERLENGTH
BY REFERENCE CURDIR
RETURNING FUNCTIONRETURN.

Marcel

 
and of course you don't forget the call convention
(CALL WINAPI "GetCurrentDirectoryA" ...)


 
Many thanks to evryone for their help - much appreciated!

Clegg
 
I tried to compile the code supplied by MKuiper with RM 7.00.03. There were so many errors there is no room here. Can RM make API calls?
 
This is because the code is using Micro Focus proprietary extensions.

The RM/COBOL approach to this is to keep the COBOL CALL standard, and use CodeBridge to do the interface between COBOL and C (this includes data types, calling conventions, etc). Then the COBOL programmer doesn't have to worry about "C calling conventions" (mentioned by Clive above) or whether the called program even understands (for example) packed decimal data types.

CodeBridge ships with the RM/COBOL Development System. Appendix B of the manual has an example of calling a Windows API function.
Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top