Here is a small C program that demonstrates what I have been talking about
and is independent of the VS6 project structure -- this program can easily
be compiled and linked via "cl" on the command line:
#include "windows.h"
struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
size_t ai_addrlen;
char *ai_canonname;
struct sockaddr *ai_addr;
struct addrinfo *ai_next;
};
main()
{
struct addrinfo *info;
struct addrinfo *walk;
char system[128];
unsigned char ip[4];
WSADATA wsaData;
int _stdcall getaddrinfo(const char FAR *nodename,
const char FAR *servname,
const struct addrinfo FAR *hints,
struct addrinfo FAR *FAR *res);
void _stdcall freeaddrinfo(struct addrinfo FAR *ai);
WSAStartup(MAKEWORD(2,0), &wsaData);
gethostname(system, sizeof system);
getaddrinfo(system, NULL, NULL, &info);
walk = info;
while(walk) {
memcpy(ip, &walk->ai_addr->sa_data[2], 4);
printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
walk = walk->ai_next;
}
freeaddrinfo(info);
return 0;
}
I can compile and run this C program on an XP system with the Platform SDK
for XP SP2 and it does the right thing which is to print the system's IP
addresses. For compactness this program omits a lot of error checking and
is not IPv6 compatible. Here are the problems.
1. I needed to provide my own declarations for getaddrinfo, freeaddrinfo,
and addrinfo because I can find no way to include ws2tcpip.h with windows.h
and not get duplicate definition errors. I'd be delighted if you could tell
me what includes can provide the definitions this program needs.
2. I want to include similar code in a VS6 project which means the code must
be compiled as C++. I can find no way to compile and link this code as C++.
3. In the end the code doesn't seem to be usable on anything other than XP
(maybe Win2003 server) because other Windows systems do not have the
function entry points in their ws2_32.dll. The MSDN documentation is
schizophrenic on this point with some versions saying these functions are
supported all the way back to Win9X and some versions saying otherwise (see
previous posts in this thread for examples of that confusion). It would be
nice if there was a redistributable ws2_32.dll to deal with this issue.
I use Microsoft development tools only when I have no choice so I am
probably not nearly as expert as you when it comes to MS development
esoterica. However I do not feel that I am unfairly blaming MS for making a
mess out of the use of these functions. If you can help me with the 3
issues above I'll cheerfully admit to being wrong.
Roger
Post by Eugene GershnikPost by Roger H. LevyThanks but all suggestions have failed. I did get the application to
compile and link through a huge hack. I used extern "C" bracketing
around my function that uses getaddrinfo and freeaddrinfo and I also
used extern "C" to declare the function at the call location. I also
provided my own declarations of getaddrinfo and freeaddrinfo with
_stdcall since the declarations in ws2tcpip.h were not effective.
The program started with these hacks but immediately halted saying
that getaddrinfo could not be found in ws2_32.dll. I suppose it
could be found in the ws2_32.dll from the PDSK but I'm not interested
in redistributing ws2_32.dll with my program. It seems to me that MS
has made a big mess out of this given that everything was documented
without any caveats in the October 2001 MSDN.
Yes, when in doubt blame MS. There are enough things they deserve to be
bashed for but your problem is not one of them.
Your code is obviously broken as code using these functions compiles and
links fine with the same SDK you use. The fact that you made things to
compile and link with some extern "C" additions also indicates that your
code/project settings are broken. And no, you don't need to redistribute
ws2_32.dll from Platform SDK because there is no such thing. MSDN version
has nothing to do with it either.
If you think there is an error with SDK headers or libraries, create a
small application that demonstrates the problem (as I suggested in the
previous post) and post it here.
--
Eugene
http://www.gershnik.com