CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2011
    Posts
    6

    SafeFileHandle equivalent

    I have a class library written in VB9, and I need to convert it to .NET Compact Framework 2.0.
    I have worked through a good portion of the issues already, but cannot seem to find a workaround for using something similar to SafeFileHandle in CF.

    Here is the code:

    Code:
    Public Sub New(ByVal DevicePath As String)
            Try
    
                ' open a read/write handle to our device using the DevicePath returned
                Dim Handle As New Microsoft.Win32.SafeHandles.SafeFileHandle(CreateFile(DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, EFileAttributes.Overlapped, IntPtr.Zero), True)
    
                If Not Handle.IsInvalid Then
                    _Stream = New FileStream(Handle, FileAccess.ReadWrite, 8, True)
    
                    _Attributes.Size = Marshal.SizeOf(_Attributes)
    
                    If Interop.HidD_GetAttributes(_Stream.SafeFileHandle.DangerousGetHandle, _Attributes) Then
                        _DevicePath = DevicePath
                        DeviceSetup()
                    Else
                        Throw New ApplicationException(String.Format("Could not get attributes for device '{0}': {1}", DevicePath, GetLastWin32Error.Message))
                    End If
                Else
                    Throw New ApplicationException("Cannot open specified device: " & GetLastWin32Error.Message)
                End If
            Catch ex As ApplicationException
                Throw
            Catch ex As Exception
                Throw New ApplicationException(String.Format("Could not access device '{0}': {1}", DevicePath, ex.Message))
            End Try
        End Sub
    SafeFileHandle is not supported under the Compact Framework, so I need to come up with some way to work around that problem while keeping the functionality of the program intact.

    Ideally someone will tell me about an equivalent to SafeFileHandle and this will be a simple fix and stupid question, but I am not getting my hopes up.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: SafeFileHandle equivalent

    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2011
    Posts
    6

    Re: SafeFileHandle equivalent

    I need something that will work in Compact Framework though.
    I get: Type 'Microsoft.Win32.SafeHandles.SafeFileHandle' is not defined.

  4. #4
    Join Date
    Jul 2012
    Posts
    4

    Re: SafeFileHandle equivalent

    Sorry for digging out old topic, but did you find the answer? I have the same problem now. I got some code for desktop application that is working fine, but when i try to do the same on windows mobile device (motorola mc9090) i need safefilehandle, but there's no such thing...

  5. #5
    Join Date
    Jul 2012
    Posts
    4

    Re: SafeFileHandle equivalent

    Sorry for digging out old topic, but i have the same problem. I have some code that is working in desktop application but i nedd to do the same in Compact Framework. Did you find solution?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: SafeFileHandle equivalent

    I guess, you should ask it in some of the .Net forums, since "Compact Framework" has nothing to do with the native C++/VC++ applications.
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2012
    Posts
    4

    Re: SafeFileHandle equivalent

    Ok, then i'll try there. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured