Mail Archives: djgpp/2002/09/11/17:30:09
Subject: Re: Getting MAC Address (popen method) within C/C++-program on
Windows 2000
Newsgroups: Cornell Newsstand:comp.os.ms-
windows.programmer.misc,microsoft.public.win2000.general
"Alex Vinokur" <alexvn AT bigfoot DOT com> wrote in
news:alno5j$1rsu90$1 AT ID-79865 DOT news DOT dfncis DOT de:
> ==========================================
> Windows 2000 Professional
> --------------------
> DJGPP 2.03
> GNU gcc/gpp version 3.1
> --------------------
> MINGW-2.0.0-2
> GNU gcc/g++ version 3.2 (mingw special 20020817-1)
> ==========================================
>
>
> All attempts
> to get MAC Address (SNMP, NetBIOS, RPC methods) within C/C++-program
> in Windows2000 have failed.
>
I am curious ... how does the NetBIOS method fail? Do you have NetBIOS
over TCP/IP enabled?
I have some old NetBIOS code written for DJGPP (I intended it to be a
library, and it could still become one, but old-timers in this group may
remember that it never was that usable). Anyway, I dug it up, and wrote a
short routine to issue a NCB.STATUS (see
http://publibz.boulder.ibm.com/cgi-
bin/bookmgr_OS390/BOOKS/bk8p7001/4.6.22?SHELF=&DT=19960430153053)
command. The first six bytes returned by this command give you the
adapter's MAC address. This method works on my Win XP Pro machine with
NetBIOS over TCP/IP enabled. (I didn't try it with just NetBEUI).
A possible gotcha is that you need to put a name beginning with * in the
remote name (not the local name field) field of the NCB you are issuing
if you want the status for the local machine. And don't forget to
allocate space for the information returned in conventional memory. Here
are the relevant snippets:
static NBAPI_BYTE __nb_dispatchNCB(NBAPI_LADDR ncbAddress, NBAPI_WORD
ncbSegment) {
_go32_dpmi_registers regs;
memset(®s, 0, sizeof(regs));
regs.x.bx = 0;
regs.x.es = ncbSegment;
_go32_dpmi_simulate_int(0x5C, ®s);
return NB_GET_RET_CODE(ncbAddress);
}
NBAPI_BYTE __nb_status(NBAPI_BYTE lana, const NBAPI_BYTE* name,
NBAPI_BYTE* info) {
NBAPI_BYTE ret;
int ncbSelector = 0;
int bufSelector = 0;
NBAPI_LADDR ncbAddress = 0;
NBAPI_LADDR bufAddress = 0;
NBAPI_WORD ncbSegment = 0;
NBAPI_WORD bufSegment = 0;
ncbSegment = (NBAPI_WORD)
__dpmi_allocate_dos_memory(NB_NCBSIZE/16, &ncbSelector);
ncbAddress = ncbSegment*16;
bufSegment = (NBAPI_WORD)
__dpmi_allocate_dos_memory(80/16, &bufSelector);
bufAddress = bufSegment*16;
NB_SET_CMD(ncbAddress, AS_NB_GETASTAT);
NB_SET_RMT_NAME(ncbAddress, name);
NB_SET_LANA(ncbAddress, lana);
NB_SET_IO_OFF(ncbAddress, 0x0000);
NB_SET_IO_SEG(ncbAddress, bufSegment);
NB_SET_LEN(ncbAddress, NB_BUFSIZE);
NB_SET_POST_OFF(ncbAddress, 0x0000);
NB_SET_POST_SEG(ncbAddress, 0x0000);
ret = __nb_dispatchNCB(ncbAddress, ncbSegment);
movedata(_dos_ds, bufAddress, _my_ds(), (int) info, 80);
__dpmi_free_dos_memory(bufSelector);
__dpmi_free_dos_memory(ncbSelector);
return ret;
}
NBAPI_BYTE doit(int lana, NBAPI_BYTE* name, NBAPI_BYTE* info) {
NBAPI_BYTE ret = NBAStat(lana, name, info);
if(ret == NB_OK) {
printf("Adapter Address:\t");
{
int f;
for(f = 0; f < 5; ++f) {
printf("%2.2X-", *(info + f));
}
printf("%2.2X\n", *(info +f));
}
}
return ret;
}
FYI:
typedef unsigned int NBAPI_LADDR;
typedef unsigned int NBAPI_DWORD;
typedef unsigned short NBAPI_WORD;
typedef unsigned char NBAPI_BYTE;
and the NB_SET/NB_GET macros are just _farpeek/_farpoke wrappers.
This should help you get started. I am not posting all the include files
etc because the code is not in a shape I want to share with the rest of
the world. If you want, I can email you the compiled program so you can
run it on your machine to see if it works.
Hope this helps.
Sinan.
--
A. Sinan Unur
asu1 AT c-o-r-n-e-l-l DOT edu
Remove dashes for address
Spam bait: mailto:uce AT ftc DOT gov
- Raw text -