Unable to get structure into managed data space using GetLParam

  • Thread starter Thread starter Robert Manookian
  • Start date Start date
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
 
Robert,

Are you sure you're looking at the right message, and not some other
message that happens to pass 1026 as wParam? I'd include a check to
verify that the message you're looking at is indeed WM_COMMAND in the
VB.NET code.



Mattias
 
You are indeed correct that I should include this check. However, the data
that GetLParam returns is still incorrect after I implemented the check.
 
Back
Top