M
Mo
Hello all,
I am developing an application that will emulate the default Wi-Fi AP
menu, it will show the available
Access point list and allow the user to connect/renew IP/ view AP
properties, our client wants extended
AP properties (the default implementation is not enough for him)
so i am designing the application in .net CF 2.0 using C#, (i couldn't
use openCF implementation unfortunately), what i am trying to is
1)create a handle to the NDISUIO adapter
2)enumerate the NICs to find the WiFi one (get device name)
3)perform access point scan (then connect/view properties, etc)
i am able to create a handle to the NDISUIO driver, its a valid handle,
but then when i make DeviceIoControl using the handle from CreatFile,
and i pass the IOCTL "IOCTL_NDISUIO_QUERY_BINDING //Retrieves
adapter name and description."
the call fails, when i call GetLastError() it returns 0x06
"ERROR_INVALID_HANDLE " for the 1st iteration and then
0x32"ERROR_SHARING_VIOLATION " --does this mean the IOCTL is not
supported?
below is my code, i am new to C# and Interop, appreciate any help
Regards
<code>
// PInvoke definitions
[DllImport("coredll.dll", EntryPoint = "CreateFile")]
private static extern Handle CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
int hTemplateFile);
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice, UInt32 dwIoControlCode,
IntPtr lpInBuffer, Int32 nInBufferSize,
IntPtr lpOutBuffer, Int32 nOutBufferSize,
ref UInt32 lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll")]
public static extern uint GetLastError();
public const string NDIS_DEVICE_NAME = "NDS0:";
public const string NDISUIO_DEVICE_NAME = "UIO1:";
//define the IOCTL's
public const uint IOCTL_NDISUIO_QUERY_OID_VALUE = 0x120804;
public const uint IOCTL_NDISUIO_SET_OID_VALUE = 0x120814;
public const uint IOCTL_NDISUIO_REQUEST_NOTIFICATION =
0x12081c;
public const uint IOCTL_NDISUIO_CANCEL_NOTIFICATION = 0x120820;
public const uint IOCTL_NDISUIO_QUERY_BINDING = 0x12c80c;
[StructLayout(LayoutKind.Sequential)]
internal struct NDISUIO_QUERY_BINDING
{
internal uint BindingIndex; // 0-based binding number
internal uint DeviceNameOffset; // from start of this struct
internal uint DeviceNameLength; // in bytes
internal uint DeviceDescrOffset; // from start of this struct
internal uint DeviceDescrLength; // in bytes
}
//here is the bread and butter
public static NdisDevice[] EnumerateDevices()
{
bool bqueryBindingResult = false;
unsafe
{
fixed (byte* buf = Buffer)
{
UInt32 dwBytesWritten=0;
DriverHandle = CreateFile(NDISUIO_DEVICE_NAME,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
INVALID_HANDLE_VALUE);
if ((int)DriverHandle == INVALID_HANDLE_VALUE)
{
uint uError = (uint)GetLastError();
}
NDISUIO_QUERY_BINDING* p = (NDISUIO_QUERY_BINDING*)buf;
for (uint i = 0; /* Nothing */; i++)
{
// The binding index is an *INPUT* to the operation.
// You have to set it to a valid value.
p->BindingIndex = i;
bqueryBindingResult=DeviceIoControl(DriverHandle,
IOCTL_NDISUIO_QUERY_BINDING,
new IntPtr(p),
sizeof(NDISUIO_QUERY_BINDING),
IntPtr.Zero,
Buffer.Length,
ref dwBytesWritten,
IntPtr.Zero);
if(bqueryBindingResult)
{
//get the adapter name
}
else if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
uint uErrorCode = GetLastError();
}
else
break;
}
.....
</code>
--PS: sometimes with this version of CreateFile, DeviceIoControl fails
and i get errorcode 0x06 then 0x050 the next iterations 0x050
"ERROR_NOT_SUPPORTED"
DriverHandle = CreateFile(NDISUIO_DEVICE_NAME, GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_OVERLAPPED,INVALID_HANDLE_VALUE);
I am developing an application that will emulate the default Wi-Fi AP
menu, it will show the available
Access point list and allow the user to connect/renew IP/ view AP
properties, our client wants extended
AP properties (the default implementation is not enough for him)
so i am designing the application in .net CF 2.0 using C#, (i couldn't
use openCF implementation unfortunately), what i am trying to is
1)create a handle to the NDISUIO adapter
2)enumerate the NICs to find the WiFi one (get device name)
3)perform access point scan (then connect/view properties, etc)
i am able to create a handle to the NDISUIO driver, its a valid handle,
but then when i make DeviceIoControl using the handle from CreatFile,
and i pass the IOCTL "IOCTL_NDISUIO_QUERY_BINDING //Retrieves
adapter name and description."
the call fails, when i call GetLastError() it returns 0x06
"ERROR_INVALID_HANDLE " for the 1st iteration and then
0x32"ERROR_SHARING_VIOLATION " --does this mean the IOCTL is not
supported?
below is my code, i am new to C# and Interop, appreciate any help
Regards
<code>
// PInvoke definitions
[DllImport("coredll.dll", EntryPoint = "CreateFile")]
private static extern Handle CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
int hTemplateFile);
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice, UInt32 dwIoControlCode,
IntPtr lpInBuffer, Int32 nInBufferSize,
IntPtr lpOutBuffer, Int32 nOutBufferSize,
ref UInt32 lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll")]
public static extern uint GetLastError();
public const string NDIS_DEVICE_NAME = "NDS0:";
public const string NDISUIO_DEVICE_NAME = "UIO1:";
//define the IOCTL's
public const uint IOCTL_NDISUIO_QUERY_OID_VALUE = 0x120804;
public const uint IOCTL_NDISUIO_SET_OID_VALUE = 0x120814;
public const uint IOCTL_NDISUIO_REQUEST_NOTIFICATION =
0x12081c;
public const uint IOCTL_NDISUIO_CANCEL_NOTIFICATION = 0x120820;
public const uint IOCTL_NDISUIO_QUERY_BINDING = 0x12c80c;
[StructLayout(LayoutKind.Sequential)]
internal struct NDISUIO_QUERY_BINDING
{
internal uint BindingIndex; // 0-based binding number
internal uint DeviceNameOffset; // from start of this struct
internal uint DeviceNameLength; // in bytes
internal uint DeviceDescrOffset; // from start of this struct
internal uint DeviceDescrLength; // in bytes
}
//here is the bread and butter
public static NdisDevice[] EnumerateDevices()
{
bool bqueryBindingResult = false;
unsafe
{
fixed (byte* buf = Buffer)
{
UInt32 dwBytesWritten=0;
DriverHandle = CreateFile(NDISUIO_DEVICE_NAME,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
INVALID_HANDLE_VALUE);
if ((int)DriverHandle == INVALID_HANDLE_VALUE)
{
uint uError = (uint)GetLastError();
}
NDISUIO_QUERY_BINDING* p = (NDISUIO_QUERY_BINDING*)buf;
for (uint i = 0; /* Nothing */; i++)
{
// The binding index is an *INPUT* to the operation.
// You have to set it to a valid value.
p->BindingIndex = i;
bqueryBindingResult=DeviceIoControl(DriverHandle,
IOCTL_NDISUIO_QUERY_BINDING,
new IntPtr(p),
sizeof(NDISUIO_QUERY_BINDING),
IntPtr.Zero,
Buffer.Length,
ref dwBytesWritten,
IntPtr.Zero);
if(bqueryBindingResult)
{
//get the adapter name
}
else if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
uint uErrorCode = GetLastError();
}
else
break;
}
.....
</code>
--PS: sometimes with this version of CreateFile, DeviceIoControl fails
and i get errorcode 0x06 then 0x050 the next iterations 0x050
"ERROR_NOT_SUPPORTED"
DriverHandle = CreateFile(NDISUIO_DEVICE_NAME, GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_OVERLAPPED,INVALID_HANDLE_VALUE);