|
Hi everyone.
I've made a working thread that interact with a window by user-defined messages (WM_USER + x).
I would like use WPARAM and/or LPARAM to give to the window objects allocated in the working thread.
In few words, I would like obtain the same result that the Windows achieve with message like WM_CREATE (LPARAM is a pointer to a structure).
I know that the object I want pass through the message must be deallocated and I've tried to use the SendMessageCallback function, but the callback user-defined function is called only if GetMessage, PeekMessage or WaitMessage are called by the same thread (MSDN docet).
Anyone can help me?
Thanks a lot,
Sergio
|
|
|
|
|
That's a tricky one. Maybe you can solve it by using PostMessage and having the message handling end do the delete but if the allocation was on a different thread this might be dangerous.
Can you use two messages, one to tell the Window end to allocate a structure, write a pointer to it at a specified address and then set an Event and the second, triggered in the secondary thread by the event, to tell the Window that structure has been filled in.
Kind of like:-
Window Thread Secondary Thread
Set up a pointer pStruct
Post a msg to the Window with pStruct
Wait on a shared event
Recieve msg with &pStruct
pStruct = new Struct
Set Shared Event
pStruct is now allocated so fill it
SendMessage to Window
Recieve msg<--------------------------
Process and delete pStruct----------->Secondary Thread continues
Perhaps that's over complicated and all you need is PostMessage but I've struggled with a few of these in the past.
Watch it when your controls are being destroyed that you don't leak allocations that havn't been used yet.
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
I might be missing something here, but what stops you from doing something like this:
FOOBAR *pData = new FOOBAR();
SendMessage( hWnd, WM_DO_FOOBAR_THING, (WPARAM)pData, NULL );
delete pData;
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
Ummm, I don't see what the problem is....
You don't say anything about what you've tried so far and how it fails, it looks to be more of a design question...
Atropus wrote: I've made a working thread that interact with a window by user-defined messages (WM_USER + x)
Quite all right, as long as you use PostMessage() and not SendMessage() to avoid a deadlock situation. But you should not use WM_USER; at least use WM_APP.
What you really should do is use registered messages.
Read this[^].
Atropus wrote: I would like use WPARAM and/or LPARAM to give to the window objects allocated in the working thread.
This should in most cases be fine as well, depending on what type of object you're allocating. Read the article linked to above.
It could be dangerous if it's an MFC object since MFC classes wraps objects like e.g. windows and sockets that are referred to through a handle and the MFC object handle map is on per thread basis.
But if it really is an MFC object your trying to pass you should re-evaluate your design because you'll get into troubles sooner or later.
Atropus wrote: I know that the object I want pass through the message must be deallocated and I've tried to use the SendMessageCallback function
Deallocate the object in the message handler.
The only danger you'll expose yourself to is a memory leak if the message for some reason should not be handled. But if that happens the memory leak would be the least of your problems.
You should really read this article[^] before continuing with your worker thread.
It's the best introduction to worker threads I've come across.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
I have the following:
CFileDialog dlg(FALSE,"for",Filename,OFN_CREATEPROMPT | OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT |OFN_PATHMUSTEXIST ,
"New Format (*.fr)|*.fr| Oldformat (*.fr)|*.fr||",this);
which will create two options on the drop down choice, How can I flag between the two options when the files have the same extension (.fr) in this case? Does this class have a member that will let me know which choice is selected?
Thanks
|
|
|
|
|
Are you referring to the nFilterIndex member (of OPENFILENAME )?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
|
As a general user interface design principle, it is probably not a good idea to make the user determine whether the file is in the "new" format or not. Can you not simply have one entry for "*.fr" files and programmatically figure out whether it is new or old?
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
I don't think its quite possible in my case. The problem is between sharing files between new application that uses (Serializes new variables)and users who still have the old applications (They cannot open new serialized files).
|
|
|
|
|
One way you can deal with this is to include version numbers in your file format and have your application inspect it to determine whether it is able to open it. If your old file format did not have a version number then start including one in the new version and interpret the absence of a version number in a file as an indication of it being in the old format. It is common for applications to be backward compatible but not the other way around, i.e., new versions of the application should be able to deal with files created using older versions but it is not necessary for older versions of the application to be able to deal with files created using the new version. Having said that, it would be desirable for applications to handle it gracefully when it encounters a file created using a newer version (use the file with reduced functionality maybe or fail to open it altogether with a user-friendly error message).
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
Hi,
Could you please help me in this issue.
I am using CTime in my code which is failing for the dates greater than 2038.Could please suggest any approach to read the dates greater than 2038.
My code is:
CTime t(y, m, d, 0, 0, 0);
if(y!=t.GetYear() || m!=t.GetMonth() || d!=t.GetDay())
{
bRet = FALSE;
}
This code is basically for displaying a Invalid date.
Thanks a lot in advance............
|
|
|
|
|
s@tish wrote: Could please suggest any approach to read the dates greater than 2038.
Use COleDateTime.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
s@tish wrote: Date greater than 2038
Use COleDateTime: 1 January 100 – 31 December 9999
In theory you could use with your CDateTimeCtrl any date in the COleDateTime range.
Some things seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hi All
how to convert LPBYTE to CString?Help me
|
|
|
|
|
cpvc++ wrote: how to convert LPBYTE to CString?Help me
If you are sure that LPBYTE contains a valid TCHAR* null terminated string then you can cast it to LPTSTR and assign to CString.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Nibu babu thomas can you give me example..
|
|
|
|
|
Other than actually typing it for you, he did.
PTBYTE name = _T("konnichiwa");
CString str = name;
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
The key thing to note here is the following qualifier:
Nibu babu thomas wrote: If you are sure that LPBYTE contains a valid TCHAR* null terminated string...
If you aren't absolutely sure about this then you could be in trouble. As a general principle, when you're serializing to and deserializing from a byte stream, it may be better to stick to a "char" or a "wchar_t" instead of "TCHAR" and then do the appropriate conversions. That's assuming you have complete control over the data stream (i.e. only your program writes to and loads from the data store). If you are implementing this on a web server extension for instance, or if you are loading the string from a file that can potentially be created and edited by other programs then you'll want to worry about text encoding and byte order marks.
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
I have an MS Word COM-addin in which this example code
try
{
throw std::exception();
}
catch (std::exception&)
{
MessageBox(NULL, "Caught", "Caught", MB_OK | MB_ICONERROR);
}
works for Debug version, but crashes Word in Release version. Is this a problem with compiler flags? I have "Enable Exception Handling" option checked both in Debug and Release. Any help would be appreciated.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
|
Microsoft Word has probably installed a global Structured Exception Handler[^] and is catching the exception before your catch block.
Try changing your code to the following and tell me if it fixes your problem.
__try
{
throw std::exception();
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
MessageBox(NULL, "Caught", "Caught", MB_OK | MB_ICONERROR);
}
Best Wishes,
-David Delaune
|
|
|
|
|
hi....
im new to widgets....
i downloaded wxWidgets 2.8.9.....
i BUILD the wx.dsw file in all configurations....
but it failed in 4 configurations....namely
DEBUG DLL, RELEASE DLL, UNICODE DEBUG DLL AND UNICODE RELEASE DLL....
I DOWNLOADED THE SAMPLE "HELLO WORLD" wxWidgets
program from this site...... when i build it....
i get the error.....
...HelloWorld fatal error LNK1181: cannot open input file 'zlib.lib'
now what is the problem????
somebody please help me
|
|
|
|
|
Here[^] is an article about wxWidgets (it contains a section describing how to install it). It's a bit outdated but you can check if it is of any value to you.
|
|
|
|
|
There is a forum devoted exclusively to wxWidgets that is very good at helping wxForum[^]
Judy
|
|
|
|
|
Hi guys,
I'm using Visual Studio 2005 and I have few questions:
1. If I have function without parameters, what to do to skip () when invoking the function?
2. I have:
int Mem;
...
Mem = LocalAlloc(LHND, ...);
...
WriteFile(f, Mem,...)
Mem keeps address of memory block.
What expression to write in WriteFile(f, ???Mem,...) to write the memory block pointed by Mem in file?
3. Is there a way a class member field to be accessible just for reading without writing?
P.S. If you know the answer of at least 1 question, please share it.
modified on Friday, November 21, 2008 6:29 AM
|
|
|
|