LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

GetTCPHostName

Does anybody know, why I just get "#Local Host" on my PXI RT-System using the function GetTCPHostName from the TCP Support Library in Labwindows/CVI 8.0. On my desktop station running WindowsXP the function works well, but on the RT-System running the PharLab OS at an embedded controller the result is as above mentioned.
Of course the machine name of the RT-System isn't set to "#Local Host" in the Measurement & Automation Explorer and also not in the "ni-rt.ini" file.
0 Kudos
Message 1 of 7
(4,784 Views)
I've verified the problem and reported this to the developers. There is a potencial workaround. If you can post your email address, we can discuss the solution further.

Thanks
Bilal Durrani
NI
Message 2 of 7
(4,750 Views)

I am not exactly sure if this is related to static versus dynamic IP addresses or something about how the DHCP server specified the host name. In either case, you can workaround it by getting the IP address using GetTCPHostAddr and then using the winsock functions inet_addr and gethostbyaddr to get the host name from the IP address. To call winsock functions you would normally need to link with the winsock library which is only available with the CVI Full SDK - but it is also possible to dynamically load the winsock DLL and call these functions. The following example shows this:

#include <windows.h>

#define inet_addr inet_addr_winsock
#define gethostbyaddr gethostbyaddr_winsock
#include <winsock.h>
#undef inet_addr
#undef gethostbyaddr

#include <tcpsupp.h>
#include <cvirte.h>
#include <ansi_c.h>

unsigned long (__stdcall * inet_addr)(const char *cp);
struct hostent * (__stdcall * gethostbyaddr)(const char *addr, int len, int type);

static BOOL LoadWinSockFuncs(void)
{
    HMODULE wsock32 = NULL;
 
 wsock32 = LoadLibrary("wsock32.dll");
 if (wsock32 == NULL)
  return FALSE;
 
 inet_addr = (void *)GetProcAddress(wsock32, "inet_addr");
 gethostbyaddr = (void *)GetProcAddress(wsock32, "gethostbyaddr");
 if (inet_addr == NULL || gethostbyaddr == NULL)
 {
  FreeLibrary(wsock32);
  return FALSE;
 }
 
 return TRUE;
}

void CVIFUNC_C RTmain (void)
{
 char host[512];
 int error;
 HOSTENT *hostent;
 unsigned int addr;
 
 if (InitCVIRTE (0, 0, 0) == 0)
  return;
 
 error = GetTCPHostName(host, sizeof(host)); // may return "#Local Host"
 error = GetTCPHostAddr(host, sizeof(host)); // returns the IP address
 if (!LoadWinSockFuncs())
  assert(0);
 addr = inet_addr(host);
 hostent = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
 // now, hostent->h_name contains the name of the host
 
 CloseCVIRTE ();
}

Message 3 of 7
(4,709 Views)
I tried and didn't work in the controller but ok when I tried it on a Windows box. The hostent is always NULL when running in the controller.
0 Kudos
Message 4 of 7
(4,644 Views)
Does GetTCPHostAddr return the system's IP address?
0 Kudos
Message 5 of 7
(4,615 Views)
Yes.  I always get the ip address from GetTCPHostAddr() and I also verified that the returned from inet_addr() is also the target ip address ( in network byte order). I couldn't use WSAGetLastError() to track the error information from gethostbyaddr() because the ETS doesn't support winsock2.
0 Kudos
Message 6 of 7
(4,611 Views)
Acqiris

Could you tell us which controller you are using and what versions of software you have installed on the target system? Also, is the controller showing up in MAX with valid network parameters?

Try using this attached project. QueryController() should return info about the controller if you run it on the controller and specific "localhost" as the ip address. You can also use this function to ping a remote RT system for its info.

Let me know if this works for you.
Bilal Durrani
NI
0 Kudos
Message 7 of 7
(4,583 Views)