views:

38

answers:

1

I need to determine if the computer is connected to the Internet using a mobile connection (e.g. 3G, EDGE, GPRS). I found this identical question, but the accepted answer isn't very helpful to me... The System.Net.NetworkInformation.NetworkInterface class exposes a few properties, but nothing that can help me determine if it is a mobile connection.

My application is in .NET, but I'm also interested in solutions involving Win32 or WMI

+1  A: 

I think it might work if you query the active device for it's Mobile Broadband status, since if it's a Mobile Broadband device it should return the status, but otherwise I assume it would return an error.

This article, MB Miniport Driver Initialization, has a diagram showing how to do something like this. Quote from that page: The following diagram represents the process taken to determine whether the interface is qualified as an MB interface and to gather information about the device capabilities.

Never tried it myself, so I'm not certain of the above and I can't show any sample code but there's a section for samples that might contain something useful here: Network Samples

Edit: Code snippet by someone called Norman Diamon in an old newsgroup posting

DWORD PhysicalMediumQuery = OID_GEN_PHYSICAL_MEDIUM;
NDIS_PHYSICAL_MEDIUM PhysicalMediumResult;
DWORD PhysicalMediumResultLength;
if (!DeviceIoControl(DeviceHandle, IOCTL_NDIS_QUERY_GLOBAL_STATS,
    &PhysicalMediumQuery, sizeof PhysicalMediumQuery,
    &PhysicalMediumResult, sizeof PhysicalMediumResult,
    &PhysicalMediumResultLength, NULL))
{ /* do error handling here */ }
ho1
Thanks, it looks promising. However it looks quite complex, I'm not sure how to use it...
Thomas Levesque
@Thomas: Yes, and as I said, unfortunately I've never used it either. However, I think you use the `DeviceIoControl` (http://msdn.microsoft.com/en-us/library/aa363216%28VS.85%29.aspx) function to do the actual queries, so it might be worth looking for info about that. I'll update my answer with a possible code snippet I found in an old newsgroup posting in case that helps.
ho1
Thanks! Do you know how I can obtain a device handle ?
Thomas Levesque
@Thomas: I think it's `CreateFile` (http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx) and while trying to confirm that I found a posting with a complete code sample here: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/6fa9b1b7-b6b9-4696-9e34-a32457a1107b though that question seems to be regarding problems with using that code on XP, so not sure how good it is, but might be enough to get you started at least.
ho1
The problem is that I don't know how to retrieve the device name... The NetworkInterface class doesn't provide that information. In some cases I can find it using WMI, but not with all 3G modems
Thomas Levesque
@Thomas: You might be able to use `EnumDeviceDrivers` (http://msdn.microsoft.com/en-us/library/ms682617%28VS.85%29.aspx) to enumerate all devices to find what you need? Someone has added a VB6 sample on that MSDN page to use that and some other API methods (`GetDeviceDriverFileNameA` and `GetDeviceDriverBaseNameA`) to get out the names of them.
ho1
@Thomas: And I just saw this sample: Enumerating All Device Drivers in the System (http://msdn.microsoft.com/en-us/library/ms682619%28VS.85%29.aspx) which seems to be a C++ version of that code.
ho1
Thanks, but I need to access the device itself, not the driver...
Thomas Levesque
@Thomas: Can't you get to the device from the driver in some way though? Feels like it should be possible, though I'm not at all sure of course.
ho1
I don't think so... EnumDeviceDrivers gives me the address of the driver in memory. This is pretty far from my original goal...
Thomas Levesque
@Thomas: Ah, my mistake, try this code project article instead: http://www.codeproject.com/KB/system/EnumDevices.aspx
ho1