Click here to Skip to main content
15,888,803 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to make a stealth application Pin
V.1-Mar-05 22:44
professionalV.1-Mar-05 22:44 
AnswerRe: how to make a stealth application Pin
RicoH2-Mar-05 0:17
RicoH2-Mar-05 0:17 
AnswerRe: how to make a stealth application Pin
ThatsAlok2-Mar-05 2:33
ThatsAlok2-Mar-05 2:33 
AnswerRe: how to make a stealth application Pin
JWood2-Mar-05 5:17
JWood2-Mar-05 5:17 
GeneralRe: how to make a stealth application Pin
David Crow2-Mar-05 8:37
David Crow2-Mar-05 8:37 
QuestionHow to minimize Form to taskbar like mediaplay9 Pin
sprewellkobe1-Mar-05 19:47
sprewellkobe1-Mar-05 19:47 
GeneralDRAWCLI design help Pin
KoderProject1-Mar-05 19:39
sussKoderProject1-Mar-05 19:39 
QuestionHow to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 19:39
saha2k51-Mar-05 19:39 
I am experiencing problem regarding calling a "PostThreadMessage" function from outside the thread function. A code is attched below... Please go through it and guide me as to how to go about correcting it...

/*
The PRODUCER and OTHER threads post messages to the
CONSUMER thread. All the threads wait on the
"consumerReady" event to be set by the main thread.
*/

#include
#include
#include

typedef struct temp
{
int msid;
char *msg;
}box;

// Thread IDs. Define these globally so the threads can refer to each other.

DWORD producerID, consumerID, otherID;

void get_msg(DWORD quid,box *obj);
void post_msg(DWORD quid,box *obj);

// An event
HANDLE consumerReady;


// The producer thread.

DWORD WINAPI produce(LPVOID)
{

// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);
int count = 0;

while(count < 2)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
strcpy(ptr->msg,"hello\0");
ptr->msid = 12;
post_msg(consumerID,ptr);
// PostThreadMessage(consumerID, WM_USER, (WPARAM)ptr, 0); // works fine if this
// line is used instead
// of above line
Sleep(1000);
printf("\nposted 12");
count++;
}

return 0;
}

// The consumer thread.

DWORD WINAPI consume(LPVOID)
{
// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);
Sleep(0);
int count = 0;
while (true)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
get_msg(consumerID,ptr);
count++;
printf("count %d",count);
}
return 0;
}

DWORD WINAPI other(LPVOID)
{

// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);

int count = 0;
// Send simple integers. Use WM_USER messages with the integer values
// in the wParam field.
while(count < 2)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
strcpy(ptr->msg,"other\0");
ptr->msid = 11;
post_msg(consumerID,ptr);
// PostThreadMessage(consumerID, WM_USER, (WPARAM)ptr, 0); // works fine if this
// line is used instead
// of above line
Sleep(1000);
printf("\nposted 11");
count++;
}

return 0;
}

// main() runs the simulation.

int main()
{
// Create an event which is initially unsignaled
consumerReady = CreateEvent(0, TRUE, FALSE, 0);

// Create the threads and start them immediately.
HANDLE consumerHandle = CreateThread(0, 0, consume, 0, 0, &consumerID);
HANDLE producerHandle = CreateThread(0, 0, produce, 0, 0, &producerID);
HANDLE otherHandle = CreateThread(0, 0, other, 0, 0, &otherID);

//Set the event now that all threads are ready
SetEvent(consumerReady);

//Keep the main thread alive
WaitForSingleObject(producerHandle, INFINITE);
WaitForSingleObject(consumerHandle, INFINITE);

return 0;
}

void post_msg(DWORD quid,box *obj)
{
PostThreadMessage(quid, WM_USER, (WPARAM)obj, 0);
int err = GetLastError();
printf("\n obj_post->msgid %d ",obj->msid);
printf("\n err_post = %d ",err);
}

void get_msg(DWORD quid,box *obj)
{
MSG lmsg;
GetMessage(&lmsg, 0, 0, 0);
obj = (box*)(lmsg.wParam);
int err = GetLastError();
printf("\n obj_get->msgid %d ",obj->msid);
printf("\n obj_get->msg %c ",*(obj->msg));
printf("\n err_get = %d ",err);
}

///////////////////////////////////////////////////////////////////////

The output is given below:

obj_post->msgid 12
err_post = 1444
obj_post->msgid 11
err_post = 1444
posted 12
obj_get->msgid 12
obj_get->msg h
err_get = 0 count 1
posted 11
obj_get->msgid 11
obj_get->msg o
err_get = 0 count 2
obj_post->msgid 11
err_post = 1444
obj_post->msgid 12
err_post = 1444
posted 11
posted 12

////////////////////////////////////////////////////////////

As can be seen , posting the messages results in a error id of 1444(INVALID THREAD IDENTIFIER) which is puzzling cos all the threads are already created....

Any help would be appreciated in this regard...
AnswerRe: I got the answer !!!!!!!!!! Pin
saha2k51-Mar-05 22:10
saha2k51-Mar-05 22:10 
GeneralNULL character problem Pin
LighthouseJ1-Mar-05 18:38
LighthouseJ1-Mar-05 18:38 
GeneralRe: NULL character problem Pin
RicoH2-Mar-05 0:31
RicoH2-Mar-05 0:31 
GeneralRe: NULL character problem Pin
LighthouseJ2-Mar-05 2:48
LighthouseJ2-Mar-05 2:48 
GeneralRe: NULL character problem Pin
David Crow2-Mar-05 2:34
David Crow2-Mar-05 2:34 
GeneralRe: NULL character problem Pin
LighthouseJ2-Mar-05 2:46
LighthouseJ2-Mar-05 2:46 
QuestionHow to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 18:23
saha2k51-Mar-05 18:23 
AnswerRe: How to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 22:25
saha2k51-Mar-05 22:25 
Question&quot; MFC appliation has encountered an error&quot; Why??? Pin
wasife1-Mar-05 17:32
wasife1-Mar-05 17:32 
AnswerRe: &quot; MFC appliation has encountered an error&quot; Why??? Pin
Christian Graus1-Mar-05 18:04
protectorChristian Graus1-Mar-05 18:04 
AnswerRe: " MFC appliation has encountered an error" Why??? Pin
David Crow2-Mar-05 2:44
David Crow2-Mar-05 2:44 
GeneralRe: &quot; MFC appliation has encountered an error&quot; Why??? Pin
Chris Losinger2-Mar-05 3:16
professionalChris Losinger2-Mar-05 3:16 
QuestionHow can I get the MAC addresses of all IP devices on same network ? Pin
charisma_youn1-Mar-05 16:34
charisma_youn1-Mar-05 16:34 
AnswerRe: How can I get the MAC addresses of all IP devices on same network ? Pin
Alexander M.,2-Mar-05 2:58
Alexander M.,2-Mar-05 2:58 
Questionhow to create setup files Pin
rjnl1-Mar-05 15:39
rjnl1-Mar-05 15:39 
AnswerRe: how to create setup files Pin
LighthouseJ1-Mar-05 15:59
LighthouseJ1-Mar-05 15:59 
AnswerRe: how to create setup files Pin
zhang8006051-Mar-05 16:13
zhang8006051-Mar-05 16:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.