Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array
Null reference exception on a byte array Null reference exception on a byte array
Null reference exception on a byte array
Go Back  Xtreme Visual Basic Talk > > > Null reference exception on a byte array


Reply
 
Thread Tools Display Modes
  #1  
Old 12-28-2011, 05:34 AM
luxus luxus is offline
Newcomer
 
Join Date: Dec 2011
Posts: 5
Default Null reference exception on a byte array


I've converted an application for connecting to a vpn server and starting a remote desktop connection from vb6 to vb.net.

But now when I'm trying to run the program I get an ArgumentNullException

I've tried searching for a solution but I've been unable to find one.

Below is my code:

VB6 (runs fine)
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, ByVal pSrc As String, ByVal ByteLen As Long)
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Dim resp As Long
Const RAS95_MaxEntryName = 256
Const RAS_MaxPhoneNumber = 128
Const RAS_MaxCallbackNumber = RAS_MaxPhoneNumber

Const UNLEN = 256
Const PWLEN = 256
Const DNLEN = 12

Private Type RASDIALPARAMS
  dwSize As Long ' 1052
  szEntryName(RAS95_MaxEntryName) As Byte
  szPhoneNumber(RAS_MaxPhoneNumber) As Byte
  szCallbackNumber(RAS_MaxCallbackNumber) As Byte
  szUserName(UNLEN) As Byte
  szPassword(PWLEN) As Byte
  szDomain(DNLEN) As Byte
End Type

Function Dial(ByVal Connection As String, ByVal domain As String, ByVal Username As String, ByVal Password As String) As Boolean
  Dim rp As RASDIALPARAMS, h As Long, resp As Long
  Dim WshShell, objEnv
  Dim strDirectory, strPBKFile
  Dim test

  Set WshShell = CreateObject("WScript.Shell")
  Set objEnv = WshShell.Environment("Process")
  
  strPBKFile = objEnv("TMP") & "\rasphone.pbk"

  rp.dwSize = Len(rp) + 6
  
  ChangeBytes Connection, rp.szEntryName
  ChangeBytes "", rp.szPhoneNumber 'Phone number stored for the connection
  ChangeBytes "*", rp.szCallbackNumber 'Callback number stored for the connection
  ChangeBytes Username, rp.szUserName
  ChangeBytes Password, rp.szPassword
  ChangeBytes "*", rp.szDomain 'Domain stored for the connection
  
  resp = RasDial(ByVal 0, strPBKFile, rp, 0, ByVal 0, h) 'AddressOf RasDialFunc
  Connectie = h
  Dial = (resp = 0)
  If resp > 0 Then
    GetError (resp)
    Form1.Command1.Caption = "Return"
  Else
    Form1.Command1.Caption = "Terminalserver"
    Form1.Label5.Caption = "De Server is succesvol verbonden." & vbNewLine & vbNewLine & "Druk op terminalserver op de RDP verbinding nogmaals te starten," _
                  & "of druk op exit om de verbinding te verbreken"
  End If

End Function

Function ChangeBytes(ByVal str As String, Bytes() As Byte) As Boolean
  'Changes a Visual Basic unicode string to an byte array
  'Returns True if it truncates str
  Dim lenBs As Long 'length of the byte array
  Dim lenStr As Long 'length of the string
  lenBs = UBound(Bytes) - LBound(Bytes)
  lenStr = LenB(StrConv(str, vbFromUnicode))

  If lenBs > lenStr Then
    CopyMemory Bytes(0), str, lenStr
    ZeroMemory Bytes(lenStr), lenBs - lenStr
  ElseIf lenBs = lenStr Then
    CopyMemory Bytes(0), str, lenStr
  Else
    CopyMemory Bytes(0), str, lenBs 'Queda truncado
    ChangeBytes = True
  End If
End Function
vb.net
Code:
'Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Object, ByVal pSrc As String, ByVal ByteLen As Long)
    Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByVal Destination As Object, ByVal Length As Long)
    Dim resp As Long

    <System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, charset:=Runtime.InteropServices.CharSet.Ansi)> _
    Public Structure RASDIALPARAMS
        Public dwSize As Long
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxEntryName + 1))> _
        Public szEntryName() As Byte
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxPhoneNumber + 1))> _
        Public szPhoneNumber() As Byte
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxCallbackNumber + 1))> _
        Public szCallBackNumber() As Byte
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.UNLEN + 1))> _
        Public szUsername() As Byte
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.PWLEN + 1))> _
        Public szPassword() As Byte
        <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
        sizeconst:=CInt(RASFieldSizeConstants.DNLEN + 1))> _
        Public szDomain() As Byte
    End Structure

