gr******@wendys .com wrote:
Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.
No, you can't use ipconfig.exe in DOS. The ipconfig.exe program is a
Win32 console executable:
C:\WINDOWS\syst em32>file ipconfig.exe
ipconfig.exe: PE executable for MS Windows (console) Intel 80386 32-bit
Attempting to run it from DOS gives the error:
C:\WINDOWS\SYST EM32>ipconfig
This program cannot be run in DOS mode.
I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.
You'll find that net.exe is also a Win32 console app.
That said, perhaps you don't really know what DOS means. You may be
thinking of the Windows console. You can certainly write a Windows
console program to run the ipconfig.exe and net.exe programs.
Here's an example program to get you started. I have tested it and it
works perfectly on DJGPP's gcc, Cygwin's gcc, Borland's bcc32 and
Microsoft's cl compilers.
It does not work in pure DOS, of course, since ipconfig.exe cannot run
in DOS.
Under DOSBox 0.65, DJGPP will compile the program but when I try to run
it, DOSBox crashes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_IP_LEN 16
#define MAX_IP_NUM 10
#define TEMP_FILENAME "ip.tmp"
int main(void)
{
char ip_addresses[MAX_IP_NUM][MAX_IP_LEN] = {{0}};
FILE *fp;
char *p;
char buf[256];
int i = 0, j;
system("ipconfi g " TEMP_FILENAME);
fp = fopen(TEMP_FILE NAME, "r");
if(fp)
{
while(fgets(buf , sizeof buf, fp))
{
if(strstr(buf, "IP Address"))
{
if(NULL != (p = strchr(buf, '\r'))) *p = 0;
if(NULL != (p = strchr(buf, '\n'))) *p = 0;
if(NULL != (p = strstr(buf, ": ")))
{
strncpy(ip_addr esses[i++], p+2, MAX_IP_LEN - 1);
if(i == MAX_IP_NUM) break;
}
}
}
remove(TEMP_FIL ENAME);
for(j = 0; j < i; j++)
{
printf("IP %d: \"%s\"\n", j, ip_addresses[j]);
}
}
else
{
fprintf(stderr, "Error opening " TEMP_FILENAME "\n");
}
return 0;
}
Example run:
C:\docs\prog\c> gcc -dumpmachine
djgpp
C:\docs\prog\c> gcc getip.c
C:\docs\prog\c> a
IP 0: "192.168.20 4.1"
IP 1: "192.168.15 4.1"
IP 2: "192.168.0. 2"
(The first two are VMware virtual adapters, and the third is my LAN
connection.)
--
Simon.