?
Solved

Conversion from VBA to VB .NET

Posted on 2010-01-11
9
Medium Priority
?
530 Views
Last Modified: 2013-11-26
Ive got the attached Code which i have converted from Excel VBA
it worked perfectly fine in VBA,
i know that it would need to be converted in some parts. and i have done that.
but it Crashes on in the Function at "GetVersionExA(osinfo)"

Why?

Not sure if its any help but:
System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=WindowsApplication1
  StackTrace:
       at WindowsApplication1.Module1.GetVersionExA(OSVERSIONINFO lpVersionInformation)
       at WindowsApplication1.Module1.SSPValidateUser(String User, String Domain, String Password) in c:\documents and settings\nathanb\my documents\visual studio 2010\Projects\WindowsApplication1\WindowsApplication1\Module1.vb:line 524
       at WindowsApplication1.LoginForm1.Button1_Click(Object sender, EventArgs e) in c:\documents and settings\nathanb\my documents\visual studio 2010\Projects\WindowsApplication1\WindowsApplication1\LoginForm1.vb:line 20
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.ButtonBase.OnKeyUp(KeyEventArgs kevent)
       at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
       at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
       at System.Windows.Forms.Control.WmKeyChar(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Module Module1
    Private Const HEAP_ZERO_MEMORY = &H8

    Private Const SEC_WINNT_AUTH_IDENTITY_ANSI = &H1

    Private Const SECBUFFER_TOKEN = &H2

    Private Const SECURITY_NATIVE_DREP = &H10

    Private Const SECPKG_CRED_INBOUND = &H1
    Private Const SECPKG_CRED_OUTBOUND = &H2

    Private Const SEC_I_CONTINUE_NEEDED = &H90312
    Private Const SEC_I_COMPLETE_NEEDED = &H90313
    Private Const SEC_I_COMPLETE_AND_CONTINUE = &H90314

    Private Const VER_PLATFORM_WIN32_NT = &H2

    Structure SecPkgInfo
        Public fCapabilities As Long
        Public wVersion As Integer
        Public wRPCID As Integer
        Public cbMaxToken As Long
        Public Name As Long
        Public Comment As Long
    End Structure

    Structure SecHandle
        Public dwLower As Long
        Public dwUpper As Long
    End Structure

    Structure AUTH_SEQ
        Public fInitialized As Boolean
        Public fHaveCredHandle As Boolean
        Public fHaveCtxtHandle As Boolean
        Public hcred As SecHandle
        Public hctxt As SecHandle
    End Structure

    Structure SEC_WINNT_AUTH_IDENTITY
        Public User As String
        Public UserLength As Long
        Public Domain As String
        Public DomainLength As Long
        Public Password As String
        Public PasswordLength As Long
        Public Flags As Long
    End Structure

    Structure TimeStamp
        Public LowPart As Long
        Public HighPart As Long
    End Structure

    Structure SecBuffer
        Public cbBuffer As Long
        Public BufferType As Long
        Public pvBuffer As Long
    End Structure

    Structure SecBufferDesc
        Public ulVersion As Long
        Public cBuffers As Long
        Public pBuffers As Long
    End Structure

    Public Structure OSVERSIONINFO
        Public dwOSVersionInfoSize As Long
        Public dwMajorVersion As Long
        Public dwMinorVersion As Long
        Public dwBuildNumber As Long
        Public dwPlatformId As Long
        Public szCSDVersion As String
    End Structure

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
          (ByVal Destination, ByVal Source, ByVal Length)

    Private Declare Function NT4QuerySecurityPackageInfo Lib "security" _
          Alias "QuerySecurityPackageInfoA" (ByVal PackageName As String, _
          ByRef pPackageInfo As Long) As Long

    Private Declare Function QuerySecurityPackageInfo Lib "secur32" _
          Alias "QuerySecurityPackageInfoA" (ByVal PackageName As String, _
          ByRef pPackageInfo As Long) As Long

    Private Declare Function NT4FreeContextBuffer Lib "security" _
          Alias "FreeContextBuffer" (ByVal pvContextBuffer As Long) As Long

    Private Declare Function FreeContextBuffer Lib "secur32" _
          (ByVal pvContextBuffer As Long) As Long

    Private Declare Function NT4InitializeSecurityContext Lib "security" _
          Alias "InitializeSecurityContextA" _
          (ByRef phCredential As SecHandle, ByRef phContext As SecHandle, _
          ByVal pszTargetName As Long, ByVal fContextReq As Long, _
          ByVal Reserved1 As Long, ByVal TargetDataRep As Long, _
          ByRef pInput As SecBufferDesc, ByVal Reserved2 As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function InitializeSecurityContext Lib "secur32" _
          Alias "InitializeSecurityContextA" _
          (ByRef phCredential As SecHandle, ByRef phContext As SecHandle, _
          ByVal pszTargetName As Long, ByVal fContextReq As Long, _
          ByVal Reserved1 As Long, ByVal TargetDataRep As Long, _
          ByRef pInput As SecBufferDesc, ByVal Reserved2 As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function NT4InitializeSecurityContext2 Lib "security" _
          Alias "InitializeSecurityContextA" _
          (ByRef phCredential As SecHandle, ByVal phContext As Long, _
          ByVal pszTargetName As Long, ByVal fContextReq As Long, _
          ByVal Reserved1 As Long, ByVal TargetDataRep As Long, _
          ByVal pInput As Long, ByVal Reserved2 As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function InitializeSecurityContext2 Lib "secur32" _
          Alias "InitializeSecurityContextA" _
          (ByRef phCredential As SecHandle, ByVal phContext As Long, _
          ByVal pszTargetName As Long, ByVal fContextReq As Long, _
          ByVal Reserved1 As Long, ByVal TargetDataRep As Long, _
          ByVal pInput As Long, ByVal Reserved2 As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function NT4AcquireCredentialsHandle Lib "security" _
          Alias "AcquireCredentialsHandleA" (ByVal pszPrincipal As Long, _
          ByVal pszPackage As String, ByVal fCredentialUse As Long, _
          ByVal pvLogonId As Long, _
          ByRef pAuthData As SEC_WINNT_AUTH_IDENTITY, _
          ByVal pGetKeyFn As Long, ByVal pvGetKeyArgument As Long, _
          ByRef phCredential As SecHandle, ByRef ptsExpiry As TimeStamp) _
          As Long

    Private Declare Function AcquireCredentialsHandle Lib "secur32" _
          Alias "AcquireCredentialsHandleA" (ByVal pszPrincipal As Long, _
          ByVal pszPackage As String, ByVal fCredentialUse As Long, _
          ByVal pvLogonId As Long, _
          ByRef pAuthData As SEC_WINNT_AUTH_IDENTITY, _
          ByVal pGetKeyFn As Long, ByVal pvGetKeyArgument As Long, _
          ByRef phCredential As SecHandle, ByRef ptsExpiry As TimeStamp) _
          As Long

    Private Declare Function NT4AcquireCredentialsHandle2 Lib "security" _
          Alias "AcquireCredentialsHandleA" (ByVal pszPrincipal As Long, _
          ByVal pszPackage As String, ByVal fCredentialUse As Long, _
          ByVal pvLogonId As Long, ByVal pAuthData As Long, _
          ByVal pGetKeyFn As Long, ByVal pvGetKeyArgument As Long, _
          ByRef phCredential As SecHandle, ByRef ptsExpiry As TimeStamp) _
          As Long

    Private Declare Function AcquireCredentialsHandle2 Lib "secur32" _
          Alias "AcquireCredentialsHandleA" (ByVal pszPrincipal As Long, _
          ByVal pszPackage As String, ByVal fCredentialUse As Long, _
          ByVal pvLogonId As Long, ByVal pAuthData As Long, _
          ByVal pGetKeyFn As Long, ByVal pvGetKeyArgument As Long, _
          ByRef phCredential As SecHandle, ByRef ptsExpiry As TimeStamp) _
          As Long

    Private Declare Function NT4AcceptSecurityContext Lib "security" _
          Alias "AcceptSecurityContext" (ByRef phCredential As SecHandle, _
          ByRef phContext As SecHandle, ByRef pInput As SecBufferDesc, _
          ByVal fContextReq As Long, ByVal TargetDataRep As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function AcceptSecurityContext Lib "secur32" _
          (ByRef phCredential As SecHandle, _
          ByRef phContext As SecHandle, ByRef pInput As SecBufferDesc, _
          ByVal fContextReq As Long, ByVal TargetDataRep As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function NT4AcceptSecurityContext2 Lib "security" _
          Alias "AcceptSecurityContext" (ByRef phCredential As SecHandle, _
          ByVal phContext As Long, ByRef pInput As SecBufferDesc, _
          ByVal fContextReq As Long, ByVal TargetDataRep As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function AcceptSecurityContext2 Lib "secur32" _
          Alias "AcceptSecurityContext" (ByRef phCredential As SecHandle, _
          ByVal phContext As Long, ByRef pInput As SecBufferDesc, _
          ByVal fContextReq As Long, ByVal TargetDataRep As Long, _
          ByRef phNewContext As SecHandle, ByRef pOutput As SecBufferDesc, _
          ByRef pfContextAttr As Long, ByRef ptsExpiry As TimeStamp) As Long

    Private Declare Function NT4CompleteAuthToken Lib "security" _
          Alias "CompleteAuthToken" (ByRef phContext As SecHandle, _
          ByRef pToken As SecBufferDesc) As Long

    Private Declare Function CompleteAuthToken Lib "secur32" _
          (ByRef phContext As SecHandle, _
          ByRef pToken As SecBufferDesc) As Long

    Private Declare Function NT4DeleteSecurityContext Lib "security" _
          Alias "DeleteSecurityContext" (ByRef phContext As SecHandle) _
          As Long

    Private Declare Function DeleteSecurityContext Lib "secur32" _
          (ByRef phContext As SecHandle) _
          As Long

    Private Declare Function NT4FreeCredentialsHandle Lib "security" _
          Alias "FreeCredentialsHandle" (ByRef phContext As SecHandle) _
          As Long

    Private Declare Function FreeCredentialsHandle Lib "secur32" _
          (ByRef phContext As SecHandle) _
          As Long

    Private Declare Function GetProcessHeap Lib "kernel32" () As Long

    Private Declare Function HeapAlloc Lib "kernel32" _
          (ByVal hHeap As Long, ByVal dwFlags As Long, _
          ByVal dwBytes As Long) As Long

    Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, _
          ByVal dwFlags As Long, ByVal lpMem As Long) As Long

    Private Declare Function GetVersionExA Lib "kernel32" _
       (ByVal lpVersionInformation As OSVERSIONINFO) As Integer

    Dim g_NT4 As Boolean

    Private Function GenClientContext(ByRef AuthSeq As AUTH_SEQ, _
          ByRef AuthIdentity As SEC_WINNT_AUTH_IDENTITY, _
          ByVal pIn As Long, ByVal cbIn As Long, _
          ByVal pOut As Long, ByRef cbOut As Long, _
          ByRef fDone As Boolean) As Boolean

        Dim ss As Long
        Dim tsExpiry As TimeStamp
        Dim sbdOut As SecBufferDesc
        Dim sbOut As SecBuffer
        Dim sbdIn As SecBufferDesc
        Dim sbIn As SecBuffer
        Dim fContextAttr As Long

        GenClientContext = False

        If Not AuthSeq.fInitialized Then

            If g_NT4 Then
                ss = NT4AcquireCredentialsHandle(0&, "NTLM", _
                      SECPKG_CRED_OUTBOUND, 0&, AuthIdentity, 0&, 0&, _
                      AuthSeq.hcred, tsExpiry)
            Else
                ss = AcquireCredentialsHandle(0&, "NTLM", _
                      SECPKG_CRED_OUTBOUND, 0&, AuthIdentity, 0&, 0&, _
                      AuthSeq.hcred, tsExpiry)
            End If

            If ss < 0 Then
                Exit Function
            End If

            AuthSeq.fHaveCredHandle = True

        End If

        ' Prepare output buffer
        sbdOut.ulVersion = 0
        sbdOut.cBuffers = 1
        sbdOut.pBuffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
              Len(sbOut))

        sbOut.cbBuffer = cbOut
        sbOut.BufferType = SECBUFFER_TOKEN
        sbOut.pvBuffer = pOut

        CopyMemory(sbdOut.pBuffers, sbOut, Len(sbOut))

        ' Prepare input buffer
        If AuthSeq.fInitialized Then

            sbdIn.ulVersion = 0
            sbdIn.cBuffers = 1
            sbdIn.pBuffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
                  Len(sbIn))

            sbIn.cbBuffer = cbIn
            sbIn.BufferType = SECBUFFER_TOKEN
            sbIn.pvBuffer = pIn

            CopyMemory(sbdIn.pBuffers, sbIn, Len(sbIn))

        End If

        If AuthSeq.fInitialized Then

            If g_NT4 Then
                ss = NT4InitializeSecurityContext(AuthSeq.hcred, _
                      AuthSeq.hctxt, 0&, 0, 0, SECURITY_NATIVE_DREP, sbdIn, _
                      0, AuthSeq.hctxt, sbdOut, fContextAttr, tsExpiry)
            Else
                ss = InitializeSecurityContext(AuthSeq.hcred, _
                      AuthSeq.hctxt, 0&, 0, 0, SECURITY_NATIVE_DREP, sbdIn, _
                      0, AuthSeq.hctxt, sbdOut, fContextAttr, tsExpiry)
            End If

        Else

            If g_NT4 Then
                ss = NT4InitializeSecurityContext2(AuthSeq.hcred, 0&, 0&, _
                      0, 0, SECURITY_NATIVE_DREP, 0&, 0, AuthSeq.hctxt, _
                      sbdOut, fContextAttr, tsExpiry)
            Else
                ss = InitializeSecurityContext2(AuthSeq.hcred, 0&, 0&, _
                      0, 0, SECURITY_NATIVE_DREP, 0&, 0, AuthSeq.hctxt, _
                      sbdOut, fContextAttr, tsExpiry)
            End If

        End If

        If ss < 0 Then
            GoTo FreeResourcesAndExit
        End If

        AuthSeq.fHaveCtxtHandle = True

        ' If necessary, complete token
        If ss = SEC_I_COMPLETE_NEEDED _
              Or ss = SEC_I_COMPLETE_AND_CONTINUE Then

            If g_NT4 Then
                ss = NT4CompleteAuthToken(AuthSeq.hctxt, sbdOut)
            Else
                ss = CompleteAuthToken(AuthSeq.hctxt, sbdOut)
            End If

            If ss < 0 Then
                GoTo FreeResourcesAndExit
            End If

        End If

        CopyMemory(sbOut, sbdOut.pBuffers, Len(sbOut))
        cbOut = sbOut.cbBuffer

        If Not AuthSeq.fInitialized Then
            AuthSeq.fInitialized = True
        End If

        fDone = Not (ss = SEC_I_CONTINUE_NEEDED _
              Or ss = SEC_I_COMPLETE_AND_CONTINUE)

        GenClientContext = True

FreeResourcesAndExit:

        If sbdOut.pBuffers <> 0 Then
            HeapFree(GetProcessHeap(), 0, sbdOut.pBuffers)
        End If

        If sbdIn.pBuffers <> 0 Then
            HeapFree(GetProcessHeap(), 0, sbdIn.pBuffers)
        End If

    End Function


    Private Function GenServerContext(ByRef AuthSeq As AUTH_SEQ, _
          ByVal pIn As Long, ByVal cbIn As Long, _
          ByVal pOut As Long, ByRef cbOut As Long, _
          ByRef fDone As Boolean) As Boolean

        Dim ss As Long
        Dim tsExpiry As TimeStamp
        Dim sbdOut As SecBufferDesc
        Dim sbOut As SecBuffer
        Dim sbdIn As SecBufferDesc
        Dim sbIn As SecBuffer
        Dim fContextAttr As Long

        GenServerContext = False

        If Not AuthSeq.fInitialized Then

            If g_NT4 Then
                ss = NT4AcquireCredentialsHandle2(0&, "NTLM", _
                      SECPKG_CRED_INBOUND, 0&, 0&, 0&, 0&, AuthSeq.hcred, _
                      tsExpiry)
            Else
                ss = AcquireCredentialsHandle2(0&, "NTLM", _
                      SECPKG_CRED_INBOUND, 0&, 0&, 0&, 0&, AuthSeq.hcred, _
                      tsExpiry)
            End If

            If ss < 0 Then
                Exit Function
            End If

            AuthSeq.fHaveCredHandle = True

        End If

        ' Prepare output buffer
        sbdOut.ulVersion = 0
        sbdOut.cBuffers = 1
        sbdOut.pBuffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
              Len(sbOut))

        sbOut.cbBuffer = cbOut
        sbOut.BufferType = SECBUFFER_TOKEN
        sbOut.pvBuffer = pOut

        CopyMemory(sbdOut.pBuffers, sbOut, Len(sbOut))

        ' Prepare input buffer
        sbdIn.ulVersion = 0
        sbdIn.cBuffers = 1
        sbdIn.pBuffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
              Len(sbIn))

        sbIn.cbBuffer = cbIn
        sbIn.BufferType = SECBUFFER_TOKEN
        sbIn.pvBuffer = pIn

        CopyMemory(sbdIn.pBuffers, sbIn, Len(sbIn))

        If AuthSeq.fInitialized Then

            If g_NT4 Then
                ss = NT4AcceptSecurityContext(AuthSeq.hcred, AuthSeq.hctxt, _
                      sbdIn, 0, SECURITY_NATIVE_DREP, AuthSeq.hctxt, sbdOut, _
                      fContextAttr, tsExpiry)
            Else
                ss = AcceptSecurityContext(AuthSeq.hcred, AuthSeq.hctxt, _
                      sbdIn, 0, SECURITY_NATIVE_DREP, AuthSeq.hctxt, sbdOut, _
                      fContextAttr, tsExpiry)
            End If

        Else

            If g_NT4 Then
                ss = NT4AcceptSecurityContext2(AuthSeq.hcred, 0&, sbdIn, 0, _
                      SECURITY_NATIVE_DREP, AuthSeq.hctxt, sbdOut, _
                      fContextAttr, tsExpiry)
            Else
                ss = AcceptSecurityContext2(AuthSeq.hcred, 0&, sbdIn, 0, _
                      SECURITY_NATIVE_DREP, AuthSeq.hctxt, sbdOut, _
                      fContextAttr, tsExpiry)
            End If

        End If

        If ss < 0 Then
            GoTo FreeResourcesAndExit
        End If

        AuthSeq.fHaveCtxtHandle = True

        ' If necessary, complete token
        If ss = SEC_I_COMPLETE_NEEDED _
              Or ss = SEC_I_COMPLETE_AND_CONTINUE Then

            If g_NT4 Then
                ss = NT4CompleteAuthToken(AuthSeq.hctxt, sbdOut)
            Else
                ss = CompleteAuthToken(AuthSeq.hctxt, sbdOut)
            End If

            If ss < 0 Then
                GoTo FreeResourcesAndExit
            End If

        End If

        CopyMemory(sbOut, sbdOut.pBuffers, Len(sbOut))
        cbOut = sbOut.cbBuffer

        If Not AuthSeq.fInitialized Then
            AuthSeq.fInitialized = True
        End If

        fDone = Not (ss = SEC_I_CONTINUE_NEEDED _
              Or ss = SEC_I_COMPLETE_AND_CONTINUE)

        GenServerContext = True

FreeResourcesAndExit:

        If sbdOut.pBuffers <> 0 Then
            HeapFree(GetProcessHeap(), 0, sbdOut.pBuffers)
        End If

        If sbdIn.pBuffers <> 0 Then
            HeapFree(GetProcessHeap(), 0, sbdIn.pBuffers)
        End If

    End Function


    Public Function SSPValidateUser(ByVal User As String, ByVal Domain As String, _
          ByVal Password As String) As Boolean

        Dim pSPI As Long
        Dim SPI As SecPkgInfo
        Dim cbMaxToken As Long

        Dim pClientBuf As Long
        Dim pServerBuf As Long

        Dim ai As SEC_WINNT_AUTH_IDENTITY

        Dim asClient As AUTH_SEQ
        Dim asServer As AUTH_SEQ
        Dim cbIn As Long
        Dim cbOut As Long
        Dim fDone As Boolean

        Dim osinfo As OSVERSIONINFO

        SSPValidateUser = False

        ' Determine if system is Windows NT (version 4.0 or earlier)
        osinfo.dwOSVersionInfoSize = Len(osinfo)
        osinfo.szCSDVersion = Space$(128)
        GetVersionExA(osinfo)
        g_NT4 = (osinfo.dwPlatformId = VER_PLATFORM_WIN32_NT And _
              osinfo.dwMajorVersion <= 4)

        ' Get max token size
        If g_NT4 Then
            NT4QuerySecurityPackageInfo("NTLM", pSPI)
        Else
            QuerySecurityPackageInfo("NTLM", pSPI)
        End If

        CopyMemory(SPI, pSPI, Len(SPI))
        cbMaxToken = SPI.cbMaxToken

        If g_NT4 Then
            NT4FreeContextBuffer(pSPI)
        Else
            FreeContextBuffer(pSPI)
        End If

        ' Allocate buffers for client and server messages
        pClientBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
              cbMaxToken)
        If pClientBuf = 0 Then
            GoTo FreeResourcesAndExit
        End If

        pServerBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _
              cbMaxToken)
        If pServerBuf = 0 Then
            GoTo FreeResourcesAndExit
        End If

        ' Initialize auth identity structure
        ai.Domain = Domain
        ai.DomainLength = Len(Domain)
        ai.User = User
        ai.UserLength = Len(User)
        ai.Password = Password
        ai.PasswordLength = Len(Password)
        ai.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI

        ' Prepare client message (negotiate) .
        cbOut = cbMaxToken
        If Not GenClientContext(asClient, ai, 0, 0, pClientBuf, cbOut, _
              fDone) Then
            GoTo FreeResourcesAndExit
        End If

        ' Prepare server message (challenge) .
        cbIn = cbOut
        cbOut = cbMaxToken
        If Not GenServerContext(asServer, pClientBuf, cbIn, pServerBuf, _
              cbOut, fDone) Then
            ' Most likely failure: AcceptServerContext fails with
            ' SEC_E_LOGON_DENIED in the case of bad szUser or szPassword.
            ' Unexpected Result: Logon will succeed if you pass in a bad
            ' szUser and the guest account is enabled in the specified domain.
            GoTo FreeResourcesAndExit
        End If

        ' Prepare client message (authenticate) .
        cbIn = cbOut
        cbOut = cbMaxToken
        If Not GenClientContext(asClient, ai, pServerBuf, cbIn, pClientBuf, _
              cbOut, fDone) Then
            GoTo FreeResourcesAndExit
        End If

        ' Prepare server message (authentication) .
        cbIn = cbOut
        cbOut = cbMaxToken
        If Not GenServerContext(asServer, pClientBuf, cbIn, pServerBuf, _
              cbOut, fDone) Then
            GoTo FreeResourcesAndExit
        End If

        SSPValidateUser = True

FreeResourcesAndExit:

        ' Clean up resources
        If asClient.fHaveCtxtHandle Then
            If g_NT4 Then
                NT4DeleteSecurityContext(asClient.hctxt)
            Else
                DeleteSecurityContext(asClient.hctxt)
            End If
        End If

        If asClient.fHaveCredHandle Then
            If g_NT4 Then
                NT4FreeCredentialsHandle(asClient.hcred)
            Else
                FreeCredentialsHandle(asClient.hcred)
            End If
        End If

        If asServer.fHaveCtxtHandle Then
            If g_NT4 Then
                NT4DeleteSecurityContext(asServer.hctxt)
            Else
                DeleteSecurityContext(asServer.hctxt)
            End If
        End If

        If asServer.fHaveCredHandle Then
            If g_NT4 Then
                NT4FreeCredentialsHandle(asServer.hcred)
            Else
                FreeCredentialsHandle(asServer.hcred)
            End If
        End If

        If pClientBuf <> 0 Then
            HeapFree(GetProcessHeap(), 0, pClientBuf)
        End If

        If pServerBuf <> 0 Then
            HeapFree(GetProcessHeap(), 0, pServerBuf)
        End If

    End Function
End Module

Open in new window

0
Comment
Question by:bromy2004
[X]
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
9 Comments
 
LVL 12

Expert Comment

by:ErezMor
ID: 26290874
one of the awsome things in .net is you have almost everything you need inside the framework and you dont need api calls anymore (which vb and vba needed desperately). though i didnt read trhough all your api calls, i'll bet most if not all of them can be replaced with .net framework's objects

but that's the downside in your case, since you're using so many, it'll take you quite a while to find all the replacements. still, if you decided you want to migrate, i think it will be worthwhile to do it
another approach to it might be moving the code into vb6 and creating an activex dll from it, then call it from .net

either way you have plenty of work ahead...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26293213
If the above code is the converted code then all the data types are wrong.  =\

Change all LONG to INTEGER and try again...
0
 
LVL 27

Expert Comment

by:planocz
ID: 26300036
What is the VBA trying to do?
There may be a better way already made in .net framework
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 10

Author Comment

by:bromy2004
ID: 26300109
The entire process is a formula to check if a Username/Password/Domain combo is valid.
Public Function SSPValidateUser(ByVal User As String, ByVal Domain As String, ByVal Password As String) As Boolean

SSPValidateUser(MyUserName,MYDOMAIN,MyPassword)
i was trying to use it in a Log-in form, so we can use our Windows Logins, rather than remake new Usernames for the users.
0
 
LVL 12

Accepted Solution

by:
ErezMor earned 2000 total points
ID: 26301257
one question i monitored on the subject prduced this link that talks about windows users and authentication in .net:
http://geekswithblogs.net/Ramaraju/archive/2009/07/14/windows_authentication_in_winform_application.aspx
0
 
LVL 10

Author Comment

by:bromy2004
ID: 26301343
Thats in C#.
Ive never used that before.
I've only self-taught myself VBA and VB.NET
How would convert that?
0
 
LVL 12

Expert Comment

by:ErezMor
ID: 26301789
i'm c# iliterate too

google "c# to vb"
there are countless sites that allow pasting c# code and traslate it to vb.net for you
0
 
LVL 10

Author Closing Comment

by:bromy2004
ID: 31675954
It was the quickest, shortest solution to my problem.

Works perfectly.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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