Solved

DLL almost works

Posted on 2000-05-17
13
195 Views
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?

Thanks.

Ramon
0
Comment
Question by:CodeCommando
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2818991
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2819008
btw whats your code look like
0
 
LVL 2

Expert Comment

by:orbitaltech
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

 
0
 

Author Comment

by:CodeCommando
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"
    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
 

Author Comment

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

Any ideas?
0
 
LVL 28

Expert Comment

by:AzraSound
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





0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Expert Comment

by:ameba
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.
0
 
LVL 1

Expert Comment

by:lochiano
ID: 2819198
If you have not read this white paper you should try it

http://msdn.microsoft.com/library/periodic/period00/versioning.htm
0
 
LVL 15

Expert Comment

by:ameba
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
0
 

Author Comment

by:CodeCommando
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.

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2819278
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
 
LVL 15

Accepted Solution

by:
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.
0
 
LVL 15

Expert Comment

by:ameba
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now