|
Typically you'd pass a pointer to the class object to the thread.
In the thread proc, cast the LPVOID back to a pointer to the object.
In your case, a pointer to the dialog class object that creates the thread is probably appropriate.
The thread proc can be a static member of the class.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How would you implement a critical section between two or more windows in a single thread, i.e. if one winproc tries to access it, it halts in place till its available, while other windows in thread continue to function and continue to recieve messages. I know CriticalSections work for threads, not windows - I was thinking of something implemented with GetMessage, PeekMessage or whatever. Maybe this is impossible.
|
|
|
|
|
If you only have one thread there's no way to halt that thread and have anything else happen in your windows or window proc(s) (aside from asynchronous
callbacks, of course, but those are separate threads).
With a single thread, while a windowproc is handling a message, all other window procs are halted.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: If you only have one thread there's no way to halt that thread and have anything else happen in your windows or window proc(s) (aside from asynchronous
callbacks, of course, but those are separate threads).
With a single thread, while a windowproc is handling a message, all other window procs are halted.
Well, if a resource is unavailable, why couldn't a WinProc start a GetMessage-Translate-Dispatch loop right there until a special message was sent indicating the resource was available, at which point the WinProc would halt the GetMessage Loop and continue as normal. I'm not understanding why this doesn't work.
|
|
|
|
|
Because that "GetMessage-Translate-Dispatch loop" is running on the same thread you start it on. Everything occurs serially until there's another thread.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am experimenting in OpenGL and want to switch between full screen mode and windowed mode. I have code that does this, but I think ALT + ENTER is a standard key combination for this function so I want to add it in my code. I tried processing WM_KEYDOWN messages, but I'm not sure what key code to look for. I am not even sure if I am processing the right message.
Any suggestions?
|
|
|
|
|
For WM_KEYDOWN, something like this
if (VK_RETURN == wParam)
{
// enter
if (0 > ::GetKeyState(VK_MENU))
{
// alt-enter
}
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks Mark, but it does not work consistently. The first time I press the combination it works, but after that it does not. I also use F11 and that works flawlessly.
I tried printing wParam and lParam to a file to see what is generated and it does not even show the codes for the WM_KEYDOWN events which make me wonder if it is being processed under a different message. Any Ideas?
Code snippets as follows...
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
...
case WM_KEYDOWN:
...
OnKeyDown(wParam, lParam);
break;
...
}
...
}
...
void OnKeyDown(int pKeyCode, DWORD pFlags) {
...
switch (pKeyCode) {
case 122:
...
DestroyWin();
CreateWin();
break;
case VK_RETURN:
if (GetKeyState(VK_MENU)) {
...
DestroyWin();
CreateWin();
}
break;
}
}
|
|
|
|
|
The keyboard messages go to the window with keyboard focus. If your WndProc is the only window proc
for all your windows, you should get all of them, except ones that go to windows of classes that aren't
yours (e.g. un-subclassed controls). Is focus going to another window?
I'm not sure how WM_KEYDOWN/VK_F11 could work flawlessly but you don't receive WM_KEYDOWN/VK_RETURN.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I only defined one WndProc() and created only one window. However, I am not experienced enough to tell if windows were defined somewhere else without my knowledge. Its a basic application skeleton with no controls yet.
I tested again and realised that the sysmenu (the one to the top left of the screen that is attached to the window's icon) activates when I hold down the alt key. Maybe this is responsible? Interestingly "CTL + ENTER", "SHIFT + ENTER" and "ENTER" work, which is not the behaviour I was expecting.
|
|
|
|
|
Try using WM_SYSKEYDOWN instead of WM_KEYDOWN for ALT key combinations.
Better yet, try this for ALT-ENTER:
case WM_SYSCOMMAND:
if (SC_KEYMENU == (wParam & 0xFFF0))
{
// alt - enter
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I need to develop an application that records sound amplitude and plot in on a graph with a function of time, using a microphone. Is the best approach to do this using DirectSound. If so is there any sample code that is available for this, or some kind of tutorial.
Thanks in Advance.
|
|
|
|
|
Either DirectSound or the Windows Multimedia APIs will work. If you choose DirectSound then download the latest
DirectSound SDK and you'll find sample apps to get you started, along with documentation. The "CaptureSound Sample"
is a good start
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I have an application where everything is written in serial fashion, one after the other and this included computation and updating certain displays within the main application. All these secondary displays are the child windows of the main application. This is a closed loop system where I need to update the displays in each iteration. While updating these displays takes 80% of the total time for each iteration, we want to increase the speed of the closed loop. So, I created different thread for updating displays while the computation loop works on it own and sets the flag for the display datasets when they r ready. My threads are defined in main application dialog file.
Here comes the problem, inorder to update the displays I am calling a method of the corresponding dialog box, which inturn calls the OnPaint() function of that dialog. This is where the program crashes with Abort Assertion Failed error.
Any ideas??
-Pavan.
Pavan
|
|
|
|
|
At first glance, this sounds like a bad design, destined for failure. How many dialogs and threads are we talking about here? Are the threads directly updating a UI component that does not belong to them?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yes, its not a good design at all, but its been written long time back at the time when I am not here. The application is a common dialog box design with 9 child dialog boxes in it. I have defined a separate thread in the main dialog box(where all the child dialog boxes are defined). There is only one thread that is defined additionally where atleast 4 dialog boxes are being refreshed by calling the methods one after the other already defined in them which inturn calls OnPaint() function of the corresponding dialog box. So we are looking at updating 4 dialog boxes from that thread. This thread is created whenever user starts closeloop and they are terminated when he stops closeloop.
Pavan
|
|
|
|
|
Pavan Berkeley wrote: So we are looking at updating 4 dialog boxes from that thread.
By posting or sending a message?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi all. I made a simple function that acts off of whether there's a certain string placed in a buffer.
<br />
string match1;<br />
string match2="virtualspace";<br />
<br />
if(client.find("matchup") != string::npos){<br />
if(client.find(word) != string::npos){<br />
if(match1 == match2){<br />
cout << "Found a match";<br />
}<br />
else{<br />
cout << "No match";<br />
}<br />
So once the first function is called and the buffer is filled it works fine. Then .clear() to empty the buffer works fine too. But when i try to fill the same buffer with the same characters from before it doesnt work. So i figured, maybe theres something left over in the buffer. So i tried to clear the buffer several times with .erase() and .clear() and i even did something like this.
string var="";
buffer << var;
Again it cleared the buffer (i think) but didnt allow me to put anything back in it.
My question is, once you wipe a buffer is there no way to re-fill it with the same data from before? And if you can, how? I've searched far and wide for an explanation. I've never dealt with something like this so i have no clue as to what i should be doing. Any suggestions? Thanx in advance!
|
|
|
|
|
void yourecrazy()
{
string str = "you are crazy";
cout << str.c_str() << endl;
str.clear();
cout << str.c_str() << endl;
str = "you are crazy";
cout << str.c_str() << endl;
}
|
|
|
|
|
.clear() doesnt work with VC++ 6
<br />
string password="logmein";<br />
stringstream loginsys;<br />
string loggedin;<br />
string loggedin2="Loggedin";<br />
<br />
bool loginsystem(const string& login){ <br />
if(buff.find(password) != string::npos){<br />
loginsys << "Loggedin";<br />
loggedin=loginsys.str();<br />
cout << "Logged in " << endl;<br />
return true;<br />
}<br />
else{<br />
cout << "Cannot log you in " << endl;<br />
return false;<br />
}<br />
}<br />
<br />
void logout(void){<br />
loggedin.erase();<br />
loggedin2.erase();<br />
cout << "Your now logged out. " << endl;<br />
}<br />
<br />
if(buff.find(".hi") != string::npos){<br />
if(buff.find(host) != string::npos){<br />
if(loggedin == loggedin2){<br />
cout << "Hello " << endl;<br />
}<br />
}<br />
}<br />
Even once loggedin and loggedin2 string buffers are emptied, i cant seem to find out how to fill it again with "Loggedin" so that the bottom function can check to see if the characters are in the buffer.
|
|
|
|
|
dellthinker wrote: i cant seem to find out how to fill it agai
Where are you trying to assign loggedin and loggedin2 new values?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: Where are you trying to assign loggedin and loggedin2 new values?
<br />
void logout(void){<br />
string empty="";<br />
string empty2="";<br />
loggedin=empty;<br />
loggedin2=empty;<br />
cout << "Your now logged out. " << endl;<br />
}<br />
This method clears the buffers, because the client doesnt respond when i type a command because it checks to see if those particular buffers have "Loggedin" inside. The problem is when i login again and it "re-fills" the buffer with the words "Loggedin" it doesnt respond .
|
|
|
|
|
I must admit that I'm a bit confused as to what the problem is. Assigning (empty) values to string variables is not going to cause your program to not respond.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i have a window console application used to generate signal ouput from a board. When i try to write this into a MFC dialog, using the same code(i mean a specific part), a warning occurs.
The signal generator board comes with its own .h file and library that includes driver functions.
in one .h file, following types are defined:
# define int16 short int
# define int32 long int
# define ptr8 char*
in the library, following driver function prototype is defined;
HEAD SpcSetParam (int16 nNr, int32 lReg, int32 lValue);
in my header file; i declared 2 char pointers:
ptr8 pbyData[2];
in my cpp, i allocated 2 char array;
for (i=0; i<2; i++)
pbyData = (ptr8) GlobalAlloc (GMEM_FIXED,1024);
then i write data to the array,and need to pass the address of this two array to the driver function.
Using the function provided:
for (i=0; i<2; i++){
nErr = SpcSetParam (hDrv, SPC_FIFO_BUFADR0+i, (int32) pbyData); <---------warning occurs here
ignore the nErr and hDrv, SPC_FIFO_BUFADR0+i is the value for specific register, and (int32) pbyData i think is the address of the the array
the code works fine in console application, no warnings, however, using the same code in a MFC dialog, following error generated:
warning C4311: 'type cast' : pointer truncation from 'char *' to 'long'
could anybody tell me whats going on and how to fix it??
Thanks
g, following error generated:
warning C4311: 'type cast' : pointer truncation from 'char *' to 'long'
could anybody tell me whats going on and how to fix it??
Thanks
|
|
|
|
|
It looks like a 64-bit warning - In a 64-bit build, converting a pointer to a long will chop 32 bits off the pointer.
You can disable the 64-bit portability warnings in the compiler options or use #pragma warning(disable: 4311).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|