Question

I've recreated some of the OpenNetCF components like PowerManagement and DeviceStatusMonitor. But since they never raised any events I suspected that something was wrong. My first thought was to check the P2PMessageQueue which they both depends on. And then BAM, the CreateMsgQueue returns IntPtr.Zero. Checking for the last Win32Error gives me an error code of value -2147467259 (minus).

Is this just another generic error code that doesn't provide any hints?

Any help would be appreciated.

(FYI: WinCE 5.0, CF 2.0)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

            Dim lpName As String = "MyQueue"
            Dim lpOptions As MSGQUEUEOPTIONS = New MSGQUEUEOPTIONS()
            Dim hMsgQ As IntPtr = IntPtr.Zero

            lpOptions.bReadAccess = True
            lpOptions.dwMaxMessages = 0
            lpOptions.cbMaxMessage = &H1000
            lpOptions.dwFlags = MSGQUEUE_ALLOW_BROKEN
            lpOptions.dwSize = Marshal.SizeOf(lpOptions)

            hMsgQ = CreateMsgQueue(lpName, lpOptions)

            If (hMsgQ = IntPtr.Zero) Then
                Throw New Win32Exception(Marshal.GetLastWin32Error())
            Else
                CloseMsgQueue(hMsgQ)
            End If

        Catch ex As Win32Exception
            MessageBox.Show(String.Format(String.Format("Win32Exception: {0}", ex.ErrorCode)))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    <DllImport("coredll.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function CloseMsgQueue(ByVal hMsgQ As IntPtr) As Boolean
    End Function

    <DllImport("coredll.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function CreateMsgQueue(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpName As String, ByVal lpOptions As MSGQUEUEOPTIONS) As IntPtr
    End Function

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure MSGQUEUEOPTIONS
        Public dwSize As Integer
        Public dwFlags As Integer
        Public dwMaxMessages As Integer
        Public cbMaxMessage As Integer
        Public bReadAccess As Boolean
    End Structure

    Private Const MSGQUEUE_ALLOW_BROKEN As Integer = 2
    Private Const MSGQUEUE_NOPRECOMMIT As Integer = 1
    Private Const MSGQUEUE_MSGALERT As Integer = 1

End Class
Was it helpful?

Solution

The lpOptions parameter is declared incorrectly. You declare it as ByVal but it should be ByRef.

That said, -2147467259 is a bit of an oddity. That's not a Win32 error code. That's a COM HRESULT. Specifically it's 0x80004005. Which is the COM wrapper around the Win32 ERROR_ACCESS_DENIED. Not sure where you get a COM HRESULT from in this code mind you, but it would appear that you don't have sufficient rights for what you are attempting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top