Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

visual studio 2005 c++ and TCHAR/unicode

Options
  • 08-01-2007 5:41pm
    #1
    Closed Accounts Posts: 1,567 ✭✭✭


    i can't get this code to compile for 64-bit, that isn't really the problem for the first error though.
    #define WIN32_LEAN_AND_MEAN
    
    #define _WIN32_WINNT 0x0502
    
    #include <tchar.h>
    #include <winsock2.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ws2tcpip.h>
    
    #pragma comment (lib, "ws2_32.lib")
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	WSADATA wsa;
    	ADDRINFO aiHints,*aiEntry,*aiList=NULL;
    	SOCKET s;
    
    	if(--argc == 2) {
    		if((WSAStartup(MAKEWORD(2,0),&wsa))==ERROR_SUCCESS) {
    			ZeroMemory(&aiHints,sizeof(aiHints));
    
    			aiHints.ai_family   = AF_INET;
    			aiHints.ai_socktype = SOCK_STREAM;
    			aiHints.ai_protocol = IPPROTO_TCP;
    
    			if((getaddrinfo(argv[1],argv[2],&aiHints,&aiList)) == 0) {
    				for(aiEntry=aiList;aiEntry != NULL;aiEntry=aiEntry->ai_next) {
    					if((s=socket(aiEntry->ai_family,aiEntry->ai_socktype,aiEntry->ai_protocol)) != SOCKET_ERROR) {
    						if((connect(s,aiEntry->ai_addr,aiEntry->ai_addrlen)) != SOCKET_ERROR) {
    							wprintf(L"\nconnected...");
    						} else wprintf(L"\nconnect() failed");
    						closesocket(s);
    					}
    				} freeaddrinfo(aiList);
    			} else wprintf(L"\ngetaddrinfo(%s) failed",argv[1]);
    		}
    	} else wprintf(L"\n\tUsage:%s <ip/host> <port>\n",argv[0]);
    	return 0;
    }
    

    Error	1	error C2664: 'WspiapiGetAddrInfo' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'	d:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\tcp\tcp\tcp.cpp	28	
    Warning	2	warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data	d:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\tcp\tcp\tcp.cpp	31
    

    i don't want to cast argv[1] and argv[2] with (const char*) as these are unicode arguements.UNICODE and _UNICODE are defined in the command line.

    i checked the api WspiapiGetAddrInfo
    #define getaddrinfo             WspiapiGetAddrInfo
    

    ...
    int
    WINAPI
    WspiapiGetAddrInfo(
        IN const char                       *nodename,
        IN const char                       *servname,
        IN const struct addrinfo            *hints,
        OUT struct addrinfo                 **res)
    {
    

    but in the platform SDK documentation its defined like
    int getaddrinfo(
      const TCHAR* nodename,
      const TCHAR* servname,
      const struct addrinfo* hints,
      struct addrinfo** res
    );
    

    by the way, i'm using Windows 2003 R2 SDK, anyone know what i'm doing wrong?


Comments

  • Closed Accounts Posts: 884 ✭✭✭NutJob


    Can i hell figure it out. But then again the only 64bit pc i have access to is in work

    Here may help
    http://www.viva64.com/links.php


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    i was using the MSDN 2005 with version of visual studio 2005 pro edition.
    there must be error in docs i have, because on msdn site,the api is defined as:
    int WSAAPI getaddrinfo(
      const char* nodename,
      const char* servname,
      const struct addrinfo* hints,
      struct addrinfo** res
    );
    

    and it goes onto to say anyone programming unicode should use GetAddrInfoW or GetAddrInfo with UNICODE or _UNICODE defined.

    i just downloaded the sp1 sdk,will try it later and see if that works.
    thanks nutjob.


Advertisement