rep+ and my eternal gratitude if you can help me come up with a good answer.
I have been banging my head against a wall here trying to solve this...
The Backstory
I have a C# GUI that needs to use the functions from a project written in C++.
I am converting the original C++ program into a un-managed and unregistered C++ DLL. The C# GUI calls the C++ DLL. So far, that parts working just fine.
I am doing this software work as a contractor for a company. My code contains no proprietary information or company trade secrets, so i got permission from the engineering team to show as much of my code as you guys want to see.
( I can even give you the whole project, but it's 8 layers of software in 35+ files. )
I just graduated from college last year, and this is my first time doing C++. I am a n00b at C++, and have been teaching myself how to use it.
The Problem
The ORIGINAL C++ code uses MFC "ON_MESSAGE" for event handling. The bottom layer of software is running it it's own thread. It needs to notify the top layer that a event has happened.
The "ON_MESSAGE" command (from the original code) no longer works in my program, even though all the code relating to "on message" has been unchanged from the original code (I believe). The only thing I did was put the original code into a DLL, and remove some GUI elements. When I debug the code, I can watch the lower software layer send out the ON_MESSAGE event, but the top software layer never gets the message. Perhaps it could be related to the code running in a DLL? Do DLL's handle threading differently than native C++ applications?
I have asked the other software engineers, and none of them can figure out why my code does not work. After hours of trying, I gave up on ON_MESSAGE.
So I tried using the standard C++ event handling code given here --> http://msdn.microsoft.com/en-us/library/ee2k0a7d.aspx . The problem is, I can get this code to WORK on just one thread on just one layer. I can not get it to work when passing events between threads and layers.
Finally, I tried a finite state machine for the events. This works,.......but gobble up all the resources of the processor. So instead of firing instantly, ...it takes about 2 minutes for the event to fire. My "while" loop seems to be gobbling up my resources, but if i remove the "while" loop it only handles 1 event then stops. Is their any way to release the main thread that runs the while loop for a few milliseconds? or make it at the lowest priority?
I've been banging my head against a wall for half a week trying to solve this. Maybe someone can help me take 2 steps back and find a simple way to do this? Or maybe someone can find the fault in my original ON_MESSAGE code?
The ON_MESSAGE code I tried is as follows.. (These are really huge files. I am only showing the code that I believe is necessary. I can show more if you wish.)
Top layer
Top Layer .CPP file
BEGIN_MESSAGE_MAP(CPIC32UBLDlg, CDialog)
ON_MESSAGE(WM_USER_BOOTLOADER_RESP_OK, OnReceiveResponse)
ON_MESSAGE(WM_USER_BOOTLOADER_NO_RESP, OnTransmitFailure)
END_MESSAGE_MAP()
LRESULT CPIC32UBLDlg::OnReceiveResponse(WPARAM cmd, LPARAM RxDataPtrAdrs)
{
// Event handling code goes here
}
LRESULT CPIC32UBLDlg::OnTransmitFailure(WPARAM cmd, LPARAM)
{
// Event handling code goes here
}
Top Layer .H file
class CPIC32UBLDlg : public CDialog
{
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg LRESULT OnReceiveResponse(WPARAM, LPARAM);
afx_msg LRESULT OnTransmitFailure(WPARAM, LPARAM);
}
Bottom Layer
Bottom Layer .CPP file
void some_class :: some_function(void)
{
::PostMessage(m_hWnd, WM_USER_BOOTLOADER_NO_RESP, (WPARAM)LastSentCommand, 0 );
}
Bottom Layer .H file
#define WM_USER_BOOTLOADER_RESP_OK WM_USER+1
#define WM_USER_BOOTLOADER_NO_RESP WM_USER+2
The state machine i tried is as follows....(These are really huge files. I am only showing the code that I believe is necessary. I can show more if you wish.)
Top layer
while (1)
{
switch (Lower_layer_Class.state)
{
case OnTransmitFailure
{
// Event handling code goes here
break;
}
case OnReceiveResponse
{
// Event handling code goes here
break;
}
case default:
{
break
}
}
UPDATE: As of right now, I am giving up on my ON_MESSAGE code and While(1)-state-machine-thingy. I am going to give the event handler a second try. I am going to base my solution off the code presented here --> http://www.codeproject.com/Articles/2042/Using-events-for-thread-synchronization . I'll give you an update if I get it working. Still, feel free to post if you know of a easier way to solve my problem.
Edited by crimsontears809739 - 10/3/12 at 9:07am
I have been banging my head against a wall here trying to solve this...The Backstory
I have a C# GUI that needs to use the functions from a project written in C++.
I am converting the original C++ program into a un-managed and unregistered C++ DLL. The C# GUI calls the C++ DLL. So far, that parts working just fine.
I am doing this software work as a contractor for a company. My code contains no proprietary information or company trade secrets, so i got permission from the engineering team to show as much of my code as you guys want to see.
( I can even give you the whole project, but it's 8 layers of software in 35+ files. )
I just graduated from college last year, and this is my first time doing C++. I am a n00b at C++, and have been teaching myself how to use it.
The Problem
The ORIGINAL C++ code uses MFC "ON_MESSAGE" for event handling. The bottom layer of software is running it it's own thread. It needs to notify the top layer that a event has happened.
The "ON_MESSAGE" command (from the original code) no longer works in my program, even though all the code relating to "on message" has been unchanged from the original code (I believe). The only thing I did was put the original code into a DLL, and remove some GUI elements. When I debug the code, I can watch the lower software layer send out the ON_MESSAGE event, but the top software layer never gets the message. Perhaps it could be related to the code running in a DLL? Do DLL's handle threading differently than native C++ applications?
I have asked the other software engineers, and none of them can figure out why my code does not work. After hours of trying, I gave up on ON_MESSAGE.
So I tried using the standard C++ event handling code given here --> http://msdn.microsoft.com/en-us/library/ee2k0a7d.aspx . The problem is, I can get this code to WORK on just one thread on just one layer. I can not get it to work when passing events between threads and layers.
Finally, I tried a finite state machine for the events. This works,.......but gobble up all the resources of the processor. So instead of firing instantly, ...it takes about 2 minutes for the event to fire. My "while" loop seems to be gobbling up my resources, but if i remove the "while" loop it only handles 1 event then stops. Is their any way to release the main thread that runs the while loop for a few milliseconds? or make it at the lowest priority?
I've been banging my head against a wall for half a week trying to solve this. Maybe someone can help me take 2 steps back and find a simple way to do this? Or maybe someone can find the fault in my original ON_MESSAGE code?
The ON_MESSAGE code I tried is as follows.. (These are really huge files. I am only showing the code that I believe is necessary. I can show more if you wish.)
Top layer
Top Layer .CPP file
BEGIN_MESSAGE_MAP(CPIC32UBLDlg, CDialog)
ON_MESSAGE(WM_USER_BOOTLOADER_RESP_OK, OnReceiveResponse)
ON_MESSAGE(WM_USER_BOOTLOADER_NO_RESP, OnTransmitFailure)
END_MESSAGE_MAP()
LRESULT CPIC32UBLDlg::OnReceiveResponse(WPARAM cmd, LPARAM RxDataPtrAdrs)
{
// Event handling code goes here
}
LRESULT CPIC32UBLDlg::OnTransmitFailure(WPARAM cmd, LPARAM)
{
// Event handling code goes here
}
Top Layer .H file
class CPIC32UBLDlg : public CDialog
{
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg LRESULT OnReceiveResponse(WPARAM, LPARAM);
afx_msg LRESULT OnTransmitFailure(WPARAM, LPARAM);
}
Bottom Layer
Bottom Layer .CPP file
void some_class :: some_function(void)
{
::PostMessage(m_hWnd, WM_USER_BOOTLOADER_NO_RESP, (WPARAM)LastSentCommand, 0 );
}
Bottom Layer .H file
#define WM_USER_BOOTLOADER_RESP_OK WM_USER+1
#define WM_USER_BOOTLOADER_NO_RESP WM_USER+2
The state machine i tried is as follows....(These are really huge files. I am only showing the code that I believe is necessary. I can show more if you wish.)
Top layer
while (1)
{
switch (Lower_layer_Class.state)
{
case OnTransmitFailure
{
// Event handling code goes here
break;
}
case OnReceiveResponse
{
// Event handling code goes here
break;
}
case default:
{
break
}
}
UPDATE: As of right now, I am giving up on my ON_MESSAGE code and While(1)-state-machine-thingy. I am going to give the event handler a second try. I am going to base my solution off the code presented here --> http://www.codeproject.com/Articles/2042/Using-events-for-thread-synchronization . I'll give you an update if I get it working. Still, feel free to post if you know of a easier way to solve my problem.
Edited by crimsontears809739 - 10/3/12 at 9:07am









