I am developing a Smartcard application on Windows Mobile 5.0 and am experiencing an application abort on a
Marshal.FreeHGlobal function. A code snippet is below:
<DllImport("winscard.dll", SetLastError:=True)> _
Friend Shared Function SCardEstablishContext(ByVal dwScope As UInt32, _ByVal pvReserved1 As UInt32, _
ByVal pvReserved2 As UInt32, _ByRef phContext As Long) As UInt32
End Function
Private m_hContext As Long = 0
Private m_nLastError As Long = 0
Private m_hContext As Long = 0
Public Sub EstablishContext(ByVal Scope As SCOPE)
Dim hContext As Long = Marshal.AllocHGlobal(Marshal.SizeOf(m_hContext))m_nLastError = SCardEstablishContext(CType(Scope, System.Int32), IntPtr.Zero, IntPtr.Zero, hContext)
If m_nLastError <> RETURN_CODE.SCARD_S_SUCCESS Then
Dim msg As String = "SCardEstablishContext error: " & m_nLastError
Marshal.FreeHGlobal(hContext)
Throw New Exception(msg)
End If
m_hContext = hContext
Marshal.FreeHGlobal(hContext)
End Sub
The function seems to work properly until the Marshal.FreeHGlobal(hContext) statement executes then the application simply stops running with no exception thrown.
Any help with this would be greatly appreciated.