Convert to VB.NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Clark

    Convert to VB.NET

    Hello,

    Recently, I converted my VB6 app to VB.NET. I got the standard upgrade
    messages and fixed them so now I can run my app. I have noticed, though,
    that some of the library functions will not work in .NET, namely:

    CreateFile
    LockFile
    UnlockFile
    CloseHandle

    declared as

    Public Declare Auto Function CreateFile Lib "kernel32.d ll" Alias
    "CreateFile A" (ByVal lpFileName As String, ByVal dwDesiredAccess As
    Int32, ByVal dwShareMode As Int32, ByRef lpSecurityAttri butes As IntPtr,
    ByVal dwCreationDispo sition As Int32, ByVal dwFlagsAndAttri butes As
    Int32, ByVal hTemplateFile As IntPtr) As IntPtr
    Public Declare Auto Function LockFile Lib "kernel32.d ll" (ByVal hFile
    As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal dwFileOffsetHig h As
    Int32, ByVal nNumberOfBytesT oLockLow As Int32, ByVal
    nNumberOfBytesT oLockHigh As Int32) As Int32
    Public Declare Auto Function UnlockFile Lib "kernel32.d ll" (ByVal
    hFile As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal dwFileOffsetHig h
    As Int32, ByVal nNumberOfBytesT oUnlockLow As Int32, ByVal
    nNumberOfBytesT oUnlockHigh As Int32) As Int32
    Public Declare Auto Function CloseHandle Lib "kernel32.d ll" (ByVal
    hObject As IntPtr) As Boolean

    How do I fix this problem?

    Thanks,
    Andrew
  • David Browne

    #2
    Re: Convert to VB.NET


    "Andrew Clark" <lark047@hotmai l.com> wrote in message
    news:1128102107 .693793a8f2ca80 434a2a9538de42f 1ce@teranews...[color=blue]
    > Hello,
    >
    > Recently, I converted my VB6 app to VB.NET. I got the standard upgrade
    > messages and fixed them so now I can run my app. I have noticed, though,
    > that some of the library functions will not work in .NET, namely:
    >
    > CreateFile
    > LockFile
    > UnlockFile
    > CloseHandle
    >[/color]
    The conversion process does not do a good job with Win32 functions. You
    should either replace the Win32 API calls with the built-in functionality
    from the .net frametwork, or go research the proper vb.net Win32 api
    declares (www.pinvoke.net).

    David


    Comment

    • Dragon

      #3
      Re: Convert to VB.NET

      Hi Andrew,

      In CreateFile declaration change ~ ByRef lpSecurityAttib utes As IntPtr ~
      either to ~ ByVal lpSecurityAttri butes As IntPtr ~ or to ~ ByRef
      lpSecurityAttri butes As SECURITY_ATTRIB UTES ~.

      Also please post code in which you use these functions.

      Roman

      "Andrew Clark" <lark047@hotmai l.com> ñîîáùèë/ñîîáùèëà â íîâîñòÿõ
      ñëåäóþùåå: news:1128102107 .693793a8f2ca80 434a2a9538de42f 1ce@teranews...[color=blue]
      > Hello,
      >
      > Recently, I converted my VB6 app to VB.NET. I got the standard upgrade
      > messages and fixed them so now I can run my app. I have noticed,[/color]
      though,[color=blue]
      > that some of the library functions will not work in .NET, namely:
      >
      > CreateFile
      > LockFile
      > UnlockFile
      > CloseHandle
      >
      > declared as
      >
      > Public Declare Auto Function CreateFile Lib "kernel32.d ll" Alias
      > "CreateFile A" (ByVal lpFileName As String, ByVal dwDesiredAccess As
      > Int32, ByVal dwShareMode As Int32, ByRef lpSecurityAttri butes As[/color]
      IntPtr,[color=blue]
      > ByVal dwCreationDispo sition As Int32, ByVal dwFlagsAndAttri butes As
      > Int32, ByVal hTemplateFile As IntPtr) As IntPtr
      > Public Declare Auto Function LockFile Lib "kernel32.d ll" (ByVal[/color]
      hFile[color=blue]
      > As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal dwFileOffsetHig h As
      > Int32, ByVal nNumberOfBytesT oLockLow As Int32, ByVal
      > nNumberOfBytesT oLockHigh As Int32) As Int32
      > Public Declare Auto Function UnlockFile Lib "kernel32.d ll" (ByVal
      > hFile As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal[/color]
      dwFileOffsetHig h[color=blue]
      > As Int32, ByVal nNumberOfBytesT oUnlockLow As Int32, ByVal
      > nNumberOfBytesT oUnlockHigh As Int32) As Int32
      > Public Declare Auto Function CloseHandle Lib "kernel32.d ll" (ByVal
      > hObject As IntPtr) As Boolean
      >
      > How do I fix this problem?
      >
      > Thanks,
      > Andrew[/color]


      Comment

      • Andrew Clark

        #4
        Re: Convert to VB.NET

        "Dragon" <no@spam.please > wrote in news:Oa2kj9exFH A.3256
        @TK2MSFTNGP09.p hx.gbl:
        [color=blue]
        > Hi Andrew,
        >
        > In CreateFile declaration change ~ ByRef lpSecurityAttib utes As IntPtr ~
        > either to ~ ByVal lpSecurityAttri butes As IntPtr ~ or to ~ ByRef
        > lpSecurityAttri butes As SECURITY_ATTRIB UTES ~.
        >
        > Also please post code in which you use these functions.
        >
        > Roman
        >[/color]

        I made the suggested change. hFile still gets -1 (and I am sure the file
        exists). Here is a snippet where I use CreateFile,

        Dim hFile As IntPtr
        hFile = IntPtr.Zero

        hFile = CreateFile( _
        "test03.txt ", _
        GENERIC_READ, _
        0, _
        IntPtr.Zero, _
        OPEN_EXISTING, _
        FILE_ATTRIBUTE_ NORMAL, _
        IntPtr.Zero)

        This will always set hFile to -1. The values of the constants are listed
        below,

        GENERIC_READ 1179785
        OPEN_EXISTING 3
        FILE_ATTRIBUTE_ NORMAL 128

        I am not finding a whole lot of help on the 'net for this :(

        Andrew

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Convert to VB.NET

          "Andrew Clark" <lark047@hotmai l.com> schrieb:

          In addition to the other replies:
          [color=blue]
          > Public Declare Auto Function CreateFile Lib "kernel32.d ll" Alias
          > "CreateFile A"[/color]

          Remove the 'Alias "CreateFile A"' alias.
          [color=blue]
          > Public Declare Auto Function LockFile Lib "kernel32.d ll" (ByVal hFile[/color]

          Remove 'Auto'.
          [color=blue]
          > Public Declare Auto Function UnlockFile Lib "kernel32.d ll" (ByVal[/color]

          Remove 'Auto'.
          [color=blue]
          > Public Declare Auto Function CloseHandle Lib "kernel32.d ll" (ByVal
          > hObject As IntPtr) As Boolean[/color]

          Remove 'Auto'.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Dragon

            #6
            Re: Convert to VB.NET

            > GENERIC_READ 1179785

            Ah, now I see...
            It's

            ~
            Const GENERIC_READ As Integer = &H80000000
            ~

            Roman


            Comment

            Working...