API functions

A

Alex

The code with API functions below is working perfectly almost on all
computers but on a few ones is not working. It's from the code to get a full
user name.
What could be the cause of this?
Thanks
......
Private Declare Function apiNetGetDCName _
Lib "netapi32.dll" Alias "NetGetDCName" _
(ByVal servername As Long, _
ByVal DomainName As Long, _
bufptr As Long) As Long

' function frees the memory that the NetApiBufferAllocate
' function allocates.
Private Declare Function apiNetAPIBufferFree _
Lib "netapi32.dll" Alias "NetApiBufferFree" _
(ByVal buffer As Long) _
As Long

' Retrieves the length of the specified wide string.
Private Declare Function apilstrlenW _
Lib "kernel32" Alias "lstrlenW" _
(ByVal lpString As Long) _
As Long

Private Declare Function apiNetUserGetInfo _
Lib "netapi32.dll" Alias "NetUserGetInfo" _
(servername As Any, _
UserName As Any, _
ByVal level As Long, _
bufptr As Long) As Long

' moves memory either forward or backward, aligned or unaligned,
' in 4-byte blocks, followed by any remaining bytes
Private Declare Sub sapiCopyMem _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Private Declare Function apiGetUserName Lib _
"advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) _
As Long

Private Const MAXCOMMENTSZ = 256
Private Const NERR_SUCCESS = 0
Private Const ERROR_MORE_DATA = 234&
Private Const MAX_CHUNK = 25
Private Const ERROR_SUCCESS = 0&
.....
 
L

Leith Ross

Hello Alex,

You need to be more specific about what problems you are experiencing
and also include the operating systems that the code does and does not
run on. What API error you are encountering and which API call has
failed.

Sincerely,
Leith Ross
 
J

Jim Thomlinson

I assume there is some sort of a problem with

msgbox environ("UserName")

???
 
A

AnExpertNovice

The code only works on 32 bit machines. Ok, there are not many 16 bit
machines left and I don't know of any 64 bit, but still, it is a nit pick
that had to be plucked.

There will be no user name if there is no such environment variable. Check
in a DOS Command prompt by typing "Set Username". If the message says it is
not defined then it needs to be created. Of course, this is an opportunity
to fix the code so that it works when the Username variable doesn't exist.
 
A

AnExpertNovice

Also, "netapi32.dll" may not exist or it may not be available. I'm not sure
if it has to be registered or just in a specific folder or any folder within
the path statement. Start by making sure it is in the same folder as the
other computers

(Mine is in C:\Windows\System32)
 
A

Alex

Thanks for the responses.
Trying to asnwer your questions, Leith:
There is Win XP on all computers. The error message is the Run time error
whether it's excel or MS Access. If I'm taking off this module it's working.
As I said before it's not working only on the few computers.
There is some opinion that the netapi32.dll and advapi32.dll probably are
not registered there. But, I used search and found these dll files inside the
C:Windows\System32 folder. But, when I tried to register them I've got "the
file was loaded. DllRegisterServer entry wasn't found. File cannot be
registered."
Probably they're already registered there and I'm not so comfortable to
experiment further on the dll registration.
 
L

Leith Ross

Hello Alex,

My suspecion is this is a server problem. These API call require some
adjustment as to which server they are run on. There are differences in
Windows 2000, Windows 2003, and the NT 4.0 server. If your code hasn't
been designed to automatically adjust to the server being used, it will
require making adjustments to the code for the users having problems.
This can become a problem again if they port the code to another
machine using a different server. In the world of API, one call doesn't
do it all. You should talk with your network admin about possible
conflicts as well.

Sincerely,
Leith Ross
 
J

John.Greenan

Why not engineer this problem away?

In excel set a reference to the windows script host object model -
c:\winnt\system32\wshom.ocx.

Then this will get the user name

Sub dd()
Dim a As WshNetwork

MsgBox "username is " & a.UserName
End Sub


As a general rule, most things that you'd think require an API call have
been wrapped up in a nicer COM dll, it's just a case of finding it. Note the
"most"...

Good luck
 
A

Alex

On my machine I have these DLLs in the System32 and in the I386 folders but
on the not working machines there is no I386 folder.

However, the username can be the case as well. On the not working machine
the username exists but it's a group username without actual full name.

Thanks everybody. I'll research it further.
 
A

Alex

Thanks, John.
It looks good. Please, advise how to define this 'a' or set up reference.
Dim a As WshNetwork
 
A

Alex

John, I did it. It returns a login id but not a full user name.
Set WshNetwork = CreateObject("WScript.Network")
MsgBox "Domain = " & WshNetwork.UserDomain
MsgBox "Computer Name = " & WshNetwork.ComputerName
MsgBox "User Name = " & WshNetwork.UserName

It looks like identical to the strUser = Environ$("username")
The API functions that I'm using are able to return a full user name not
just a login.

Please, correct if I'm wrong.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top