Highlighted
Absent Member.
Absent Member.
986 views

[archive] GetComputerName

[Migrated content. Thread originally posted on 30 December 2005]

Looking into the API-exemples, I don't get my routine to work

01 lpBuffer PIC X(250).
01 lpnSize PIC X(8) COMP-N.

SET ENVIRONMENT "DLL_CONVENTION" TO "1".
CALL "Kernel32.DLL".

CALL "GetComputerNameA" USING
BY REFERENCE lpBuffer
BY REFERENCE lpnSize
END-CALL.

The variables stay empty. Where are the bugs ?
0 Likes
6 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

Your code works on my Windows XP.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

Indeed ... I tried this at home and my routine works, except one small thing. My comp. at home call 'Brother Home' and the routine just returns 'Brother', even if I rename my comp. to 'Brother-Home'.

Anyway, I tried the same routine on my laptop now at home and the fields stay blank.

Any reason for this to happen ?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

I just use the following - seems to work OK.

accept LNK-COMPUTER-NAME from EVIRONMENT "COMPUTERNAME".
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

Originally posted by Hans
Looking into the API-exemples, I don't get my routine to work

01 lpBuffer      PIC X(250).
01 lpnSize       PIC X(8) COMP-N.

           SET ENVIRONMENT "DLL_CONVENTION"       TO "1".
           CALL "Kernel32.DLL".

           CALL "GetComputerNameA" USING
                BY REFERENCE lpBuffer
                BY REFERENCE lpnSize
           END-CALL.

The variables stay empty. Where are the bugs ?


A number of problems here:

1. You do nowhere set the size of lpBuffer. This should be set with th lpnSize variable. The function takes this variable on input to determine if it has space to hold the name of the computer. To be exact, set lpnSize to 250. Upon return, this variable will hold the actual length of the computer name copied to the buffer.

2. To my knowledge, the second parameter in this case is a DWORD, DWORD is 4 bytes, thus, the size of the second parameter is too big. Reduce it to 4.

That this works on some computers and not on others is because your variables are not initialized, thus, whatever is currently present in the memory of the variable at execution time is used.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

Txs Ghisle and others ...

The accept comp-name from environment "COMPUTERNAME" works ok.

And Ghisle, I changed my project with the comments you suggested and now it works ok.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] GetComputerName

Just as a footnote to the accept computername solution - I believe that you must be careful with older versions of Windows - sometimes the environment name is different and sometimes not there at all on 98 and earlier.
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.