#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    WSAStartup() error?!!! ( basic winsock application )


    Hello,

    I was creating a basic winsock application when strange error message appeared ( while initializing winsock ) .. here's the code:
    Code:
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <stdio.h>
    
    #pragma comment(lib, "Ws2_32.lib")
    
    int main(void)
    {
    	WSADATA wsadata;
    	int iresult = WSAStartup(MAKEWORD(2, 2), &wsadata);
    	if(iresult != 0) printf("WSASTARTUP faild: %d\n", iresult);
    	else printf("WSAStartup() Call succeeded\n");
    	
    	if(LOBYTE(wsadata.wVersion != 2) || HIBYTE(wsadata.wVersion != 2))
    	{
    		printf("Couldn't find the winsock.dll file!!!\n");
    		WSACleanup();
    	}
    	else printf("Found the required winsock.dll file\n");
    	WSACleanup();
    	return 0;
    }
    And here's the output:
    Code:
    WSAStartup() Call succeeded
    Couldn't find the winsock.dll file!!!
    so I added the line:
    Code:
    printf("low byte: %d, High byte: %d\n", LOBYTE(wsadata.wVersion), HIBYTE(wsadata.wVersion));
    in the second if clause
    and found the strange output:
    Code:
    WSAStartup() Call succeeded
    Couldn't find the winsock.dll file!!!
    low byte: 2, High byte: 2
    so what?
    Am I missing something?

    thanks in advance ..
    Note:
    OS Name: Microsoft Windows 7 Home Premium
    OS Version: 6.1.7600 N/A Build 7600
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    Well, the logic is not what I would use, but it is the exact same logic that is on MSDN so I would be shocked if it turned out to be faulty. We have a couple of experienced Windows socket programmers, hopefully they will weight in soon.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    Stupid question: what version is your winsock.dll?
    Even stupider question: Does your program really depend so absolutely on that one specific version of winsock.dll?

    PS
    I jotted off that response from home over breakfast, as I was rushing to get ready to leave for work. Now that I'm at work, I was able to look up the documentation for WSAStartup on MSDN (IOW, RTFM!); at http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx:
    Originally Posted by MSDN: WSAStartup function
    In order to support various Windows Sockets implementations and applications that can have functional differences from the latest version of Windows Sockets specification, a negotiation takes place in WSAStartup. The caller of WSAStartup passes in the wVersionRequested parameter the highest version of the Windows Sockets specification that the application supports. The Winsock DLL indicates the highest version of the Windows Sockets specification that it can support in its response. The Winsock DLL also replies with version of the Windows Sockets specification that it expects the caller to use.

    When an application or DLL calls the WSAStartup function, the Winsock DLL examines the version of the Windows Sockets specification requested by the application passed in the wVersionRequested parameter. If the version requested by the application is equal to or higher than the lowest version supported by the Winsock DLL, the call succeeds and the Winsock DLL returns detailed information in the WSADATA structure pointed to by the lpWSAData parameter. The wHighVersion member of the WSADATA structure indicates the highest version of the Windows Sockets specification that the Winsock DLL supports. The wVersion member of the WSADATA structure indicates the version of the Windows Sockets specification that the Winsock DLL expects the caller to use.

    If the wVersion member of the WSADATA structure is unacceptable to the caller, the application or DLL should call WSACleanup to release the Winsock DLL resources and fail to initialize the Winsock application. In order to support this application or DLL, it will be necessary to search for an updated version of the Winsock DLL to install on the platform.

    The current version of the Windows Sockets specification is version 2.2. The current Winsock DLL, Ws2_32.dll, supports applications that request any of the following versions of Windows Sockets specification:

    1.0
    1.1
    2.0
    2.1
    2.2
    IOW, WSAStartup did indeed successfully load the DLL, but then you arbitrarily rejected it because it did not match the version you wanted. Well, now thanks to somebody else's we find that your parentheses were misplaced, but still I don't see the reason for you to reject a successful loading of the winsock DLL unless it were really necessary.
    Last edited by dwise1_aol; January 31st, 2012 at 12:05 PM.
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,276
    Rep Power
    4193
    Originally Posted by jokes_finder
    Hello,
    Code:
    	if(LOBYTE(wsadata.wVersion != 2) || HIBYTE(wsadata.wVersion != 2))
    Your parenthesis are in the wrong places. Should be:

    Code:
    if(LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
    As mentioned though, unless there is some reason you must have that specific version it is not necessary to make that detailed of a check. WSAStartup's success/failure check is enough.

    Comments on this post

    • mitakeet agrees : Man do I feel like an idiot for missing that!
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0
    @ dwise1_aol:
    what version is your winsock.dll?
    sorry, but may u tell me how?
    a quick google search wasn't of much use

    Does your program really depend so absolutely on that one specific version of winsock.dll?
    well, I don't think so ..
    I started learning about sockets and netwoek programming just 2 days ago. That is, I'm still a beginner. ( so I need some recommendations if u plz :) [books, tutorials, pages, ..] )

    the application I want is just sending mails. I don't think it depends on specific version .. am i right?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0
    Originally Posted by kicken
    Your parenthesis are in the wrong places. Should be:

    Code:
    if(LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
    As mentioned though, unless there is some reason you must have that specific version it is not necessary to make that detailed of a check. WSAStartup's success/failure check is enough.
    oops
    things now are working :)
    thanks

    but still I want to know my winsock.dll version :)
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    Originally Posted by jokes_finder
    but still I want to know my winsock.dll version :)
    Actually, what matters is the version of the Winsock specification that it supports. Read the PS of my previous post -- I'd rather not have to recreate it here as well -- for the documentation on WSAStartup.

    PS
    There are two winsock DLLs: winsock.dll and ws2_32.dll. If you examine the hex dump of either file, towards the end you will see the version information, though it's a bit harder to read it in ws2_32.dll, because the text is in Unicode instead of in ASCII.

    On my XP Pro system at work:
    winsock.dll -- 3.10
    ws2_32.dll -- 5.1.2600.5512 D

    FWIW.
    Last edited by dwise1_aol; January 31st, 2012 at 12:29 PM.
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,489
    Rep Power
    1876
    I see you haven't learnt the lesson about cross-posting from your other threads....
    Hey, over here!!!

    Get informed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0
    thanks guys :)

    @ Salem: I'm sorry for that. My only reason was "just to be on the safe side". Thanks :)

    Comments on this post

    • ptr2void disagrees : You're not sorry at all. You've done it on every thread you've started. You're just an inconsiderate jerk.

IMN logo majestic logo threadwatch logo seochat tools logo