Function ConnectVPN(ByVal Connection As String, ByVal domain As String, ByVal username As String, ByVal password As String) As Boolean
        Dim rp As RASDIALPARAMS, h As Long, resp As Long
        Dim strPBKFile As String

        Dim tempPath = If(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))

        If My.Computer.FileSystem.FileExists(tempPath & "\rasphone.pbk") Then
            Dim objReader As New System.IO.StreamReader(tempPath & "\rasphone.pbk")
            strPBKFile = objReader.ReadToEnd

            rp.dwSize = Len(rp) + 6
            ChangeBytes(Connection, rp.szEntryName)
            ChangeBytes("", rp.szPhoneNumber)
            ChangeBytes("*", rp.szCallBackNumber)
            ChangeBytes(username, rp.szUsername)
            ChangeBytes(password, rp.szPassword)
            ChangeBytes(domain, rp.szDomain)

            resp = RasDial(0, 1, rp, 0, 0, h)

            Connection = 0

            If resp > 0 Then
                GetError(resp)
                Return False
            Else
                Return True
            End If
        Else
            Return False
        End If
    End Function

    Function ChangeBytes(ByVal str As String, ByVal Bytes() As Byte) As Boolean
        'Changes a Visual Basic unicode string to an byte array
        'Returns True if it truncates str
        Dim lenBs As Byte 'length of the byte array
        Dim lenStr As Byte 'length of the string
        MsgBox(Bytes)
        lenBs = UBound(Bytes) - LBound(Bytes)
        lenStr = LenB(System.Text.Encoding.Unicode.GetBytes(str)) '(StrConv(str, vbFromUnicode))

        'lenStr = System.Text.UnicodeEncoding.Unicode.GetBytes(str)
        If lenBs > lenStr Then
            CopyMemory(Bytes(0), str, lenStr)
            ZeroMemory(Bytes(lenStr), lenBs - lenStr)
            ChangeBytes = False
        ElseIf lenBs = lenStr Then
            CopyMemory(Bytes(0), str, lenStr)
            ChangeBytes = False
        Else
            CopyMemory(Bytes(0), str, lenBs) 'Queda truncado
            ChangeBytes = True
        End If
    End Function

Last edited by luxus; 12-29-2011 at 12:29 AM.
Reply With Quote
  #2  
Old 12-28-2011, 08:00 PM
snarfblam's Avatar
snarfblamNull reference exception on a byte array snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

On which line does the exception occur?
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 12-29-2011, 12:30 AM
luxus luxus is offline
Newcomer
 
Join Date: Dec 2011
Posts: 5
Default

Quote:
Originally Posted by snarfblam View Post
On which line does the exception occur?
I'm sorry I forgot to mention it.
The error happens on the following line in ChangeBytes when calling ChangeBytes(Connection, rp.szEntryName)

lenBs = UBound(Bytes) - LBound(Bytes)

I've also marked it in the first post
Reply With Quote
  #4  
Old 12-29-2011, 06:42 AM
snarfblam's Avatar
snarfblamNull reference exception on a byte array snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Hmmmmm.... now where did that first post go?
__________________
C# _VB.NET _
Reply With Quote
  #5  
Old 12-29-2011, 10:19 AM
luxus luxus is offline
Newcomer
 
Join Date: Dec 2011
Posts: 5
Default

Quote:
Originally Posted by snarfblam View Post
Hmmmmm.... now where did that first post go?
I was wondering the same thing, but is seems that it has returned.
As you can see I've marked the line in which the error occurs in red and the command that was called for going to the ChangeBytes function in blue.
Reply With Quote
  #6  
Old 12-29-2011, 01:42 PM
PlausiblyDamp's Avatar
PlausiblyDampNull reference exception on a byte array PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I would check the API Declares for VB.Net, one of the things that changed between versions was the size of certain data types. The Declares that use Long under VB6 should probably be IntPtr under .Net
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 12-29-2011, 03:31 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

The error isnt at an API call, but instead at an lbound() call.

The function ChangeBytes() isnt getting an allocated array for its Bytes parameter.

In VB.NET, the array fields of structures cannot be preallocated like they can in VB6 (the reason for this is unimportant here.) All that MarshalAs stuff does is tell the compiler how to repacked managed data into unmanaged data for API calls.. you still need to Dim/ReDim the arrays and given them the right size.
Reply With Quote
  #8  
Old 12-30-2011, 01:27 AM
luxus luxus is offline
Newcomer
 
Join Date: Dec 2011
Posts: 5
Default

Quote:
Originally Posted by Rockoon View Post
The error isnt at an API call, but instead at an lbound() call.

The function ChangeBytes() isnt getting an allocated array for its Bytes parameter.

In VB.NET, the array fields of structures cannot be preallocated like they can in VB6 (the reason for this is unimportant here.) All that MarshalAs stuff does is tell the compiler how to repacked managed data into unmanaged data for API calls.. you still need to Dim/ReDim the arrays and given them the right size.
OK,
I've added the following lines to the ConnectVPN function:

[code]
ReDim rp.szEntryName(RASFieldSizeConstants.RAS_MaxEntryName)
ReDim rp.szPhoneNumber(RASFieldSizeConstants.RAS_MaxPhoneNumber)
ReDim rp.szCallBackNumber(RASFieldSizeConstants.RAS_MaxCallbackNumber)
ReDim rp.szUsername(RASFieldSizeConstants.UNLEN)
ReDim rp.szPassword(RASFieldSizeConstants.PWLEN)
ReDim rp.szDomain(RASFieldSizeConstants.DNLEN)
[code]

But now I get a new error:

Arithmetic operation resulted in an overflow.

Any ideas?
Reply With Quote
  #9  
Old 12-30-2011, 09:14 AM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Its going to take awhile for me to use remote viewing to see which line causes the error because I havent been practicing that skill much.

Perhaps you can help me along?

(btw: ReDim x(10) creates and *11* element array)
Reply With Quote
Reply

Tags
nullreferenceexception, vb.net, vb6


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array Null reference exception on a byte array
Null reference exception on a byte array
Null reference exception on a byte array
 
Null reference exception on a byte array
Null reference exception on a byte array
 
-->