Valhalla Legends Forums Archive | C/C++ Programming | LPVOID help w/ CreateThread

AuthorMessageTime
Eli_1
I'm making a simple CHAT bot that will load multiple bots. When each bot connects, it creates a thread. For some reason, the second time a thread is created, 'index' ends up being a huge number and the program crashes while trying to typecast it to 'int'.


When the bot connects:
[code]
void bconnected(int index) {
   printf("Connected!\n");
   char login[256];
   sprintf(login, "\3\4%s\r\n%s\r\n/join %s\r\n\0", busername[index], bpassword[index], bhome[index]);
   printf("Logging in!\n");
   send(sckbot[index], login, strlen(login), 0);
   
   printf("Creating receive thread (%d)... ", index);
   recvhandle[index] = CreateThread(NULL, 0, recvthread, &index, 0, &recvret);
   printf("Created!\n");
}
[/code]


The receive thread:
[code]
DWORD WINAPI recvthread(LPVOID index) {
   printf("Entered recvthread.\n");

   char buffer[1024];
   printf("%d\n", index);

   int i = *(int*)index;
   printf("%d\n", i);

   int bytesrecv = 1;
   printf("Variables declared.\n");

   while (bytesrecv > 0) {
      bytesrecv = recv(sckbot[i], buffer, 1024, 0);
      buffer[bytesrecv] = '\0';


      char *message = strtok(buffer, "\r\n");
      while (message != NULL) {
         if (stricmp(message, "LoGiN FaiLeD.") == 0) {
            printf("Failed login!\n");
            bclose(i);
            return 0;
         }
         message = strtok(NULL, "\r\n");
      }


   }
   
   closesocket(sckbot[i]);
   bclose(i);
   return 0;
}
[/code]


Output before it crashes:
[quote]
Connecting... Connected!
Logging in!
Creating receive thread (0)... Created!
Entered recvthread.
6618204
0
Variables declared.
Failed login!
Closing receive thread... Closed!

Disconnected -- Connecting... Connected!
Logging in!
Creating receive thread (0)... Created!
Entered recvthread.
17300252 <--- insert error message and crash here...
[/quote]


Why is that number so big? It shouldn't change should it?
June 20, 2004, 9:19 PM
Zeller
It dousnt look like its crashing when trying to typecast it to int. The reason that number is so big is becous is its an adress (I think you wanted to dereference it).

Edit: I think I found the problem. Index was created in the function bconnected and your sending the adress of index to a different thread. The function bconnect might be returning and disposing 'index' before the new thread reads from its adress. To see if this is the case, try changing the parameters in bconnected to take index by reference
June 20, 2004, 11:09 PM
Eli_1
That was the problem, thanks. I couldn't pass it by refrence though, because that variable gets passed to it by another function that has exited by then, also. I never had to worry about all this stuff when I used VB. :'(
June 21, 2004, 12:08 AM
Adron
Just typecast index to (LPVOID) and pass it directly, you don't have to pass a pointer argument.
June 21, 2004, 8:54 PM
Arta
Slightly OT: What's the point in loading multiple chat bots when you can only be connected with one at a time?
June 21, 2004, 9:30 PM
Eli_1
[quote author=Arta[vL] link=board=30;threadid=7356;start=0#msg66576 date=1087853452]
Slightly OT: What's the point in loading multiple chat bots when you can only be connected with one at a time?
[/quote]

It's not for the battle.net servers.
June 21, 2004, 9:42 PM

Search