如何强制我的 Windows Mobile 6.5 应用程序始终使用手机 WIFI 适配器,即使手机通过 USB 连接到 PC。
这可以在代码中完成吗?我尝试使用连接管理器 API,但无法弄清楚如何选择特定的网络适配器。
编辑:这是我尝试使用的代码(在网络上的某个地方找到),但在 Connect() 中,我无法弄清楚如何添加特定的适配器,只有当它应该连接到互联网或工作网络时。
namespace Model
{
using System;
using System.Text;
using System.Runtime.InteropServices;
public class ConnectionManager : IDisposable
{
#region Consts
private const int RAS_MaxEntryName = 20;
#endregion
#region structs
private class ConnectionInfo
{
public int cbSize = 0x40; // structure size
public int dwParams = 0;
public int dwFlags = 0; // flags of connection settings
public int dwPriority = 0; // connection priority
public int bExclusive = 0;
public int bDisabled = 0;
public Guid guidDestNet = Guid.Empty; // Connection GUID
public IntPtr hWnd = IntPtr.Zero;
public int uMsg = 0;
public int lParam = 0;
public int ulMaxCost = 0;
public int ulMinRcvBw = 0;
public int ulMaxConnLatency = 0;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct RasConn
{
public int dwSize;
public IntPtr hRasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
public string szEntryName;
}
#endregion
#region P/Invoke
[DllImport("cellcore.dll")]
private static extern int ConnMgrEstablishConnection(
ConnectionInfo connInfo,
out IntPtr connection
);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out]RasConn[] lpRasconn, // buffer to receive connections data
ref int lpcb, // size in bytes of buffer
out int lpcConnections // number of connections written to buffer
);
[DllImport("coredll.dll")]
private static extern uint RasHangUp(
IntPtr hRasConn
);
#endregion
public ConnectionManager()
{
}
public bool Connect()
{
ConnectionInfo connInfo_ = new ConnectionInfo();
connInfo_.cbSize = Marshal.SizeOf(connInfo_);
connInfo_.dwFlags = 0;
connInfo_.dwParams = 0x1;
connInfo_.guidDestNet = new Guid("436EF144-B4FB-4863-A041-8F905A62C572");
connInfo_.dwPriority = 0x08000;
connInfo_.bExclusive = 0;
connInfo_.bDisabled = 0;
connInfo_.hWnd = IntPtr.Zero;
connInfo_.lParam = 0;
IntPtr conn_ = IntPtr.Zero; //we dont need to save it because it aint work
return ConnMgrEstablishConnection(connInfo_, out conn_) == 0;
}
//using ras to disconnect
public static void Disconnect()
{
RasConn[] rconn_ = new RasConn[1]; //as a rule 1 connection is enough
int out_ = Marshal.SizeOf(typeof(RasConn));
int cout_ = 1;
rconn_[0].dwSize = out_;
rconn_[0].szEntryName = null;
RasEnumConnections(rconn_, ref out_, out cout_);
if (cout_ > 0)
{
RasHangUp(rconn_[0].hRasconn);
System.Threading.Thread.Sleep(3000); //msdn says that we should do that
}
}
public void Dispose()
{
ConnectionManager.Disconnect();
}
}
}