Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


CreateThread with class function
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sun Apr 12, 2009 5:53 pm    Post subject: CreateThread with class function Reply with quote

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
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun Apr 12, 2009 6:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sun Apr 12, 2009 6:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sun Apr 12, 2009 6:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sun Apr 12, 2009 6:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sun Apr 12, 2009 6:14 pm    Post subject: Reply with quote

I just realized that right when I saw the me->_recv(). Thank you, detective Flyte.

Edit:
I ran into the same problem again Sad
Back to top
View user's profile Send private message
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Sun Apr 12, 2009 8:30 pm    Post subject: Reply with quote

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); 
}


_________________
Hacks I made for kongregate.
Kongregate Universal Badge Hack: http://forum.cheatengine.org/viewtopic.php?p=4129411
Kongreate Auto Rating/Voter hack: http://forum.cheatengine.org/viewtopic.php?t=263576
Took a test lol
Back to top
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sun Apr 12, 2009 10:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Mon Apr 13, 2009 6:25 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Mon Apr 13, 2009 11:32 am    Post subject: Reply with quote

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
View user's profile Send private message
sphere90
Grandmaster Cheater
Reputation: 0

Joined: 24 Jun 2006
Posts: 912

PostPosted: Mon Apr 13, 2009 11:35 am    Post subject: Reply with quote

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
View user's profile Send private message
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Mon Apr 13, 2009 12:11 pm    Post subject: Reply with quote

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.

_________________
Hacks I made for kongregate.
Kongregate Universal Badge Hack: http://forum.cheatengine.org/viewtopic.php?p=4129411
Kongreate Auto Rating/Voter hack: http://forum.cheatengine.org/viewtopic.php?t=263576
Took a test lol
Back to top
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Mon Apr 13, 2009 4:36 pm    Post subject: Reply with quote

You should check your work and leave the tutoring to those who know what they're talking about.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Mon Apr 13, 2009 5:17 pm    Post subject: Reply with quote

Well intels compiler lets you use &class.func to get its address so Microsofts might be able to.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Apr 13, 2009 6:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites