-
Displaying chat messages
hi all,
i have figure out how to display chat messages in a edit box BUT the buffer that i use gets full and then when ever somert is put in the buffer it over writes what is at the end i need to know how to increase the size of the buffer so it can hold the new messages with out a buffer limit so i can carry on writing messages to the screen in my chat prog .. like msn;)
void OutputTextToScreen(char *p,int error)
{
GetWindowText(hMessage, szNewBuffer, 100);
strcat(szNewBuffer, p);
if((error != -1 ) || (error != SOCKET_ERROR)){
strcat(szNewBuffer, "OK\r\n");
}else{
strcat(szNewBuffer, "Failed\r\n");
}
SetWindowText(hMessage, szNewBuffer);
}
-
Code:
did this with [ code ] and [ /code ] without the spaces.
-
-
Maybe a edit is the wrong control (it is the easiest to use and most common).
A listview (or Listbox) could have each line of chat added as an item, retrieved with one of the many Listview_ series of macros, eliminating the need to GlobalRealloc() (or LocalRealloc() ) the buffer.
-
ermm.....
hi every time i try to run my progy its crashs every time i try to use the function - OutputTextToLog
please tell me if i am doing anything wrong this is the code :-
Code:
void OutputTextToLog(char *p,int error)
{
len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGEAREA));
GetWindowText(hMessage, szNewBuffer, 256);
char* szBuffer2;
realloc(szBuffer2, len+256);
strcat(szBuffer2,szNewBuffer);
strcat(szBuffer2,p);
if((error != -1 ) || (error != SOCKET_ERROR)){
strcat(szBuffer2, "OK\r\n");
}else{
strcat(szBuffer2, "Failed\r\n");
}
SetWindowText(hMessage, szBuffer2);
}
thx in advance
-
Is hMessage the same as GetDlgItem(hwnd, IDC_MESSAGEAREA)? If it is, try this:
Code:
len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MESSAGEAREA));
GetWindowText(hMessage, szNewBuffer, 256);
replaced by:
len = GetDlgItemText(hwnd, IDC_MESSAGEAREA, szNewBuffer, 256);
Note that GetWindowTextLength returns the size of the text, while GetDlgItemText() returns the number of characters copied to szNewBuffer. This probably won't solve any problems, but it's neater and easier to understand (assuming that this is what you really wanted to do, of course :D).
You can also do this:
Code:
SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);
Code:
szBuffer2 = realloc(szBuffer2, len+256); //<-------
strcat(szBuffer2,szNewBuffer);
strcat(szBuffer2,p);
if((error != -1 ) || (error != SOCKET_ERROR)){
strcat(szBuffer2, "OK\r\n");
}else{
strcat(szBuffer2, "Failed\r\n");
}
You are assuming that p is at max 247 characters?
-
dam...
right what u told me did clear things up BUT still it continues to crash i think i have found the prob its not the realloc its the
Code:
strcat(szBuffer2,szNewBuffer);
because it doesnt get to the second messagebox before it crashs if wont let me put any thing in the szBuffer2 and heres the new code -
Code:
int nMessageLen;
int nPLen;
char szNewBuffer[1024];
char szBuffer[100] = "Ufo Text Messenger v1.00\r\n\r\n";
char* szBuffer2;
void OutputTextToLog(char *p,int error)
{
nMessageLen = GetDlgItemText(hwnd, IDC_MESSAGEAREA, szNewBuffer, 1024);
nPLen = strlen(p);
realloc(szBuffer2, nMessageLen+nPLen+10);
MessageBox(0,"1",0,0);
strcat(szBuffer2,szNewBuffer);
MessageBox(0,"2",0,0);
strcat(szBuffer2,p);
if((error != -1 ) || (error != SOCKET_ERROR)){
strcat(szBuffer2, "OK\r\n");
}else{
strcat(szBuffer2, "Failed\r\n");
}
SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);
}
PLEASE HELP THIS HAS BEEN DRIVING ME MAD!!!!!!!!!!!! MAD!!!!!!!!!!!! AND ITS SLOWING ME DOWN
thx in advance
-
It gave you an illegal operation, right? If it did, did you click on details? I think the problem was realloc(). strcat() shouldn't crash unless you didn't reserve enough memory or something... try this instead:
Code:
szBuffer2 = (char*)malloc(nMessageLen + nPLen + 10);
//other stuff
SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2);
free((void*)szBuffer2); //you might not need the typecast
I really have no idea if it will work (I use C++, so I use new and delete instead of malloc() and free()), but you can give it a try. If it still doesn't work, try replacing "malloc(nMessageLen + nPLen + 10);" with "malloc(2000)". If it works then, then you know that there's something wrong with "nMessageLen + nPLen + 10", and work on the problem from there.
Good luck!
-
Oh, I totally forgot: You can use a std::string. I'll mark the changes with comments.
PHP Code:
//int nMessageLen; //<----
//int nPLen; //<----
char szNewBuffer[1024];
char szBuffer[100] = "Ufo Text Messenger v1.00\r\n\r\n";
std::string szBuffer2; //or rename it if you want
void OutputTextToLog(char *p,int error)
{
GetDlgItemText(hwnd, IDC_MESSAGEAREA, szNewBuffer, 1024); //<----
//nPLen = strlen(p); //<----
//realloc(szBuffer2, nMessageLen+nPLen+10); //<----
MessageBox(0,"1",0,0);
szBuffer2.append(szNewBuffer); //<----
MessageBox(0,"2",0,0);
szBuffer2.append(p); //<----
if((error != -1 ) || (error != SOCKET_ERROR)){
szBuffer2.append("OK\r\n"); //<----
}else{
szBuffer2.append("Failed\r\n"); //<----
}
SetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2.c_str()); //<----
}
szBuffer2 is not a c-style string (i.e. isn't a char*), so in order to use it in SetDlgItemText(), you have to call its c_str() function. But remember, you CANNOT use the code below, because c_str() returns a constant string.
PHP Code:
GetDlgItemText(hwnd, IDC_MESSAGEAREA, szBuffer2.c_str(), 1024);
Well, good luck!
-
YAAHOO!!!!!!!
THANKS ALOT hunter YOUR DA MAN
THIS GUY KNOWS HIS STUFF IT WORKED FIRST TIME HAHAHAHAHAHA!!!!!!!!!!!:D :D :D :D :D :D :D :D :D
-