PDA

View Full Version : Computer Name API Doesn't Work



CreganTur
11-12-2008, 12:59 PM
I'm currently working through the Wrox book Expert Access 2007 Programming. Unfortunately, I'm doing this in Access 2003 :whistle: (no access to Access 2007 at work).

Most of the book seems to be generic, so I'm able to work through the examples without any issues... except for this one.

Below is the code for an API call that should return the Computer Name, and the format of the returned name is defined when the function is called, via the Enum.

I cannot get this to work at all. I even got the code from the site and C&Ped it, but still no dice. Could someone with Access 2007 test this for me and tell me if it works? Paste it into a new module and in the Immediate Window type: "?GetComputerNameHelper(" without the quotes, naturally. After you type the opening parantheses the options declared in the Enum should appear. Choose one, close the parantheses and hit enter- it should return your computer name.

If it doesn't work then it means there's something wrong with the example.

Option Compare Database
Option Explicit
Public Declare Function GetComputerNameEx Lib "kernel32.dll" Alias "GetComputerNameExA" ( _
ByVal NameType As COMPUTER_NAME_FORMAT, _
ByVal lpBuffer As String, _
ByRef lpnSize As Long) As Long

Public Enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
ComputerNameDnsDomain
ComputerNameDnsDillyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDnsDomain
ComputerNamePhysicalDnsFullyQualified
ComputerNameMax
End Enum
Private Function GetComputerNameHelper(NameType As COMPUTER_NAME_FORMAT) As String
Dim buffer As String 'space to receive data
Dim rc As Long 'return code
Dim pSize As Long 'pointer to a long- number of characters returned by function
'allocate some space
buffer = Space(255)
pSize = Len(buffer)
'return data
If (GetComputerNameEx(NameType, buffer, pSize)) Then
GetComputerNameHelper = Trim(Left(buffer, pSize))
End If
End Function

Tommy
11-13-2008, 05:26 AM
Randy FWIW the only one I didn't get a return on was ComputerNameMax. Everything else I got something from.

CreganTur
11-13-2008, 06:16 AM
Randy FWIW the only one I didn't get a return on was ComputerNameMax. Everything else I got something from.

Hmmm... must be a 2003 issue then.:think:

Thanks for checking:thumb

Bob Phillips
11-13-2008, 06:51 AM
I get thgem all in Access 2003 except the two domain values (not surprisingly), and ComputerNameMax.

Tommy
11-13-2008, 07:01 AM
I'm using Word 2003. API calls should have no difference in versions of Office. It may have different effects on different versions of the OS.
This is the test sub I set up :

Sub tester()
Debug.Print GetComputerNameHelper(ComputerNameNetBIOS)
Debug.Print GetComputerNameHelper(ComputerNameDnsHostname)
Debug.Print GetComputerNameHelper(ComputerNameDnsDomain)
Debug.Print GetComputerNameHelper(ComputerNameDnsDillyQualified)
Debug.Print GetComputerNameHelper(ComputerNamePhysicalNetBIOS)
Debug.Print GetComputerNameHelper(ComputerNamePhysicalDnsHostname)
Debug.Print GetComputerNameHelper(ComputerNamePhysicalDnsDomain)
Debug.Print GetComputerNameHelper(ComputerNamePhysicalDnsFullyQualified)
Debug.Print GetComputerNameHelper(ComputerNameMax)
End Sub


According to ;
http://msdn.microsoft.com/en-us/library/ms724224(VS.85).aspx
ComputerNameMax is not used

CreganTur
11-13-2008, 07:19 AM
Okay, then something very strange is going on. I've got MZTools, and when I use the Review Source Code tool it tells me that "The procedure 'GetComputerNameHelper' is not used"

When I try to call this function from the Immediate Window I get a "Function or Sub not defined" error.

I just tried Tommy's tester Sub and it actually worked! It seems strange to me that it fails when I try to call it from the Immediate Window... but oh well, at least it works now.

Thanks!

Tommy
11-13-2008, 08:51 AM
Okay, then something very strange is going on. I've got MZTools, and when I use the Review Source Code tool it tells me that "The procedure 'GetComputerNameHelper' is not used"

When I try to call this function from the Immediate Window I get a "Function or Sub not defined" error.

I just tried Tommy's tester Sub and it actually worked! It seems strange to me that it fails when I try to call it from the Immediate Window... but oh well, at least it works now.

Thanks!
MZTools would say that because you are not calling the function. You are calling (trying to) from the immediate window not in the code.

Function or Sub not found is because it is private. It has to be Public or not defined as private to call from the immediate window.

My tester sub worked because computers are afraid of me.:devil2:

Bob Phillips
11-13-2008, 11:34 AM
I actually built a function like Tommy's, so that is why it worked for me.

CreganTur
11-13-2008, 12:00 PM
I guess I just never realized that making a function private could make it private even to the Immediate Window! Oh well, you learn every day :thumb

SydneyGeek
11-13-2008, 08:51 PM
You could also use Environ("computername") instead of the API call.

In 2003, the Environ function was sandboxed but you can still call it from VBA. I use Environ("username") and several other Environ calls in many of the databases that I build.

Denis