ahh thanks, only problem now is that my whole app is unicode and its asking for alot of things and alot of the things like buttons and all that are not loading...ill go throu and make everything TCHAR and see if this helps , thanks
ahh thanks, only problem now is that my whole app is unicode and its asking for alot of things and alot of the things like buttons and all that are not loading...ill go throu and make everything TCHAR and see if this helps , thanks
ok im not sure whats happening but i tried this
it didnt look like it would work , should it?Code:TCHAR re[128] = "RICHED20.DLL"; LoadLibrary(re);
and if not is there something i should do so i dont have to go throu everything and do it like that.cheers
What you really need to do is go through your code line by line. No exceptions, no shortcuts, no compiling to see if it magically works now.
Every datatype that was char or any pointer variation of it needs to be TCHAR.
Every string literal has to be enclosed in the TEXT or _T macro. No more "text", only _T( "text" ). Same goes for characters. No more 'c'. Only _T( 'c' ).
Every function handling a string literal or character needs to be changed. If it is a library functions, there will be another function for unicode. It probably begins with _t. Look into the MSDN. Replace these functions.
Get rid of any calculation assuming char has a sizeof 1. Get rid of any calculation assuming an array can hold as many characters as it's size.
Do this line by line. I cannot stress this enough, no exceptions, no shortcuts, no compiling to see if it magically works now.
If you did this, then your application will be ANSI and UNICODE compatible depending on your UNICODE/_UNICODE flags while compiling.
Anything else will just result in a lot of headaches.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Code:HMODULE hModule = LoadLibrary( _T("RICHED20.DLL") ); if( hModule == NULL ) { // error, call GetLastError to get information }
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
ok it all seems good its now showing both ANSI and unicode full but theres 1 problem it wont resolve the host name/ip on the client..heres the code for it
Code:int ConnectAndSend(){ unsigned short port = 9980; int retval = 0; struct sockaddr_in server; WSADATA wsaData; SOCKET conns; struct hostent *hp; unsigned int addr; TCHAR server_name[50]; retval = WSAStartup(0x202, &wsaData); if (retval != 0){ AppendWindowText(hwndRichEdit, _T("ERROR: Cannot load winsock DLL.\r\n")); return 1; } AppendWindowText(hwndRichEdit, _T("> Winsock DLL loaded.\r\n")); SendMessage(hwndIPEdit, WM_GETTEXT, (WPARAM)ARRAYSIZE(server_name), (LPARAM)server_name); if (isalpha(server_name[0])) { hp = gethostbyname(server_name); }else{ addr = inet_addr(server_name); hp = gethostbyaddr((TCHAR*)&addr, 4, AF_INET); } if (hp == NULL){ AppendWindowText(hwndRichEdit, _T("Can't resolve IP/Hostname. Please make sure it is correct.\r\n")); WSACleanup(); return 1; }
Last edited by Rare177; 06-02-2004 at 04:01 AM.
Unfortunately, there are a few functions where a TCHAR version does not exist. Some of these functions only take unicode strings while others such as gethostbyname() and inet_addr() only take ansi strings. Therefore, you need to special case these functions. First, you need a good TCHAR to ansi conversion function:
Then you can use it:Code:BOOL TToAnsi(LPCTSTR szOriginal, LPSTR szAnsi, int cchAnsi) { #ifdef UNICODE BOOL bRet = (0 != WideCharToMultiByte(CP_ACP, 0, szOriginal, -1, szAnsi, cchAnsi, NULL, NULL)); #else BOOL bRet = (NULL != lstrcpyn(szAnsi, szOriginal, cchAnsi)); #endif if (!bRet && cchAnsi > 0) szAnsi[0] = '\0'; return bRet; }
The other alternative is to specifically use SendMessageA() to get an ansi string, but I think the conversion method can be cleaner.Code:if (SendMessage(hwndIPEdit, WM_GETTEXT, (WPARAM)ARRAYSIZE(server_name), (LPARAM)server_name)) { CHAR server_nameA[50]; // Convert down to ansi... TToAnsi(server_name, server_nameA, ARRAYSIZE(server_nameA)); // Now use the A version where needed... if (isalpha(server_nameA[0])) { hp = gethostbyname(server_nameA); } else { addr = inet_addr(server_nameA); hp = gethostbyaddr((TCHAR*)&addr, 4, AF_INET); } }
It should be noted that there is potential loss of information whenever unicode text is converted to ansi so this should be avoided when at all possible.
P.S Please consider posting the compiler warning/error messages when you run into a problem.
Last edited by anonytmouse; 06-02-2004 at 04:24 AM.
ok thanks
got a few erros thou
assignment of pointer to const char to pointer to unsigned shortCode:hp = gethostbyaddr((TCHAR*)&addr, 4, AF_INET);
assignment of pointer to const char to pointer to unsigned shortCode:send(conns, SendBuffer, sizeof(SendBuffer), 0);
possible usage of conns before definitionCode:SOCKET conns;
Both of these functions take a const char * to signify that they take an array of bytes(rather than a string). Therefore, it is safe to cast:Originally Posted by Rare177
Code:hp = gethostbyaddr((const char *) &addr, 4, AF_INET); send(conns, (const char *) SendBuffer, sizeof(SendBuffer), 0);Not sure what is going on here. Can you post the code where conns is actually used in the ConnectAndSend() function.possible usage of conns before definitionCode:SOCKET conns;
Code:int ConnectAndSend(){ unsigned short port = 9980; int retval = 0; struct sockaddr_in server; WSADATA wsaData; SOCKET conns; struct hostent *hp; unsigned int addr; TCHAR server_name[50]; retval = WSAStartup(0x202, &wsaData); if (retval != 0){ AppendWindowText(hwndRichEdit, _T("ERROR: Cannot load winsock DLL.\r\n")); return 1; } AppendWindowText(hwndRichEdit, _T("> Winsock DLL loaded.\r\n")); SendMessage(hwndIPEdit, WM_GETTEXT, (WPARAM)ARRAYSIZE(server_name), (LPARAM)server_name); if (SendMessage(hwndIPEdit, WM_GETTEXT, (WPARAM)ARRAYSIZE(server_name), (LPARAM)server_name)) { CHAR server_nameA[50]; // Convert down to ansi... TToAnsi(server_name, server_nameA, ARRAYSIZE(server_nameA)); // Now use the A version where needed... if (isalpha(server_nameA[0])) { hp = gethostbyname(server_nameA); } else { addr = inet_addr(server_nameA); hp = gethostbyaddr((const char *) &addr, 4, AF_INET); } } if (hp == NULL){ AppendWindowText(hwndRichEdit, _T("Can't resolve IP/Hostname. Please make sure it is correct.\r\n")); WSACleanup(); return 1; } AppendWindowText(hwndRichEdit, _T("> Socket created.\r\n")); if (connect(conns, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR){ AppendWindowText(hwndRichEdit, _T("ERROR: Cannot make remote connection.\r\n")); WSACleanup(); return 1; } AppendWindowText(hwndRichEdit, _T("> Connected to ARCA server ...\r\n")); send(conns, (const char *) SendBuffer, sizeof(SendBuffer), 0); closesocket(conns); WSACleanup(); AppendWindowText(hwndRichEdit, _T("\r\n> Command sent successfully.\r\n\r\n")); SendMessage(hwndReasonEdit, WM_SETTEXT, (WPARAM)0, (LPARAM)TEXT("")); SendMessage(hwndRemoteNameEdit, WM_SETTEXT, (WPARAM)0, (LPARAM)TEXT("")); return 0; }
You need a:
in there. Otherwise you pass a random uninitialised value to connect(), hence the warning. As far as I can see, you have not initialized server either.Code:conns = socket(...);
thanks! got that working
now theres 1 more thing
the server is not accepting the data but other than that the client is free of errors
assignment of pointer to const char to pointer to unsigned shortCode:retval = recv(accepts, recv_buffer, ARRAYSIZE(recv_buffer), 0);
Same as send() above. recv() expects a byte array, so cast to char *. Also, don't use ARRAYSIZE() here, as we need to pass the size of the buffer in bytes and not characters. So use sizeof(). I know, confusing!
I'm off for now.
thank you soooo much i would probly be in a mental hospital if it wasnt for all of you help
legends.