473,513 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

api GetUserName not working properly - error: Bad DLL calling convention

Seth Schrock
2,965 Recognized Expert Specialist
I'm attempting to use an api to get the username of the currently logged in Windows user. I've basically copied and pasted the function from Function to return Username

Here is the code that I'm using:
Expand|Select|Wrap|Line Numbers
  1. Declare Function GetUserName Lib "advapi32.dll" Alias _
  2.                      "GetUserNameA" (ByVal lpBuffer As String, nSize As Long)
  3.  
  4. Public Function fGetUserName()
  5.  
  6. 'Get current Windows user name
  7. Dim strUser
  8. Dim s$, cnt&, dl&
  9. Dim max_String As Integer
  10.  
  11.     max_String = 30
  12.     cnt& = 199
  13.     s$ = String$(max_String, 1)
  14.     dl& = GetUserName(s$, cnt)
  15.     strUser = Trim$(Left$(s$, cnt))
  16.     strUser = UCase(Mid(strUser, 1, Len(strUser) - 1))
  17.  
  18.  
  19.  
  20. 'Get user ID
  21. Application.TempVars.Add "UserID", DLookup("EmployeeID", "tblEmployee", "Username = '" & strUser & "'")
  22. Debug.Print Application.TempVars("UserID").Value
  23.  
  24. 'Get user type
  25. Application.TempVars.Add "UserType", DLookup("EmployeeType", "tblEmployee", "Username = '" & strUser & "'")
  26. Debug.Print Application.TempVars("UserType").Value
  27.  
  28.  
  29. End Function
However, I'm getting an error message on line 14 that says
Run-time error '49':

Bad DLL calling convention


I looked it up in MSDN and found Bad DLL calling convention. I understand absolutely nothing in the page so I'm going to need help.
Jan 15 '13 #1
14 4154
NeoPa
32,557 Recognized Expert Moderator MVP
It's because Mary's shockingly sloppy :-D

Add the & to cnt to refer to the actual variable cnt&.
Jan 15 '13 #2
Seth Schrock
2,965 Recognized Expert Specialist
Same error. Here is my line 14 now
Expand|Select|Wrap|Line Numbers
  1.  dl& = GetUserName(s$, cnt&)
I also added the & to the cnt in line 15. Did I need to?
Jan 15 '13 #3
NeoPa
32,557 Recognized Expert Moderator MVP
I'm not sure exactly what the problems are Seth, but here's some code to try that I know works for me :
Expand|Select|Wrap|Line Numbers
  1. Private Declare Function GetUserName Lib "advapi32.dll" _
  2.     Alias "GetUserNameA" (ByVal lpBuffer As String, _
  3.                           lpnSize As Long) As Long
  4.  
  5. 'GetLogonName() determines the logon ID of the current user.
  6. Public Function GetLogonName() As String
  7.     Dim lngMax As Long
  8.     Dim strBuffer As String
  9.  
  10.     lngMax = &HFF
  11.     strBuffer = String(lngMax, vbNullChar)
  12.     Call GetUserName(lpBuffer:=strBuffer, lpnSize:=lngMax)
  13.     GetLogonName = Trim(Left(strBuffer, lngMax - 1))
  14. End Function
Seth:
I also added the & to the cnt in line 15. Did I need to?
Yes. That would have been necessary in the previous code, I believe.
Jan 15 '13 #4
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
You could try this instead:
Expand|Select|Wrap|Line Numbers
  1. Public Function GetLoginName() as string
  2.   GetLoginName= CreateObject("wscript.network").UserName
  3. End Function
Jan 15 '13 #5
NeoPa
32,557 Recognized Expert Moderator MVP
I may be wrong for current versions of the OS Smiley, but I don't believe the WScript engine (or the CScript one either - assuming they are separate) is loaded by default. If that is still the case then loading it will cause some extra overhead. I'm certain it would work, but is not so light footprinted as one might imagine from the tidiness of the code.

On the other hand, it is much smaller as far as the code goes, so may appeal to many for that reason.
Jan 15 '13 #6
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Valid point, but then again, what does the API call of the previous mentioned method involve?

Some timing scores for the function I posted when used from Access 2010 and Windows 7:
[1] iterations performed in:0ms
[100] iterations performed in:31ms
[1000] iterations performed in:343ms

Some timing scores for the function I copied from post #4:
[1] iterations performed in:0ms
[100] iterations performed in:16ms
[1000] iterations performed in:62ms



