[Windows Mobile] Connection Manager API C#

Some posts ago i wrote about using RAS to establish internet connection in that post i mentioned about Connection Manager API. Full description you can find on MSDN, but in most cases developers use 2 functions:

ConnMgrEstablishConnection
ConnMgrReleaseConnection

So to establish connection we need just fill one struct and call ConnMgrEstablishConnection. Thats easy, isnt it?

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;
}

I dont know why, but ConnMgrReleaseConnection aint work for me :( i returns success result, but connection still established, so to Disconnect i still use RAS, but how? i can call RasHangUp if i dont know Connection Handle, so we need to use RasEnumConnections to get it and then call RasHangUp

//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
}
}

Sources you can donload here

5 Responses to “[Windows Mobile] Connection Manager API C#”

  1. 1
    Niko Says:

    Hi,

    RasEnumConnections not work on my windows mobile 6.1 device. Why ?

    Thanks

  2. 2
    Dinesh Says:

    Dear all,

    I am new to C#.net. Can any one give me the built code for RAS Dial connection and i am using winCE 5.0 version.

    Thanks.
    Dinesh

  3. 3
    gdd Says:

    work fine on my wm6.5 phone,thanks.

  4. 4
    Krull Says:

    The reason I think the ConnMgrReleaseConnection doesnt work for you is because it leaves the connection cashed. But it depends on how you use it.

    Prior to Windows Mobile 5.0, an application could only specify whether or not a connection was cached. With Windows Mobile 5.0 and continuing with Windows Mobile 6, you can specify the amount of time you would like the Connection Manager to cache the connection. You do this by simply passing the desired number of seconds to cache the connection rather than passing a 0 or a 1 to the cache parameter. For example, the following code shows how to release the connection and request that the Connection Manager cache the connection for 5 minutes (300 seconds).

    // Release the connection and cache it for 5 minutes.
    ConnMgrReleaseConnection(_connectionHandle, 300);

  5. 5
    admin Says:

    I`ve tried 0 connection still established

Leave a Reply