WSAStartup() causing unhandled exception

Hi, I'm trying to get a sample code to run from Beej's guide. I added the WSAStartup from MSDN, but it's giving me undhandled exception every time I run the code, why?
(excact location is at err = WSAStartup(wVersionRequested, &wsaData);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma comment(lib, "ws2_32.lib") 
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <Ws2tcpip.h>




int main(int argc, char *argv[])
{
    WSADATA wsaData;   // if this doesn't work
    //WSAData wsaData; // then try this instead
    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
    if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
     fprintf(stderr, "WSAStartup failed.\n");
     exit(1);
    }
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];
    if (argc != 2) {
        fprintf(stderr,"usage: showip hostname\n");
        return 1;
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;	
   if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
       fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
       return 2;
   }
   printf("IP addresses for %s:\n\n", argv[1]);
   for(p = res;p != NULL; p = p->ai_next) {
       void *addr;
       char *ipver;
       // get the pointer to the address itself,
       // different fields in IPv4 and IPv6:
       if (p->ai_family == AF_INET) { // IPv4
           struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
           addr = &(ipv4->sin_addr);
           ipver = "IPv4";
       } else { // IPv6
           struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
           addr = &(ipv6->sin6_addr);
           ipver = "IPv6";
       }
       // convert the IP to a string and print it:
       inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
       printf("  %s: %s\n", ipver, ipstr);
   }
   freeaddrinfo(res); // free the linked list
   return 0;
}
Last edited on
This is wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void *addr;
char *ipver;    // should be const char *
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
    addr = &(ipv4->sin_addr);
    ipver = "IPv4";
} else { // IPv6
    struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
    addr = &(ipv6->sin6_addr);
    ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);

Thank you for the answer, but this doesn't solve the exception problem.
WSAStartup still generates unhandled exception.
Unhandled exception at 0x76f615de (ntdll.dll) in test.exe: 0xC0000005: Access violation.

Are you sure it's WSAStartup that's causing the problem? How do you know?
Original post:
Not entirely sure, but if I remove it, there are no exceptions. But then again, I won't be able to do anything.Visual studio says "next statement to execute when this thread returns from the current function" and it's the WSAStartup.

What else could it be?

EDIT: It wasn't the WSAStartup after all. I reseted my Visual Basic c++ 2010 settings, loaded symbols again Debug->Options and settings->Debugging->Symbols and it worked(I'll edit the original code too).
Oh, and to run the code you'll have to run it from cmd, see here -> http://crasseux.com/books/ctutorial/argc-and-argv.html

(for googlers, I hate threads where the OP finds the solution and never answers)


Thank you kbw!
Last edited on
Topic archived. No new replies allowed.