GetAdaptersInfo, GetNetworkParams, IP_ADAPTER_INFO, IP_ADDR_STRING

  • Thread starter Thread starter Daniel Moth
  • Start date Start date
D

Daniel Moth

Hi

Maybe some pinvoke guru can help with this please...

I have two requirements:
1. Show details of a DHCP connection (ala ipconfig)
2. Notify the user when there is no IP connection (with/without Ethernet
cable in)

My searches have led me to the following APIs/structs:
GetAdaptersInfo, GetNetworkParams, IP_ADAPTER_INFO, IP_ADDR_STRING

Has anyone used them successfully from .NETcf code?

Cheers
Daniel
 
Thanks Alex.. Sorry I should have searched the group better before posting
my request... I think the SelfMarshalledStruct is something many could
benefit from and a good candidate for opennetcf inclusion...

Your code works great on both the CE emualtor and our CE device except for
Lease Obtained/Expires properties.. they report correct DateTime plus 7
hours... Maybe I am using the info wrong:

DateTime d = new DateTime(1970,1,1);
d = d.AddSeconds(Convert.ToDouble( info.LeaseObtained ));
//d is out by 7 hours (it is in the future)
// same if I use info.LeaseExpires

Any clues?

Cheers
Daniel
 
That sounds suspiciously like time zone issue. Emulator by default uses PDT
= GMT-7.
The LeaseObtained is defined as time_t which is a number of seconds since
January 1, 1970 UTC. DateTime(1970, 1, 1) returns the same date for the
current timezone. Try
DateTime dtLeaseObtained = new
DateTime(1970,1,1).ToLocalTime().AddSeconds(info.LeaseObtained);
Also make that the TZ setting is correct
 
Thanks for the good suggestion...

After changing the timezone to GTM (London)...

Both on the device and emulator the time now reports 1 hour in the past
(that is progress from 7 hours in the future):
*new DateTime(1970,1,1).ToLocalTime().AddSeconds(info.LeaseObtained);*

Instead using the following produces the right result:
*new
DateTime(1970,1,1).ToUniversalTime().AddSeconds(info.LeaseExpires).ToLocalTi
me();*

I suspect this is because of the daylight savings but in any case it
works...

Cheers
Daniel
 
Back
Top