I would argue that in the end it makes little difference, as its not a function you will call hundreds of times (at a time) anyway.
Jan 15 '13 #7
NeoPa
32,557 Recognized Expert Moderator MVP
Smiley:
I would argue that in the end it makes little difference, as its not a function you will call hundreds of times (at a time) anyway.
I would suggest that indicates that the timing test was not an appropriate one. The API is calling code which is already loaded. The WScript option requires extra code to be loaded into RAM (and thus tie up that extra RAM). That load would never need to be repeated, so tests of running it multiple times would only tend to hide the 'cost' rather than illustrate it. Thus, it is a resources issue rather than a timing one I was referring to.

Personally, I believe that well mannered software should always aim to increase its footprint only when necessary, so until WScript is incorporated as standard into the OS I use it as little as I can (Which is a shame as I'm sure you appreciate that what it can offer is very extensive).

PS. You would probably find, if you were interested in looking into it that deeply, that the calls themselves would end up as performing very comparably and the extra overhead is simply due to the repeated interpreting of the lines of code within the function I posted in post #4. Again, not something that repetition throws any light on, but makes sense if you understood my earlier comment to be simply about performance (which as you rightly say is totally negligible).
Jan 15 '13 #8
Seth Schrock
2,965 Recognized Expert Specialist
Strangly enough, the error was in the GetUserName function. I replaced mine with yours and it worked perfectly. The only difference that I can see is that you have a private function and you have changed the variable name from nSize to lpnSize. I'm not sure if this actually matters, but that is all I see different.

As to which method to use, I'll play around with it and see. I do have the answer to my original question though.

Thanks NeoPa and Smiley for your assistance and advice.

By the way, why didn't the compiler find the variable with the missing &? I have Option Explicit set.
Jan 15 '13 #9
NeoPa
32,557 Recognized Expert Moderator MVP
Seth:
By the way, why didn't the compiler find the variable with the missing &? I have Option Explicit set.
Good question. I'm not sure is my answer. Maybe it recognises it with or without and it didn't need the & added after all. I never use such names as they have been de-recommended. They only still work to support old code.

They are an echo from very much older versions of BASIC where Dim etc didn't even exist. I don't believe that MS ever recommended they be used for serious work (Fine if you're knocking up a quick equation on a calculator or similar device of course).
Jan 15 '13 #10
zmbd
5,501 Recognized Expert Moderator Expert
I should point out... my IT has disabled WScript via GPO in all general user accounts. Only Admin and certain "poweruser" accounts have the rights to run scripting.
I've had to redo my "install" method and my front-end updater because of this "lockout" as both used script files.
Jan 21 '13 #11
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Ouch bummer. Certainly something to be aware of.
Can you use Shell commands? I use a Shell command to launch a database containing my update code, so its all native access except the shell command.

I don't know much about how WScript and GPO functions. Have you tried the code in post #5? Did it error? I would like to know as it could mean I ought to rethink some things.
Jan 22 '13 #12
zmbd
5,501 Recognized Expert Moderator Expert
I have one of those "special" accounts; thus, I didn't know anything was wrong until one of my error traps started sending me emails. I had altered the backend tables; thus, the frontend update... worked fine on my PC... not so well on the boss' PC. Simple point and click via IT fixed but involved a lot of paperwork.

Therfor, I'll not be able to test your code in my work setting; however, here's a link covering the basics: Security Settings for WSH Scripts from what IT has told me this link is still the root method, just with Windows7 a fancier interface.
Jan 22 '13 #13
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Did your method use wscript in a manor similar to mine, or was the script external to access? I.e. a script file being run from access?
Jan 22 '13 #14
NeoPa
32,557 Recognized Expert Moderator MVP
I'm pretty sure the code from post #5 would fail if WScript were blocked Smiley, as it invokes WScript within the CreateObject() call.
Jan 23 '13 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1981
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
3
2793
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in information in the database. When I step through the...
1
3294
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to hit Refresh for the images to load properly....
5
2777
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
2
3272
by: Annu | last post by:
Hi I need help on <enbed> tag. Following code(No 1) is working properly on windows but on linux code no.2 is not working Code No 1: <EMBED type='application/x-mplayer2' ...
12
4566
kamill
by: kamill | last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it redirected to index.php of admin section......BUT when i...
5
3604
by: damezumari | last post by:
When a user logs in to my site http://iwantyourquestion.com I set $_SESSION to true if his username and password are OK. When he calls a page I check if $_SESSION is true. If it not I ask him to...
3
1999
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
4
3743
by: zairali | last post by:
Hi, I am trying to fix a webpage ( http://www.d.umn.edu/itss/labs/maps/ ) which uses some html (or xml also?) to show pop ups when you rollover the numbers on a map of the building. They work fine...
8
3355
by: neovantage | last post by:
Hey all, Website page is not working properly in IE6 and IE7 but working normal in firefox. Can somebody tell me how i can fixed that problem. Here is the url of the page http://www.ragehockey.biz/...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.