Cannot Marshall Me.Handle()

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

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

void __stdcall WriteWindowHandle(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 WriteWindowHandle Lib _

"C:\Program Files\AmiBroker\Plugins\XLPlugin.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.

InitializeComponent()

LibWrap.WriteWindowHandle(Me.Handle().ToInt32()) 'This line will not execute

'System.Runtime.InteropServices.MarshalDirectiveException' occurred in App5.exe

End Sub

The question is how can I marshal Me.Handle() into HWND wHandle.
 
Rob said:
I wanted to pass ByVal a Me.Handle() to a CStyle DLL.
The code for the DLL is as follows:

void __stdcall WriteWindowHandle(HWND wHandle)
[...]
Declare Function WriteWindowHandle Lib _

'Function' => 'Sub'.
"C:\Program Files\AmiBroker\Plugins\XLPlugin.dll" (ByVal i As Integer)

'As Integer' => 'As IntPtr'.
LibWrap.WriteWindowHandle(Me.Handle().ToInt32()) 'This line will not
execute

=> '...(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 said:
Rob said:
I wanted to pass ByVal a Me.Handle() to a CStyle DLL.
The code for the DLL is as follows:

void __stdcall WriteWindowHandle(HWND wHandle)
[...]
Declare Function WriteWindowHandle Lib _

'Function' => 'Sub'.
"C:\Program Files\AmiBroker\Plugins\XLPlugin.dll" (ByVal i As
Integer)

'As Integer' => 'As IntPtr'.
LibWrap.WriteWindowHandle(Me.Handle().ToInt32()) 'This line will not
execute

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