• Welcome to Valhalla Legends Archive.
 

LPVOID help w/ CreateThread

Started by Eli_1, June 20, 2004, 04:19 PM

Previous topic - Next topic

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:

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



The receive thread:

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



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...


Why is that number so big? It shouldn't change should it?

Zeller

#1
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

Eli_1

#2
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.  :'(

Adron

Just typecast index to (LPVOID) and pass it directly, you don't have to pass a pointer argument.

Arta

Slightly OT: What's the point in loading multiple chat bots when you can only be connected with one at a time?

Eli_1

Quote from: Arta[vL] on June 21, 2004, 04:30 PM
Slightly OT: What's the point in loading multiple chat bots when you can only be connected with one at a time?

It's not for the battle.net servers.