C#在wince如何操作网卡(禁用/启用)

peterotomy 2010-02-24 10:23:20
有人说DeviceIoControl可以做,但不知道怎么用,请教高手
...全文
236 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Defonds 2010-03-02
  • 打赏
  • 举报
回复
引用 1 楼 peterotomy 的回复:
没人响应,自己来
/// <summary>
        /// 在注册表中对IP信息进行修改后,禁用网卡然后重启网卡以应用修改
        /// </summary>
        public static bool ApplyIpAddress()
        {
            int hNdis = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
            if (hNdis == INVALID_HANDLE_VALUE)
            {
                return false;
            }
            // Send the device command.
            if (DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER, EtherCardName, EtherCardName.Length * 2 + 2, null, 0, 0, 0) == 0)
            {
                return false;
            }

            CloseHandle(hNdis);
            return true;
        }
        /// <summary>
        /// 以太网卡的设备文件名称
        /// </summary>
        private const string EtherCardFileName = "NDS0:";

        /// <summary>
        /// 以太网卡的名称
        /// </summary>
        private static string EtherCardName = string.Empty;
        private const int OPEN_EXISTING = 3;
        private const int FILE_ATTRIBUTE_NORMAL = 0x80;
        private const int INVALID_HANDLE_VALUE = -1;
        private const int IOCTL_NDIS_REBIND_ADAPTER = 1507374;
        private const int IOCTL_NDIS_GET_ADAPTER_NAMES = 1507386;

        [DllImport("Coredll.dll", EntryPoint = "CreateFile")]
        private static extern int CreateFile(
                string lpFileName,
                int dwDesiredAccess,
                int dwShareMode,
                int lpSecurityAttributes,
                int dwCreationDisposition,
                int dwFlagsAndAttributes,
                int hTemplateFile
            );

        [DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")]
        private static extern int DeviceIoControl(
                int hDevice,
                int dwIoControlCode,
                string lpInBuffer,
                int nInBufferSize,
                string lpOutBuffer,
                int nOutBufferSize,
                int lpBytesReturned,
                int lpOverlapped
            );

        [DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")]
        private static extern int DeviceIoControl2(
                int hDevice,
                int dwIoControlCode,
                string lpInBuffer,
                int nInBufferSize,
                string lpOutBuffer,
                int nOutBufferSize,
                ref int lpBytesReturned,
                int lpOverlapped
            );

        [DllImport("Coredll.dll", EntryPoint = "CloseHandle")]
        private static extern int CloseHandle(int hObject);

        /// <summary>
        /// 获得网卡的名称
        /// </summary>
        /// <returns> </returns>
        private static string GetEtherCardName()
        {
            string Names = new string(' ', 255);
            int Bytes = 0;

            int FileHandle = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
            if (FileHandle == INVALID_HANDLE_VALUE)
            {
                return string.Empty;
            }

            if (DeviceIoControl2(FileHandle, IOCTL_NDIS_GET_ADAPTER_NAMES, null, 0, Names, 255, ref Bytes, 0) == 0)
            {
                return string.Empty;
            }

            int AIndex = Names.IndexOf('\0');
            string AResult = Names.Substring(0, AIndex);
            return AResult;
        }
可以了?
世外涛缘 2010-03-01
  • 打赏
  • 举报
回复
楼主应该是发错地方了。
给你转到Windows Mobile小版了。
peterotomy 2010-03-01
  • 打赏
  • 举报
回复
没人响应,自己来
/// <summary>
/// 在注册表中对IP信息进行修改后,禁用网卡然后重启网卡以应用修改
/// </summary>
public static bool ApplyIpAddress()
{
int hNdis = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
if (hNdis == INVALID_HANDLE_VALUE)
{
return false;
}
// Send the device command.
if (DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER, EtherCardName, EtherCardName.Length * 2 + 2, null, 0, 0, 0) == 0)
{
return false;
}

CloseHandle(hNdis);
return true;
}
/// <summary>
/// 以太网卡的设备文件名称
/// </summary>
private const string EtherCardFileName = "NDS0:";

/// <summary>
/// 以太网卡的名称
/// </summary>
private static string EtherCardName = string.Empty;
private const int OPEN_EXISTING = 3;
private const int FILE_ATTRIBUTE_NORMAL = 0x80;
private const int INVALID_HANDLE_VALUE = -1;
private const int IOCTL_NDIS_REBIND_ADAPTER = 1507374;
private const int IOCTL_NDIS_GET_ADAPTER_NAMES = 1507386;

[DllImport("Coredll.dll", EntryPoint = "CreateFile")]
private static extern int CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);

[DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")]
private static extern int DeviceIoControl(
int hDevice,
int dwIoControlCode,
string lpInBuffer,
int nInBufferSize,
string lpOutBuffer,
int nOutBufferSize,
int lpBytesReturned,
int lpOverlapped
);

[DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")]
private static extern int DeviceIoControl2(
int hDevice,
int dwIoControlCode,
string lpInBuffer,
int nInBufferSize,
string lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
int lpOverlapped
);

[DllImport("Coredll.dll", EntryPoint = "CloseHandle")]
private static extern int CloseHandle(int hObject);

/// <summary>
/// 获得网卡的名称
/// </summary>
/// <returns></returns>
private static string GetEtherCardName()
{
string Names = new string(' ', 255);
int Bytes = 0;

int FileHandle = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
if (FileHandle == INVALID_HANDLE_VALUE)
{
return string.Empty;
}

if (DeviceIoControl2(FileHandle, IOCTL_NDIS_GET_ADAPTER_NAMES, null, 0, Names, 255, ref Bytes, 0) == 0)
{
return string.Empty;
}

int AIndex = Names.IndexOf('\0');
string AResult = Names.Substring(0, AIndex);
return AResult;
}

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