Crash with hostent data retrieve

D

Devon Reed

I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct.

Any help here? I get a clean compile...

Here's the code

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
char * hostname;

int socket_desc;

socket_desc=socket(AF_INET,SOCK_STREAM,0);
if (socket_desc==-1)
perror("Create socket");

struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, hostname, -1, &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
 
J

John Harrison

Devon Reed said:
I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct.

Any help here? I get a clean compile...

Here's the code


struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;

Taking a wild guess I would say that he is a null pointer.

If that doesn't solve it then take this question to a Windows programming
group like This group is for
standard C++ only, and there very little standard C++ in your code.

john
 
J

Juergen Heinzl

I'm trying to create a simple program in Visual C++ to output the
results of a gethostbyname function call.

However, my program is crashing, and the problem seems to be with
retrieving the data from the hostent struct. [-]
struct hostent *he;
he = gethostbyname("www.jiffysquid.com");
hostname = he->h_name;
[-]
How about ...
if (he != NULL) {
hostname = ...
} else {
shite();
}
.... or something similar?
[-]
Ta',
Juergen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top