02-27-2009 07:20 AM
Hello,
I'm testing porting a library into NI CVI 9.0 and I've just built a very simple DLL to exercise one function from WinSock on XP SP3. When I run the executable from the command line, gethostbyname() returns correct data. When I'm debugging using the CVI 9.0 IDE, gethostbyname() returns -1.
I'm not sure where to start as I'm not sure why there should be a difference.
This is compiled as a DLL:
#include <winsock.h>
#define LIBAPI __declspec(dllexport)
LIBAPI int WINAPI SYScurrentHost(char *buff, size_t buflen) {
result = gethostname(buff, buflen);
if (result) return result;
return strlen(buff);
}
The main() that calls SYScurrentHost when run (as a console application) from the console returns the actual host name. But when I put a breakpoint in SYScurrentHost(), result is -1 indicating an error. The LIB file for wsock32.lib is found from "C:\Program Files\National Instruments\CVI90\sdk\lib\msvc\wsock32.lib" which I had to add manually (somehow I have the feeling that this shouldn't be needed, but I had linker errors without it).
Thanks,
Jason.
Solved! Go to Solution.
02-27-2009 11:11 AM
Before making any calls to Winsock, you must initialize the library by calling WSAStartup. You'll notice that if you call WSAGetLastError after gethostname returns -1, it returns WSANOTINITIALISED. It appears that creating a command window must automatically initialize Winsock, though I could not find any documentation to back this up.
Mert A.
National Instruments