DLL almost works

Posted on 2000-05-17
Last Modified: 2010-05-18
This question has two parts.
Part I.
I created a wrapper dll (calls the GetUserName) to shield my users from the complexities of api functions. With the help of ameba (if you're out there, here's some more points for you), I got it to work on a Win 95 machine. When I run it on an NT machine, it gives me the following error:

The instruction at "0x77dc3e5f" referenced memory at
"0x000000ff". The memory could not be "read".

Click OK to terminate application.

I seem to remember that NT handles text strings differently from Win 95. It's supposed to make text strings more versatile for international users (I forget what it's called). Anyway, I
suspect that could be the problem.

That finishes Part I.

Part II.
I'm interested in wrapping many api calls in a series of dlls. I seen references to already made wrapper classes for vb. Anyone know where I can find them?


Question by:CodeCommando
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
LVL 28

Expert Comment

ID: 2818991
there are several at planet source are a couple

"cOSNT_ChangePassword -A WinNT NETApi Class wrapper"

"cFindFile - A Windows API wrapper for FindNext"

LVL 28

Expert Comment

ID: 2819008
btw whats your code look like

Expert Comment

ID: 2819038
Just an off note:

referencing memory @ 0x000000ff

Have you stepped through your code to see exactly witch function is producing this? If you do/have then the syntax lies within that function

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Author Comment

ID: 2819058
Here is ameba's solution. I made that work. The only change to his original code was that a function was named MachineName and I renamed it GetMachineName to make it consistent with the other function.

Here is 'optimized' version of your class. Note that you don't need module for declarations.
' clsUtils class
Option Explicit
Private Declare Function GetComputerName Lib "kernel32" Alias _
    "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName& Lib "advapi32.dll" _
    Alias "GetUserNameA" (ByVal lpBuffer As String, ByVal nSize As Long)
'Get the local computer name
Public Function GetMachineName() As String
    Dim pos As Long
    On Error Resume Next
    GetMachineName = Space$(255) ' prepare buffer
    GetComputerName GetMachineName, 255 ' call windows API
    ' trim unneeded characters
    pos = InStr(GetMachineName, vbNullChar)
    GetMachineName = Left$(GetMachineName, pos - 1)
End Function

Public Function GetUser() As String
    Dim pos As Long
    On Error Resume Next
    GetUser = Space(255) ' prepare buffer
    GetUserName GetUser, 255 ' call windows API
    pos = InStr(GetUser, vbNullChar)
    If pos = 0 Then
        GetUser = "UNKNOWN"
        ' trim unneeded characters
        GetUser = Left$(GetUser, pos - 1)
    End If
End Function

' You can test your class with this form
' Form1 code
Option Explicit

Private Sub Form_Click()
    Dim Utils As clsUtils
    Set Utils = New clsUtils ' create instance
    MsgBox "Machine: " & Utils.GetMachineName & vbCrLf & _
        "User: " & Utils.GetUser
End Sub


Author Comment

ID: 2819078
Good call for orbitech. The problem only exists in the GetUser function. The GetMachineName is working fine.

Any ideas?
LVL 28

Expert Comment

ID: 2819080
is it just the getuser function?  try this one:

Public Function NTDomainUserName() As String
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String

    lngBufferLength = 255
    lngRet = GetUserName(strBuffer, lngBufferLength)
    strTemp = UCase(Trim$(strBuffer))
    NTDomainUserName = Left$(strTemp, Len(strTemp) - 1)

End Function

LVL 15

Expert Comment

ID: 2819162
Hi, again!

MS suggest creating setups for NT on NT machine, i.e. ideally you'll need one Vb machine per OS, but;
I don't think the API declarations or anything is different for *these* 2 functions on NT.
The only suggestion I have at a moment is:

Do a regular setup by using PDW, then INSTALL your package. Installation will copy and register everything your dll and exe needs.

Expert Comment

ID: 2819198
If you have not read this white paper you should try it
LVL 15

Expert Comment

ID: 2819207
Ok, 2 things you can try:

1. Pass nSize argument ByRef (your declaration is passing it ByVal)

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

2. Use GetUserNameW API instead of GetUserName

Public Declare Function GetUserNameW Lib "advapi32.dll" (lpBuffer As Byte, nSize As Long) As Long

Author Comment

ID: 2819258
Ok ameba, you got it working again. I have to go back later tonight and test again on my Win 95 machine to see if it will still work.

Will adding the "W" make it always compatible between 95 and NT? Or would it be safer to include an OS checker and then have different functions based on which OS is being used?

I get back later and tell you whether it works.

Azrasound gets partial credit of 50 points for finding the wrapper sites.

LVL 28

Expert Comment

ID: 2819278
two more classes to give you:

"Class Wrapper for the Common Dialog Functions"

"Wrapper class for mouse API calls"
LVL 15

Accepted Solution

ameba earned 300 total points
ID: 2819323
I'm glad it worked.

>Will adding the "W" make it always compatible between 95 and NT?

I would use the other, 'non-Wide' version, it should work OK on both OS-es.

But, everything should be tested, since OS changes too often, too fast, not enough beta testing by MS, many surprises ... e.g. passing variables that include Chr(0), between components on Windows 2000 might not work.
LVL 15

Expert Comment

ID: 2819677
Thanks for the points, CodeCommando

Good luck with your wrappers. I'll see if I can find something that Azrasound didn't mention.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 17 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question