PDA

View Full Version : CONVERT IPaddress to string



dmanion
11-11-2003, 02:20 PM
I have been working on a script that gets the IP address entered by the user and writeline to the hosts file.
I can use a sprintfbox with %d's to display the IP address, but If I use the "%d.%d.%d.%d in the writeline it puts in the actual %d not the numbers.
How can I convert the IPaddress into a string so it will write to the hosts file?

thanks.

philiph
11-11-2003, 04:10 PM
Is the ip address typed into different edit boxes? A single edit box? I am not sure I understand the problem, it sounds like you have created a custom dialog to get the user to type in an ip address. After they type in the ip address you must place it into a variable(s) of type number or string. If you place the ip in a string, then you already have the string that you will write to file. If you have it in number (which then wouldn't have any '.' in it) then you can use NumToStr() to convert each octet.

Hope this helps.

RobertDickau
11-11-2003, 04:12 PM
You can use the Sprintf function to turn the results into a string that can be passed to WriteLine...

dmanion
11-12-2003, 12:39 PM
Here is the script I am using. It verifies the IP address is an actual address.
Thanks.

#include "isrt.h"
#include "ifx.h"
#define HOSTDIR "C:\\WINNT\\system32\\drivers\\etc"
#define HOSTFILE "hosts"

//Name of the custom dialog
#define SD_IP_DLG "SdIPControlDialog"

//ID of the custom dialog
#define SD_IP_DLG_ID 13030

//ID of the palce holder static control
#define IDC_STATIC_PLACEHOLDER 1302

///////////////////////////////////////////////////////
//Winuser.h -- Window Styles
#define WS_CHILDWINDOW 0x40000000L

#define WS_EX_LEFT 0x00000000L
#define WS_EX_LTRREADING 0x00000000L
#define WS_EX_CLIENTEDGE 0x00000200L
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
//Commctrl.h

//Load IP address class constant.
#define ICC_INTERNET_CLASSES 0x00000800

//IP address control window class.
#define WC_IPADDRESS "SysIPAddress32"

//Meesage that retrieves the address values for all
//four fields in the IP address control.
#define IPM_GETADDRESS 1126

///////////////////////////////////////////////////////
prototype ExFn_OpenFile(HWND);
export prototype ExFn_OpenFileMode(HWND);
prototype ExFn_WriteLine(HWND);
//STRING szMsg;
//NUMBER nvFileHandle;
///////////////////////////////////////////////////////
//Windows API's
prototype BOOL Comctl32.InitCommonControlsEx(pointer);
prototype BOOL User32.ScreenToClient(HWND, pointer);
prototype HWND User32.CreateWindowExA(int,
byval string,
byval string,
int,
int,
int,
int,
int,
HWND,
HWND,
HWND,
pointer
);
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
//Custom functions
prototype int SdIPAddressDlg(byref int, byref int, byref int, byref int);

prototype int FIRST_ADDRESS(int);
prototype int SECOND_ADDRESS(int);
prototype int THIRD_ADDRESS(int);
prototype int FOURTH_ADDRESS(int);
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
//Structs that match Windows structs
typedef INITCOMMONCONTROLSEX
begin
int dwSize;
int dwICC;
end;

typedef POINT
begin
long x;
long y;
end;

typedef RECT
begin
int left;
int top;
int right;
int bottom;
end;
///////////////////////////////////////////////////////

//declare variables

INITCOMMONCONTROLSEX InitCtrls;
int nFirst, nSecond, nThird, nFourth;
STRING szLine, host;
NUMBER nvFileHandle;
VARIANT IPnumber;

program
//Load the dll we need
UseDLL(WINSYSDIR ^ "Comctl32.dll");

//Initialize INITCOMMONCONTROLSEX struct
InitCtrls.dwSize = 8;
//or
//InitCtrls.dwSize = SizeOf(InitCtrls);
InitCtrls.dwICC = ICC_INTERNET_CLASSES;

//Initilialize common controls
InitCommonControlsEx(&InitCtrls);

//Call function to display dialog
SdIPAddressDlg(nFirst, nSecond, nThird, nFourth);
//nFirst, nSecond, nThird, nFourth will contain
//the numbers entered in each field.
//////////////////////////////////////////////////////////
//Display the number entered in the IPAddress Control
SprintfBox(INFORMATION, "IP Address1", "%d . %d . %d . %d",
nFirst, nSecond, nThird, nFourth);
/////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
IPnumber = IPM_GETADDRESS;
////////////////////////;
szLine = "igtdmm";
OpenFileMode (FILE_MODE_APPEND);
if (OpenFile (nvFileHandle, HOSTDIR , HOSTFILE) = 0 )
then WriteLine(nvFileHandle,IPnumber + szLine ) ;
endif;
CloseFile(nvFileHandle);
//Unload the dll
UnUseDLL("Comctl32.dll");
endprogram
// ///////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
//Dialog function
//Mostly boilerplate code for all dialogs. The code specific to the
//IPAddress control is in DLG_INIT and SD_PBUT_CONTINUE of switch statement.
/////////////////////////////////////////////////////////////////////////////
function int SdIPAddressDlg(first, second, third, fourth)
string szDialog;
int nId, nDialog, nIPControlID, nAddress;
BOOL bDone, b;
HWND hDlg, hIPControl, hStatic;
RECT rect;
POINT point;
begin
szDialog = SD_IP_DLG;
nDialog = SD_IP_DLG_ID;

// ensure general initialization is complete
if (!bSdInit) then
SdInit();
endif;

if (EzDefineDialog(szDialog, ISUSER, "", nDialog) = DLG_ERR) then
return ISERR_GEN_FAILURE;
endif;

// Loop in dialog until the user selects a standard button
bDone = FALSE;

while (!bDone)
nId = WaitOnDialog(szDialog);

switch (nId)
case DLG_INIT:
hDlg = CmdGetHwndDlg(szDialog);
SdGeneralInit(szDialog, hDlg, 0, szSdProduct);

/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//Begin relevant code

//Get the static control place holder
hStatic = GetDlgItem(hDlg, IDC_STATIC_PLACEHOLDER);

//Get the static control's coordinates
b = GetWindowRect(hStatic, &rect);

point.x = rect.left;
point.y = rect.top;

//Convert coordinates relative to dialog's coordinates
ScreenToClient(hDlg, &point);

//Create the IPAddress control on top of static control placeholder
hIPControl = CreateWindowExA(WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_CLIENTEDGE,
WC_IPADDRESS,
"",
WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,
point.x,
point.y,
rect.right - rect.left,
rect.bottom - rect.top, hDlg,
NULL,
NULL,
NULL
);

//Give IPAddress control the focus
SetFocus(hIPControl);
//End relevant code
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

case SD_PBUT_CONTINUE:
nId = NEXT;
bDone = TRUE;

/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//Begin relevant code

//Get the number entered in IPAddress control
SendMessage(hIPControl, IPM_GETADDRESS, 0, &nAddress);

//Parse out to get each of the four values enteredf
first = FIRST_ADDRESS(nAddress);
second = SECOND_ADDRESS(nAddress);
third = THIRD_ADDRESS(nAddress);
fourth = FOURTH_ADDRESS(nAddress);

//End relevant code
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

case SD_PBUT_BACK:
nId = BACK;
bDone = TRUE;

case DLG_ERR:
nId = ISERR_GEN_FAILURE;
SdError(nId, szDialog);
bDone = TRUE;

case DLG_CLOSE:
SdCloseDlg(hDlg, nId, bDone);

default:
// check standard handling
if (SdIsStdButton(nId) && SdDoStdButton(nId)) then
bDone = TRUE;
endif;
endswitch;
endwhile;

EndDialog(szDialog);
ReleaseDialog(szDialog);

SdUnInit();
return nId;
end;

//Get the first part of address
function int FIRST_ADDRESS(x)
begin
return ((x>>24) & 0xff);
end;

//Get the second part of address
function int SECOND_ADDRESS(x)
begin
return ((x>>16) & 0xff);
end;

//Get the third part of address
function int THIRD_ADDRESS(x)
begin
return ((x>>8) & 0xff);
end;

//Get the fourth part of address
function int FOURTH_ADDRESS(x)
begin
return (x & 0xff);
end;