LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

winsock gethostbyname() behaves different when debugging

Solved!
Go to solution

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.

0 Kudos
Message 1 of 2
(3,430 Views)
Solution
Accepted by jcurl

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

0 Kudos
Message 2 of 2
(3,419 Views)