Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I reference a dll in vbscript? 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I know you can't reference a dll in vbscript. But searching the internet I found references where you can write some code that includes the dll definition and then save the code as an ocx. (Or something like that.) Then I could reference the ocx via CreateObject. However, I've not found where anyone has done it.

Basically, I want to call the function Logonuser found in the dll advapi32.dll. I created a web page (via asp.net 2.0) that prompts the user for name, password, and domain. It then calls the function Logonuser to authenticate the user. This works fine except, asp is causing other problems. It would be much easier if I could just call LogonUser via a vbscript. Does anyone know how I might do this?

Here's the code I have for the asp.net 2.0 form.
Code:
 Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal api_strUsername As String, _
                                                             ByVal api_strDomain As String, _
                                                             ByVal api_strPassword As String, _
                                                             ByVal api_intLogonType As Integer, _
                                                             ByVal api_intLogonProvider As Integer, _
                                                             ByRef phToken As IntPtr) As Boolean

'****************************************************************************
'*                              ValidateLogon                               *  
'****************************************************************************

Private Function ValidateLogOn(ByVal strUsername As String, _
                               ByVal strPassword As String, _
                               ByVal strDomain As String) As Boolean

    Dim token As IntPtr         'Token returned by the API call

    intLoginCount += 1

    If LogonUser(strUsername, strDomain, strPassword, 3, 0, token) = True Then
        Return True
    Else
        Return False
        If (intLoginCount > 3) Then Call cmdCancel_Click()
    End If

End Function
 
1. Open up a new VB6 ActiveX Dll project
2. Paste that code above into your class module.
3. Change ValidateLogOn from Private to Public.
4. Rename the project and class module.
5. Compile into a DLL
6. Copy DLL to your web server and register it with regsvr32.exe
7. Create an instance of your new class in VBScript using syntax like this:
[tt]Set myObject = CreateObject("MyProjectName.MyClassName")[/tt]
8. Call it like this:
[tt]If MyObject.ValidateLogOn("blah", "blah", "blah") Then[/tt]
9. Don't forget to set it to Nothing when you are done.
 
Follow-up...

That's what I was looking for. However, I'm having problems registering it. I created a class via a vb project but it couldn't find the entry point LogonUser. Also, vb didn't like AUTO in "Declare Auto Function". So I created a class via Visual Studio 2005. But when I register it I get the error msg "...loaded. DllResisterServer entry point was not found." Currently, researching that problem. Will keep you posted...
 
Oh, sorry... I didn't notice the "Auto" keyword... VB6 doesn't support that... also not the Return keyword... also in VB6 an Integer is only 16bit

... the code above is all .Net stuff.

Oh, also you don't want that call to the click event if the logon fails... and the counter variable should be handled differently... probably counted elsewhere.


For VB6 you want something more like this:
Code:
Private Declare Function LogonUser Lib "Advapi32" Alias "LogonUserA" _
        (ByVal api_strUsername As String, _
         ByVal api_strDomain As String, _
         ByVal api_strPassword As String, _
         ByVal api_intLogonType As Long, _
         ByVal api_intLogonProvider As Long, _
         ByRef phToken As Long) As Long


Private Declare Function CloseHandle Lib "kernel32" _
        (ByVal hObject As Long) As Long

Const LOGON32_PROVIDER_DEFAULT = 0&
Const LOGON32_LOGON_NETWORK = 3&

'***********************************************************
'*                              ValidateLogon
'***********************************************************

Private Function ValidateLogOn(ByVal strUsername As String, _
                               ByVal strPassword As String, _
                               ByVal strDomain As String) As Boolean

  Dim token As Long  'Token returned by the API call
  Dim ret As Long

  ret = LogonUser(strUsername, _
                  strDomain, _
                  strPassword, _
                  LOGON32_LOGON_NETWORK, _
                  LOGON32_PROVIDER_DEFAULT, _
                  token)

    If CBool(ret) Then
      ValidateLogOn = True
      CloseHandle Token
    End If
End Function
 
That's it!!! I've been on this for 2 days. Thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top