Conversation started Mar 14, 2018 at 11:55.
Mar 14, 2018 11:55
I've been developing in 32-bit Office 2010 on 64-bit Win7 for a while, while the rest of the company defaults to 32-bit Win7. (Yes, we're behind the times, don't ask.)
Now, however, we're finally starting to upgrade to Win10 64-bit and Office 2016 64-bit, and these calls have failed.
I've added the
#If Win64 Then Private Declare PtrSafe...
to make these calls work in either environment. 0

I'm self taught and hoping that people with more experience and knowledge can look over the following code and provide me with some pointers to optimize and streamline the process. Things such as 'rather than rebuild the arrays why don't you create global arrays', or 'you're really not accomplish...
I'd had just the
#Else
portion in my mixed Win7/Offcie10 environment, now I've got the whole thing in my Win10/Office16 environment: #If Win64 Then Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal IpBuffer As String, nSize As Long) As Long Private Declare PtrSafe Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _ (ByVal lpBuffer As String, nSize As Long) As Long #Else Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal IpBuffer As String, nSize As Long) As Long
Mar 14, 2018 12:49
The return value should be
Boolean
. VB knows what to do with a BOOL
and you get to avoid the weird If Not 1 Then
bug
and I don't like using
Win64
. Why use the non-PtrSafe
version just because you're running 32-bit? Better to use VBA7
flag instead.
and it's not clear from the comments whether adding
PtrSafe
was all that was done and whether one literally use LongLong
. I really hope that wasn't the hope.
All variables that represents a pointer should be consistently converted to
LongPtr
; not to LongLong
; it should be only used if the API actually needs a 64-bit integer which is very rare.
Oh, FWIW.... there is a small Wiki that tries to enumerate common VBA7 declaration here: utteraccess.com/wiki/Category:API
Mar 14, 2018 13:29
@this - stupid question time... the utteraccess (I keep reading that as "udderaccess". Not sure why) page you linked to doesn't contain either of the functions I'm calling
GetUserName
or GetComputerName
(which should be in a kernel32
listing somewhere). Thoughts?
Also, for the
WNetGetUser
function, which seems to me to be the closest (utteraccess.com/wiki/WNetGetUser), it indicates "Pre-VBA7" is pre-Access2010. Since I'm coding in 2010 (Access & Excel) doesn't that mean I've been using VBA7 all along? Mar 14, 2018 13:44
It appears to me based on MS docs that the parameters should be long pointers, but I'm no c++ expert...
Mar 14, 2018 14:03
so i'm a bit limited on the time but generally.... VB does some magic WRT strings and
BOOL
. That is one of reason why you always use the ANSI version, not unicode, in the spite of the fact that VBA uses Unicode internally with its string and why you pass the string "ByVal" even though the MSDN doc will say they expect a pointer to the string.
Now, the confusing thing is that C++ people loves to redefine types. So you see all those stuff like
HWND
, LPDWORD
, INT
, whatever.
Conversation ended Mar 14, 2018 at 14:10.
Calling Win API functions in VBA
Mar '1814
VBA Rubberducking
This chat has moved to Discord: discord.gg/MYX9RECenJ
Participants
- this 50%
- FreeMan 39%
- Quack Exchange 3%
- SlowLearner 3%
- M.Doerner 3%
- Hosch250 1%
all times are UTC