| View previous topic :: View next topic |
| Author |
Message |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sun Apr 12, 2009 5:53 pm Post subject: CreateThread with class function |
|
|
I'm trying to create a thread with a function from a class, but it's not working. It crashes the moment I call CreateThread.
| Code: |
/* main.cpp */
#include "nIRC.h"
int __cdecl main(void)
{
client c;
c.attach();
/* Receive until the peer closes the connection */
HANDLE ThreadHandle;
DWORD thread;
ThreadHandle = CreateThread(NULL, NULL, &c.recvMsg, NULL, NULL, &thread);
while(GetAsyncKeyState(VK_ESCAPE) == 0)
{
/* Send buffer */
c.sendMsg();
}
return c.detach();
}
/* nIRC.h */
...
DWORD WINAPI client::recvMsg()
{
console = GetStdHandle(STD_OUTPUT_HANDLE);
res = recv(nIRC, buffer, (int)strlen(buffer), 0);
if(res > 0)
{
buffer[res] = '\0';
printf("Server: ");
WriteConsole(console, buffer, (DWORD)strlen(buffer), NULL, NULL);
memset(buffer, 0, sizeof(buffer));
}
return 0;
}
|
When I compile this code, I get a 'illegal operation on bound member function expression.' If I remove it, then it tells me to put the "&" there.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Apr 12, 2009 6:05 pm Post subject: |
|
|
I could never ever get this to work myself, instead I just created a separate function with the parameter to a pointer to the class and just used
| Code: | | CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Function, this, 0, &dwThreadId); |
_________________
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Apr 12, 2009 6:06 pm Post subject: |
|
|
Fill out the rest of the class with whatever you need.
| Code: | class Client {
static DWORD WINAPI _ClientThreadEntry(Client *me) {
me->_Recv();
return 0;
}
void _Recv() {
console = GetStdHandle(STD_OUTPUT_HANDLE);
res = recv(nIRC, buffer, (int)strlen(buffer), 0);
if(res > 0) {
buffer[res] = '\0';
printf("Server: ");
WriteConsole(console, buffer, (DWORD)strlen(buffer), NULL, NULL);
memset(buffer, 0, sizeof(buffer));
}
}
public:
void Recv() {
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)_ClientThreadEntry, this, 0, 0);
}
}; |
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sun Apr 12, 2009 6:07 pm Post subject: |
|
|
I'll try it out and let you know how it goes. I wonder if someone could actually solve this mystery though.
Edit:
Thanks Flyte.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Apr 12, 2009 6:08 pm Post subject: |
|
|
| nwongfeiying wrote: | | I'll try it out and let you know how it goes. I wonder if someone could actually solve this mystery though. |
Detective Flyte already solved this mystery.
| nwongfeiying wrote: | | Flyte, what if I don't want my function to be static? |
It's only a gateway, it doesn't matter if it's static.
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sun Apr 12, 2009 6:14 pm Post subject: |
|
|
I just realized that right when I saw the me->_recv(). Thank you, detective Flyte.
Edit:
I ran into the same problem again
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Sun Apr 12, 2009 8:30 pm Post subject: |
|
|
this not work? (guessing)
| Code: |
void recvMsg(client c) {
c.recvMsg();
}
void main() {
client c;
c.attach();
CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)&recvMsg(c), NULL, 0, NULL);
}
|
_________________
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Sun Apr 12, 2009 10:42 pm Post subject: |
|
|
| pkedpker wrote: | this not work? (guessing)
| Code: |
void recvMsg(client c) {
c.recvMsg();
}
void main() {
client c;
c.attach();
CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)&recvMsg(c), NULL, 0, NULL);
}
|
|
...what? There was no logic to what you have posted. And Flyte, thanks for your help. I got it to work, but if you could contact me one more time, I have a final question to ask you (hopefully final) :]
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Mon Apr 13, 2009 6:25 am Post subject: |
|
|
| Code: | SecureInt *INT = new SecureInt(rand()%999+1); // Create object and 1st value
SetConsoleTitle(TEXT("SecureInt Demonstration v3"));
sec->CreateFirstHash();
void* Addr = &INT->c_int; |
nwongfeiying, I think intel's compiler lets you do stuff like this. You might want to try it =]
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Mon Apr 13, 2009 11:32 am Post subject: |
|
|
Thanks Noz3001, but the only difference I'm seeing in using the CreateThread is a faster processing. Though that is good, what I'm aiming for is an asynchronous recv(), but it's not working out.
The only idea left I got is to make a timer and set the recv() at regular intervals. I'm going to have to make a GUI or IPC afterall since it's not possible to have two console windows for one process D:
Last edited by nwongfeiying on Mon Apr 13, 2009 11:40 am; edited 1 time in total |
|
| Back to top |
|
 |
sphere90 Grandmaster Cheater
Reputation: 0
Joined: 24 Jun 2006 Posts: 912
|
Posted: Mon Apr 13, 2009 11:35 am Post subject: |
|
|
| nwongfeiying wrote: | Thanks Noz3001, but the only difference I'm seeing in using the CreateThread is a faster processing. Thought that is good, what I'm aiming for is an asynchronous recv(), but it's not working out.
The only idea left I got is to make a timer and set the recv() at regular intervals. I'm going to have to make a GUI or IPC afterall since it's not possible to have two console windows for one process D: |
If you want async recv, try IO Completion Port + WSARecv.
_________________
Give a hungry man a fish and he'll be full for a day. Teach a hungry man how to fish and he'll be full for the rest of his life. |
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Mon Apr 13, 2009 12:11 pm Post subject: |
|
|
| nwongfeiying wrote: | | pkedpker wrote: | this not work? (guessing)
| Code: |
void recvMsg(client c) {
c.recvMsg();
}
void main() {
client c;
c.attach();
CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)&recvMsg(c), NULL, 0, NULL);
}
|
|
...what? There was no logic to what you have posted. And Flyte, thanks for your help. I got it to work, but if you could contact me one more time, I have a final question to ask you (hopefully final) :] |
seems perfect logic to me you might be not seeing it as I see it.
I'll rename it a bit to make it easier to understand
| Code: |
void NewRecvMsg(client c) {
c.recvMsg();
}
void main() {
client c;
c.attach();
CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)&NewRecvMsg(c), NULL, 0, NULL);
}
|
but I can't test it too much work.
_________________
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Mon Apr 13, 2009 4:36 pm Post subject: |
|
|
| You should check your work and leave the tutoring to those who know what they're talking about.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Mon Apr 13, 2009 5:17 pm Post subject: |
|
|
| Well intels compiler lets you use &class.func to get its address so Microsofts might be able to.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Apr 13, 2009 6:20 pm Post subject: |
|
|
| davros wrote: | | Well intels compiler lets you use &class.func to get its address so Microsofts might be able to. |
This must be some kind of compiler voodoo.
CreateThread expects a C style pointer, not a pointer to a member function...
Under MSVC you'll have to use a static member function, or I guess pass the this pointer.
|
|
| Back to top |
|
 |
|