DLL almost works

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?

Thanks.

Ramon
CodeCommandoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
amebaConnect With a Mentor Commented:
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.
0
 
AzraSoundCommented:
there are several at planet source code...here are a couple

"cOSNT_ChangePassword -A WinNT NETApi Class wrapper"

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=2885

"cFindFile - A Windows API wrapper for FindNext"

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=2886

0
 
AzraSoundCommented:
btw whats your code look like
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
orbitaltechCommented:
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

 
0
 
CodeCommandoAuthor Commented:
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"
    Else
        ' 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


0
 
CodeCommandoAuthor Commented:
Good call for orbitech. The problem only exists in the GetUser function. The GetMachineName is working fine.

Any ideas?
0
 
AzraSoundCommented:
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





0
 
amebaCommented:
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.
0
 
lochianoCommented:
If you have not read this white paper you should try it

http://msdn.microsoft.com/library/periodic/period00/versioning.htm
0
 
amebaCommented:
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
0
 
CodeCommandoAuthor Commented:
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.

0
 
AzraSoundCommented:
two more classes to give you:

"Class Wrapper for the Common Dialog Functions"

http://www.inil.com/users/browne/vb/code/clscomdlg.html

"Wrapper class for mouse API calls"

http://home.mweb.co.za/gs/gslennox/vb/database/vb103.html
0
 
amebaCommented:
Thanks for the points, CodeCommando

Good luck with your wrappers. I'll see if I can find something that Azrasound didn't mention.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.