Click here to Skip to main content
15,902,447 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to make a stealth application Pin
Norman-Timo1-Mar-05 22:45
Norman-Timo1-Mar-05 22:45 
GeneralWhy should this application not be listed in Task-Manager? Pin
Colin Angus Mackay2-Mar-05 0:21
Colin Angus Mackay2-Mar-05 0:21 
GeneralRe: Why should this application not be listed in Task-Manager? Pin
Dave Kreskowiak2-Mar-05 8:14
mveDave Kreskowiak2-Mar-05 8:14 
AnswerRe: how to make a stealth application Pin
Norman-Timo2-Mar-05 20:47
Norman-Timo2-Mar-05 20:47 
GeneralCreating cursor from image(bitmap) Pin
Liborac_1-Mar-05 21:26
Liborac_1-Mar-05 21:26 
GeneralXP Style selection highlight Pin
Radgar1-Mar-05 21:15
Radgar1-Mar-05 21:15 
GeneralRe: XP Style selection highlight Pin
Judah Gabriel Himango2-Mar-05 4:09
sponsorJudah Gabriel Himango2-Mar-05 4:09 
QuestionHow to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 19:28
saha2k51-Mar-05 19:28 
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 <windows.h>
#include <iostream.h>
#include <stdio.h>

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: How to call PostThreadMessage from outside the thread function Pin
leppie1-Mar-05 19:59
leppie1-Mar-05 19:59 
Generalregexpression highlight results Pin
Pyro Joe1-Mar-05 16:07
Pyro Joe1-Mar-05 16:07 
GeneralRe: regexpression highlight results Pin
leppie2-Mar-05 3:47
leppie2-Mar-05 3:47 
Generaltime Pin
Anonymous1-Mar-05 15:24
Anonymous1-Mar-05 15:24 
GeneralRe: time Pin
Bojan Rajkovic1-Mar-05 16:54
Bojan Rajkovic1-Mar-05 16:54 
GeneralRe: time Pin
Luis Alonso Ramos1-Mar-05 16:59
Luis Alonso Ramos1-Mar-05 16:59 
GeneralRe: time Pin
Luis Alonso Ramos1-Mar-05 16:56
Luis Alonso Ramos1-Mar-05 16:56 
GeneralDealing with shortcuts Pin
Christian Graus1-Mar-05 15:15
protectorChristian Graus1-Mar-05 15:15 
GeneralRe: Dealing with shortcuts Pin
Christian Graus1-Mar-05 18:07
protectorChristian Graus1-Mar-05 18:07 
GeneralRe: Dealing with shortcuts Pin
Radgar1-Mar-05 22:04
Radgar1-Mar-05 22:04 
Generalpix Pin
wael_waw1-Mar-05 14:33
wael_waw1-Mar-05 14:33 
GeneralRe: pix Pin
Christian Graus1-Mar-05 15:13
protectorChristian Graus1-Mar-05 15:13 
GeneralSimple example ZIP of Windows Service with remoting Pin
a_edwill1-Mar-05 14:28
a_edwill1-Mar-05 14:28 
GeneralRe: Simple example ZIP of Windows Service with remoting Pin
Scott Serl1-Mar-05 19:02
Scott Serl1-Mar-05 19:02 
GeneralRe: Simple example ZIP of Windows Service with remoting Pin
a_edwill2-Mar-05 7:36
a_edwill2-Mar-05 7:36 
GeneralRe: Simple example ZIP of Windows Service with remoting Pin
Scott Serl4-Mar-05 8:28
Scott Serl4-Mar-05 8:28 
GeneralMDI and moveable (custom) control painting Pin
pacharakeng1-Mar-05 12:13
pacharakeng1-Mar-05 12: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.