R
Robert Manookian
We have a C++ windows application that sends windows messages to a running
VB application to perform various activities. On of these activities needs
more information than can fit into a 32 bit integer. I created a structure
to hold the information needed. However, when I get the information in the
VB.Net code using the GetLParam method it returns incorrect results. How
does one pass a structure from unmanaged code to managed code via a windows
message? Here's the code:
C++ Code:
#include "stdafx.h"
#include "resource.h"
#include <Shellapi.h>
#include "objbase.h"
#pragma pack(4)
struct DataviewMessage {
long TaskID;
long SettingKey;
};
void PostBusinessDesktopDataviewMessage(long lTaskID, long lSettingKey);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
PostBusinessDesktopDataviewMessage((long)1, (long)1);
return TRUE;
}
void PostBusinessDesktopDataviewMessage(long lTaskID, long lSettingKey)
{
HWND hTargetWnd = ::FindWindow(NULL,
kBsinessDesktopHelperWindowCaption);
if (hTargetWnd != NULL) {
DataviewMessage *DvMsg = (DataviewMessage
*)malloc(sizeof(DataviewMessage));
DvMsg->TaskID = lTaskID;
DvMsg->SettingKey = lSettingKey;
::SendMessage(hTargetWnd, WM_COMMAND, 1026, (long)DvMsg);
free(DvMsg);
}
}
VB Code:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Explicit)> _
Public Structure DataviewShortcut
<FieldOffset(0)> Public TaskID As Integer
<FieldOffset(4)> Public SettingKey As Integer
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Try
If (m.WParam.ToInt32 = 1026) Then
If (m.LParam.ToInt32 <> 0) Then
Dim DataviewSetting As Object
DataviewSetting = m.GetLParam(GetType(DataviewShortcut))
Console.WriteLine(CType(DataviewSetting,
DataviewShortcut).TaskID & ", " & CType(DataviewSetting,
DataviewShortcut).SettingKey)
End If
End If
Catch oException As Exception
MessageBox.Show(oException.Message, Me.Text,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MyBase.WndProc(m)
End Sub
VB application to perform various activities. On of these activities needs
more information than can fit into a 32 bit integer. I created a structure
to hold the information needed. However, when I get the information in the
VB.Net code using the GetLParam method it returns incorrect results. How
does one pass a structure from unmanaged code to managed code via a windows
message? Here's the code:
C++ Code:
#include "stdafx.h"
#include "resource.h"
#include <Shellapi.h>
#include "objbase.h"
#pragma pack(4)
struct DataviewMessage {
long TaskID;
long SettingKey;
};
void PostBusinessDesktopDataviewMessage(long lTaskID, long lSettingKey);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
PostBusinessDesktopDataviewMessage((long)1, (long)1);
return TRUE;
}
void PostBusinessDesktopDataviewMessage(long lTaskID, long lSettingKey)
{
HWND hTargetWnd = ::FindWindow(NULL,
kBsinessDesktopHelperWindowCaption);
if (hTargetWnd != NULL) {
DataviewMessage *DvMsg = (DataviewMessage
*)malloc(sizeof(DataviewMessage));
DvMsg->TaskID = lTaskID;
DvMsg->SettingKey = lSettingKey;
::SendMessage(hTargetWnd, WM_COMMAND, 1026, (long)DvMsg);
free(DvMsg);
}
}
VB Code:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Explicit)> _
Public Structure DataviewShortcut
<FieldOffset(0)> Public TaskID As Integer
<FieldOffset(4)> Public SettingKey As Integer
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Try
If (m.WParam.ToInt32 = 1026) Then
If (m.LParam.ToInt32 <> 0) Then
Dim DataviewSetting As Object
DataviewSetting = m.GetLParam(GetType(DataviewShortcut))
Console.WriteLine(CType(DataviewSetting,
DataviewShortcut).TaskID & ", " & CType(DataviewSetting,
DataviewShortcut).SettingKey)
End If
End If
Catch oException As Exception
MessageBox.Show(oException.Message, Me.Text,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MyBase.WndProc(m)
End Sub