Cannot Marshall Me.Handle()

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

    #1

    Cannot Marshall Me.Handle()

    I wanted to pass ByVal a Me.Handle() to a CStyle DLL.
    The code for the DLL is as follows:

    void __stdcall WriteWindowHand le(HWND wHandle)
    {
    DATASTRUCT ds;
    DWORD dwBytesWritten;

    ds.wndHandle = wHandle;

    m_pMem->Lock();
    BOOL bSuccess = m_pMem->Write (
    &ds, // Destination for data
    sizeof(ds), // Number of bytes to write
    &dwBytesWritten , // Buffer for count of bytes uploaded to shared memory
    0 // Offset into shared memory buffer
    );
    m_pMem->Unlock();

    return;
    }

    Using Visual Basic 6 a Long parameter is easily marshalled to HWND wHandle.
    However using the VB.NET code written below I cannot find a way to marshal
    Me.Handle() into the above HWND wHandle parameter. The code does compile,
    but will not execute.

    Public Class LibWrap

    ' Declare a managed prototype for the unmanaged function.

    Declare Function WriteWindowHand le Lib _

    "C:\Program Files\AmiBroker \Plugins\XLPlug in.dll" (ByVal i As Integer)

    End Class 'LibWrap

    ' Blah Blah Blah

    Public Sub New()

    MyBase.New()

    'This call is required by the Windows Form Designer.

    InitializeCompo nent()

    LibWrap.WriteWi ndowHandle(Me.H andle().ToInt32 ()) 'This line will not execute

    'System.Runtime .InteropService s.MarshalDirect iveException' occurred in App5.exe

    End Sub

    The question is how can I marshal Me.Handle() into HWND wHandle.



  • Herfried K. Wagner [MVP]

    #2
    Re: Cannot Marshall Me.Handle()

    "Rob" <rls_jls@worldn et.att.net> schrieb:[color=blue]
    >I wanted to pass ByVal a Me.Handle() to a CStyle DLL.
    >The code for the DLL is as follows:
    >
    >void __stdcall WriteWindowHand le(HWND wHandle)
    >[...]
    > Declare Function WriteWindowHand le Lib _[/color]

    'Function' => 'Sub'.
    [color=blue]
    >
    > "C:\Program Files\AmiBroker \Plugins\XLPlug in.dll" (ByVal i As Integer)[/color]

    'As Integer' => 'As IntPtr'.
    [color=blue]
    > LibWrap.WriteWi ndowHandle(Me.H andle().ToInt32 ()) 'This line will not
    > execute[/color]

    => '...(Me.Handle) '.

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

    Comment

    • Rob

      #3
      Re: Cannot Marshall Me.Handle()

      After reading the fix I feel really dumb, a big thank you to Herfried.
      Thanks a lot, and pardon my negligence.

      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:OG3wqBiiFH A.2852@TK2MSFTN GP14.phx.gbl...[color=blue]
      > "Rob" <rls_jls@worldn et.att.net> schrieb:[color=green]
      > >I wanted to pass ByVal a Me.Handle() to a CStyle DLL.
      > >The code for the DLL is as follows:
      > >
      > >void __stdcall WriteWindowHand le(HWND wHandle)
      > >[...]
      > > Declare Function WriteWindowHand le Lib _[/color]
      >
      > 'Function' => 'Sub'.
      >[color=green]
      > >
      > > "C:\Program Files\AmiBroker \Plugins\XLPlug in.dll" (ByVal i As[/color][/color]
      Integer)[color=blue]
      >
      > 'As Integer' => 'As IntPtr'.
      >[color=green]
      > > LibWrap.WriteWi ndowHandle(Me.H andle().ToInt32 ()) 'This line will[/color][/color]
      not[color=blue][color=green]
      > > execute[/color]
      >
      > => '...(Me.Handle) '.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>
      >[/color]


      Comment

      Working...