Monday, January 26, 2009

Message send/receive between DLL and EXE

1. Message.h

#pragma once
#include "enums.h"
class CMessages
{
public:
static DWORD sm_dwNotificationMessage;
static HWND sm_hWndNotification;
static void PostMessage(NOTIFICATION_
MESSAGES message,LPARAM info=0);
};

//enum.h
enum NOTIFICATION_MESSAGES
{
DISCOVERY_COMPLETE=0x100,
PAIRING_COMPLETE,
PAIRING_ERROR,
DISCOVERY_ERROR,
SERVICE_DISCOVERY_COMPLETE,
SERVICE_DISCOVERY_ERROR,
};

2. Message.cpp

#include "StdAfx.h"
#include "Messages.h"

DWORD CMessages::sm_dwNotificationMessage = RegisterWindowMessage(_T("Notification2A23463D-0006-4ec7-89CC-0A27AA71C4A8"));
HWND CMessages::sm_hWndNotification = NULL;

void CMessages::PostMessage(NOTIFICATION_MESSAGES message,LPARAM info)
{
::PostMessage(CMessages::sm_hWndNotification,CMessages::sm_dwNotificationMessage,message,info);
}

3. Usage:

CMessages::PostMessage(DISCOVERY_ERROR,iError);

Exported functions:

MYLIB_API unsigned WINAPI GetNotificationMessage();
MYLIB_API void WINAPI SetNotificationWindow(HWND hWndNotify) ;

CMessages m_CMessages;
HWND GetNotificationWnd() {return m_CMessages.sm_hWndNotification;}
DWORD GetNotificationMessage() {return m_CMessages.sm_dwNotificationMessage;}
void SetNotificationWindow(HWND hWndNotify) {m_CMessages.sm_hWndNotification = hWndNotify;}

4. In exe, using C#

#region MessageFunctions
//> ---------------------------------------------------------------------------
//> Class: NotificationWindow
//> Purpose: target window for bluetooth device events
//> ---------------------------------------------------------------------------
public class NotificationWindow : MessageWindow
{
// Create an instance of the form.
private Form1 m_FormConnWizard;

// Save a reference to the form so it can
// be notified when messages are received.
public NotificationWindow(Form1 formConnWizard)
{
this.m_FormConnWizard = formConnWizard;
}

// handle window messages from the bluetooth dll
protected override void WndProc(ref Message msg)
{
if (msg.Msg == m_FormConnWizard.GetMyNotificationMessage())
{
// call back to the form to handle this message
m_FormConnWizard.NotificationMessage(msg);
}
else{}
// Call the base WndProc method
// to process any messages not handled.
base.WndProc(ref msg);
}
}
private int GetMyNotificationMessage()
{
return (int)GetNotificationMessage();
}
private void NotificationMessage(Message msg)
{
switch ((uint)msg.WParam)
{
case (uint)NOTIFICATION_MESSAGES.DISCOVERY_COMPLETE:
{
KillSearchingTimer();
MessageBox.Show("DISCOVERY COMPLETE!");
break;
}
case (uint)NOTIFICATION_MESSAGES.DISCOVERY_ERROR:
{
//dx: revise the message
MessageBox.Show("...");
break;
}
case (uint)NOTIFICATION_MESSAGES.SERVICE_DISCOVERY_COMPLETE:
{

break;
}
case (uint)NOTIFICATION_MESSAGES.SERVICE_DISCOVERY_ERROR:
{
//MessageBox.Show(Properties.Resources.strDiscoveryError, "Service Discovery Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
break;
}

}

}
#endregion

public partial class Form1 : Form
{
NotificationWindow m_NotificationWindow;
public Form1()
{
m_NotificationWindow = new NotificationWindow(this);
SetNotificationWindow(m_NotificationWindow.Hwnd);

InitializeComponent();
}
..
}

[DllImport("xxx.dll", EntryPoint = "SetNotificationWindow", SetLastError = true)]
private extern static void SetNotificationWindow(IntPtr hWndNotify);

[DllImport("xxx.dll", EntryPoint = "GetNotificationMessage", SetLastError = true)]
private extern static uint GetNotificationMessage();

No comments: