any help appreciated in creating c# functions for RasEnumConnections and RasHangUp

M

Milsnips

Hi there,

i've tried and tried and just cannot get this working, i want to create a
function in c# that calls the RasEnumConnections and loop through the items,
when i find the one i'm after, then i call RasHangUp on it to disconnect it.

This is what i have so far (sorry if any mistakes here, this is my first
real attempt at API calls)...:

--------------------------------------------------------------
[DllImport("coredll.dll")]
private static extern int RasEnumConnections(ref RASCONN[] lprasconn, ref
uint lpcb, ref UIntPtr lpcConnections);

[DllImport("coredll.dll")]
private static extern int RasHangUp(string session); //not sure here so so i
put string, but i think it needs a handle.



[StructLayout(LayoutKind.Sequential)]
public struct RASCONN
{
public uint dwSize;
public UIntPtr hrasconn;
public string szEntryName;
}

--------------------------------------------------------------
......the rest i'm not too sure of in how to initialize it all and make the
API call to RasEnumConnections....



Any help with source code examples would be reeeallly appreciated :))
thanks in advance,
Paul
 
T

Tomer Gabel

Hello Milsnips,

I've never had use for those particular APIs, however the struct definition
for RASCONN has a string in it; unless you're using CF 2.0 SP2 (which I doubt,
considering it only just came out) it'll never work for you.

Personally I always use a disposable struct on do the marshalling myself,
e.g.

public struct RASCONN : IDisposable
{
public uint dwSize;
public UIntPtr hrasconn;
public IntPtr szEntryName;

....

public void SetEntryName( string s )
{
if ( !this.szEntryName.Equals( IntPtr.Zero ) )
Marshal.FreeHGlobal( this.szEntryName );

byte[] content = System.Text.Encoding.Unicode.GetBytes( s );
byte[] zeroMark = new byte[] { 0, 0 };
this.szEntryName = Marshal.AllocHGlobal( content.Length + 2 );
Marshal.Copy( content, 0, this.szEntryName, content.Length );
Marshal.Copy( zeroMark, 0, new IntPtr( this.szEntryName.ToInt64() + content.Length
), 2 );
}

void IDisposable.Dispose()
{
Marshal.FreeHGlobal( this.szEntryName );
}

(copy/pasted and rewritten from memory, no idea if this will compile or not...
but it should get you on your way.)


Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)
 
G

Guest

Hi,

you might try this one...
Have fun :))

[DllImport("coredll.dll", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out] RASCONN[] rasconn,
[In, Out] ref int cb,
[Out] out int connections);

[DllImport("coredll.dll")]
private static extern uint RasHangUp(IntPtr pRasConn);

/// <summary>
/// Returns all active RAS connections as an array of data structure
RASCONN
/// </summary>
/// <returns></returns>
public static RASCONN[] GetAllConnections()
{
RASCONN[] tempConn = new RASCONN[1];
RASCONN[] allConnections = tempConn;

tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lpcb = tempConn[0].dwSize;
int lpcConnections = 0;
uint ret = RasEnumConnections(tempConn, ref lpcb, out
lpcConnections);
if (ret == ERROR_INVALID_SIZE)
{
throw new Exception("RAS: RASCONN data structure has invalid
format for this OS version");
}
else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
{
// first call returned that there are more than one
connections
// and more memory is required
allConnections = new RASCONN[lpcb /
Marshal.SizeOf(typeof(RASCONN))];
allConnections[0] = tempConn[0];
ret = RasEnumConnections(allConnections, ref lpcb, out
lpcConnections);
}

// Check errors
if (ret != SUCCESS)
{
throw new Exception("RAS returns error: " + ret);
}
if (lpcConnections > allConnections.Length)
{
throw new Exception("RAS: error retrieving correct
connection count");
}
else if (lpcConnections == 0)
{
// if there are no connections resize the data structure
allConnections = new RASCONN[0];
}

return allConnections;
}

/// <summary>
/// Closes all active RAS connections
/// </summary>
/// <returns></returns>
public static void CloseAllConnections()
{
RASCONN[] connections = GetAllConnections();
for (int i = 0; i < connections.Length; ++i)
{
RasHangUp(connections.hrasconn);
}
}




Milsnips said:
Hi there,

i've tried and tried and just cannot get this working, i want to create a
function in c# that calls the RasEnumConnections and loop through the items,
when i find the one i'm after, then i call RasHangUp on it to disconnect it.

This is what i have so far (sorry if any mistakes here, this is my first
real attempt at API calls)...:

--------------------------------------------------------------
[DllImport("coredll.dll")]
private static extern int RasEnumConnections(ref RASCONN[] lprasconn, ref
uint lpcb, ref UIntPtr lpcConnections);

[DllImport("coredll.dll")]
private static extern int RasHangUp(string session); //not sure here so so i
put string, but i think it needs a handle.



[StructLayout(LayoutKind.Sequential)]
public struct RASCONN
{
public uint dwSize;
public UIntPtr hrasconn;
public string szEntryName;
}

--------------------------------------------------------------
......the rest i'm not too sure of in how to initialize it all and make the
API call to RasEnumConnections....



Any help with source code examples would be reeeallly appreciated :))
thanks in advance,
Paul
 
G

Guest

Sorry, forgot some of the code in my last reply.
Here comes the full version...
Have fun :))


// ------------------------------------------------------------------
// GPRS connection control
// ------------------------------------------------------------------

private const int SUCCESS = 0;
private const int ERROR_NOT_ENOUGH_MEMORY = 8;
private const int RASBASE = 600;
private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
private const int ERROR_INVALID_SIZE = RASBASE + 32;

// --- RASCONN data structure definition (refer to ras.h) --
private const int RAS_MaxEntryName = 20;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName
+ 1)]
public string szEntryName;
}
// --------------------------------------------

[DllImport("coredll.dll", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out] RASCONN[] rasconn,
[In, Out] ref int cb,
[Out] out int connections);

[DllImport("coredll.dll")]
private static extern uint RasHangUp(IntPtr pRasConn);

/// <summary>
/// Returns all active RAS connections as an array of data structure
RASCONN
/// </summary>
/// <returns></returns>
public static RASCONN[] GetAllConnections()
{
RASCONN[] tempConn = new RASCONN[1];
RASCONN[] allConnections = tempConn;

tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lpcb = tempConn[0].dwSize;
int lpcConnections = 0;
uint ret = RasEnumConnections(tempConn, ref lpcb, out
lpcConnections);
if (ret == ERROR_INVALID_SIZE)
{
throw new Exception("RAS: RASCONN data structure has invalid
format");
}
else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
{
// first call returned that there are more than one
connections
// and more memory is required
allConnections = new RASCONN[lpcb /
Marshal.SizeOf(typeof(RASCONN))];
allConnections[0] = tempConn[0];
ret = RasEnumConnections(allConnections, ref lpcb, out
lpcConnections);
}

// Check errors
if (ret != SUCCESS)
{
throw new Exception("RAS returns error: " + ret);
}
if (lpcConnections > allConnections.Length)
{
throw new Exception("RAS: error retrieving correct
connection count");
}
else if (lpcConnections == 0)
{
// if there are no connections resize the data structure
allConnections = new RASCONN[0];
}

return allConnections;
}

/// <summary>
/// Closes all active RAS connections
/// </summary>
/// <returns></returns>
public static void CloseAllConnections()
{
RASCONN[] connections = GetAllConnections();
for (int i = 0; i < connections.Length; ++i)
{
RasHangUp(connections.hrasconn);
}
}
 
M

Milsnips

Hi there,

thanks heaps for the example!! i will try it out these days and reply back
on how it went.

thanks,
Paul

mikinder said:
Sorry, forgot some of the code in my last reply.
Here comes the full version...
Have fun :))


// ------------------------------------------------------------------
// GPRS connection control
// ------------------------------------------------------------------

private const int SUCCESS = 0;
private const int ERROR_NOT_ENOUGH_MEMORY = 8;
private const int RASBASE = 600;
private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
private const int ERROR_INVALID_SIZE = RASBASE + 32;

// --- RASCONN data structure definition (refer to ras.h) --
private const int RAS_MaxEntryName = 20;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
RAS_MaxEntryName
+ 1)]
public string szEntryName;
}
// --------------------------------------------

[DllImport("coredll.dll", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out] RASCONN[] rasconn,
[In, Out] ref int cb,
[Out] out int connections);

[DllImport("coredll.dll")]
private static extern uint RasHangUp(IntPtr pRasConn);

/// <summary>
/// Returns all active RAS connections as an array of data
structure
RASCONN
/// </summary>
/// <returns></returns>
public static RASCONN[] GetAllConnections()
{
RASCONN[] tempConn = new RASCONN[1];
RASCONN[] allConnections = tempConn;

tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lpcb = tempConn[0].dwSize;
int lpcConnections = 0;
uint ret = RasEnumConnections(tempConn, ref lpcb, out
lpcConnections);
if (ret == ERROR_INVALID_SIZE)
{
throw new Exception("RAS: RASCONN data structure has
invalid
format");
}
else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
{
// first call returned that there are more than one
connections
// and more memory is required
allConnections = new RASCONN[lpcb /
Marshal.SizeOf(typeof(RASCONN))];
allConnections[0] = tempConn[0];
ret = RasEnumConnections(allConnections, ref lpcb, out
lpcConnections);
}

// Check errors
if (ret != SUCCESS)
{
throw new Exception("RAS returns error: " + ret);
}
if (lpcConnections > allConnections.Length)
{
throw new Exception("RAS: error retrieving correct
connection count");
}
else if (lpcConnections == 0)
{
// if there are no connections resize the data structure
allConnections = new RASCONN[0];
}

return allConnections;
}

/// <summary>
/// Closes all active RAS connections
/// </summary>
/// <returns></returns>
public static void CloseAllConnections()
{
RASCONN[] connections = GetAllConnections();
for (int i = 0; i < connections.Length; ++i)
{
RasHangUp(connections.hrasconn);
}
}
 
M

Milsnips

Hi Mikinder,

Thanks very much for your code example, i gave it a go today in my project
and it works exactly as i had hoped!
I'd been looking for an example like this for a long time and could never
get around to get it working right or completed...

Thanks again!

best regards,
Paul

mikinder said:
Sorry, forgot some of the code in my last reply.
Here comes the full version...
Have fun :))


// ------------------------------------------------------------------
// GPRS connection control
// ------------------------------------------------------------------

private const int SUCCESS = 0;
private const int ERROR_NOT_ENOUGH_MEMORY = 8;
private const int RASBASE = 600;
private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
private const int ERROR_INVALID_SIZE = RASBASE + 32;

// --- RASCONN data structure definition (refer to ras.h) --
private const int RAS_MaxEntryName = 20;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
RAS_MaxEntryName
+ 1)]
public string szEntryName;
}
// --------------------------------------------

[DllImport("coredll.dll", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out] RASCONN[] rasconn,
[In, Out] ref int cb,
[Out] out int connections);

[DllImport("coredll.dll")]
private static extern uint RasHangUp(IntPtr pRasConn);

/// <summary>
/// Returns all active RAS connections as an array of data
structure
RASCONN
/// </summary>
/// <returns></returns>
public static RASCONN[] GetAllConnections()
{
RASCONN[] tempConn = new RASCONN[1];
RASCONN[] allConnections = tempConn;

tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lpcb = tempConn[0].dwSize;
int lpcConnections = 0;
uint ret = RasEnumConnections(tempConn, ref lpcb, out
lpcConnections);
if (ret == ERROR_INVALID_SIZE)
{
throw new Exception("RAS: RASCONN data structure has
invalid
format");
}
else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
{
// first call returned that there are more than one
connections
// and more memory is required
allConnections = new RASCONN[lpcb /
Marshal.SizeOf(typeof(RASCONN))];
allConnections[0] = tempConn[0];
ret = RasEnumConnections(allConnections, ref lpcb, out
lpcConnections);
}

// Check errors
if (ret != SUCCESS)
{
throw new Exception("RAS returns error: " + ret);
}
if (lpcConnections > allConnections.Length)
{
throw new Exception("RAS: error retrieving correct
connection count");
}
else if (lpcConnections == 0)
{
// if there are no connections resize the data structure
allConnections = new RASCONN[0];
}

return allConnections;
}

/// <summary>
/// Closes all active RAS connections
/// </summary>
/// <returns></returns>
public static void CloseAllConnections()
{
RASCONN[] connections = GetAllConnections();
for (int i = 0; i < connections.Length; ++i)
{
RasHangUp(connections.hrasconn);
}
}
 
M

Milsnips

Hi mikinder,

Thanks again for your code example, i have been trying to learn API
programming also, but i just cant seem to get the grasp of some things like:

1. where do i find the consts / and their values that correspond to an API?
2. Is there any website kind of reference helper that can show me the
conversion from c++ to c# datatypes etc?
3. I know MSDN is always a good place to look for info on windows API's but
their help pages seem vague and a little difficult to understand at times..
4. [In, Out] parameter - what is the c# equivalent for this? i've seen it a
number of times but im not sure..?

thanks again,
Paul


mikinder said:
Sorry, forgot some of the code in my last reply.
Here comes the full version...
Have fun :))


// ------------------------------------------------------------------
// GPRS connection control
// ------------------------------------------------------------------

private const int SUCCESS = 0;
private const int ERROR_NOT_ENOUGH_MEMORY = 8;
private const int RASBASE = 600;
private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
private const int ERROR_INVALID_SIZE = RASBASE + 32;

// --- RASCONN data structure definition (refer to ras.h) --
private const int RAS_MaxEntryName = 20;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
RAS_MaxEntryName
+ 1)]
public string szEntryName;
}
// --------------------------------------------

[DllImport("coredll.dll", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern uint RasEnumConnections(
[In, Out] RASCONN[] rasconn,
[In, Out] ref int cb,
[Out] out int connections);

[DllImport("coredll.dll")]
private static extern uint RasHangUp(IntPtr pRasConn);

/// <summary>
/// Returns all active RAS connections as an array of data
structure
RASCONN
/// </summary>
/// <returns></returns>
public static RASCONN[] GetAllConnections()
{
RASCONN[] tempConn = new RASCONN[1];
RASCONN[] allConnections = tempConn;

tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lpcb = tempConn[0].dwSize;
int lpcConnections = 0;
uint ret = RasEnumConnections(tempConn, ref lpcb, out
lpcConnections);
if (ret == ERROR_INVALID_SIZE)
{
throw new Exception("RAS: RASCONN data structure has
invalid
format");
}
else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
{
// first call returned that there are more than one
connections
// and more memory is required
allConnections = new RASCONN[lpcb /
Marshal.SizeOf(typeof(RASCONN))];
allConnections[0] = tempConn[0];
ret = RasEnumConnections(allConnections, ref lpcb, out
lpcConnections);
}

// Check errors
if (ret != SUCCESS)
{
throw new Exception("RAS returns error: " + ret);
}
if (lpcConnections > allConnections.Length)
{
throw new Exception("RAS: error retrieving correct
connection count");
}
else if (lpcConnections == 0)
{
// if there are no connections resize the data structure
allConnections = new RASCONN[0];
}

return allConnections;
}

/// <summary>
/// Closes all active RAS connections
/// </summary>
/// <returns></returns>
public static void CloseAllConnections()
{
RASCONN[] connections = GetAllConnections();
for (int i = 0; i < connections.Length; ++i)
{
RasHangUp(connections.hrasconn);
}
}
 
G

Guest

1. where do i find the consts / and their values that correspond to an

The SDK headers.
2. Is there any website kind of reference helper that can show me the
conversion from c++ to c# datatypes etc?

Nope. Pure experience. There are plenty of sites that have examples that
you can reverse, but nothing I'm aware of that shows them side by side.
3. I know MSDN is always a good place to look for info on windows API's
but their help pages seem vague and a little difficult to understand at
times..

Again, look at the SDK source instead.
4. [In, Out] parameter - what is the c# equivalent for this? i've seen it
a number of times but im not sure..?

It would be 'ref' for a value type. A memory type is already passed by ref,
so you'd use no decoration (unless you actually want a double pointer, which
is sometimes the case).


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
 
M

Milsnips

Thanks for the valueable info, Chris.

i'll have a test run on some CF API's and see how i get along :)

regards,
Paul.

1. where do i find the consts / and their values that correspond to an
API?

The SDK headers.
2. Is there any website kind of reference helper that can show me the
conversion from c++ to c# datatypes etc?

Nope. Pure experience. There are plenty of sites that have examples that
you can reverse, but nothing I'm aware of that shows them side by side.
3. I know MSDN is always a good place to look for info on windows API's
but their help pages seem vague and a little difficult to understand at
times..

Again, look at the SDK source instead.
4. [In, Out] parameter - what is the c# equivalent for this? i've seen
it a number of times but im not sure..?

It would be 'ref' for a value type. A memory type is already passed by
ref, so you'd use no decoration (unless you actually want a double
pointer, which is sometimes the case).


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
 
G

Guest

eww...Nice code there! And it works fine in the emulator.

I will try to use this to "repair" GPRS connections that occaisionally
hang/block. I THINK if I use your class to kill the GPRS connection, the next
time the app needs to send via GPRS, it will try and make a new
connection.......hopefully.

In the emulator, the szEntryName = 'DMA Authentication (or something like
that). Any idea what a GPRS connection would be called? I dont have a device
to do any tests on and I assume this class wont work for WM2003 because there
will be a conflict in the coredll.dll.

Thanks for that very good working example.

Ox
 
L

Lev Anter

Hi

This code looks excellent and exactly what I am trying to get to work.
It all compiles and executes fine on the device...But this example
always returns 0 connections.

I am using an HTC 710S WM6...are there differences when running on this
device?

I also read somewhere that if the unit is ActiveSync connections are not
counted

An idea why i am getting zero connections even when GPRS is connected?

Thanks in advance

n.
 
S

Silviu Saulea

Hello I tryed your code and finally it's something what is doing
something. Kills even the USB connection, and that's something (good to
know). But...

I programed not on the emulator (Pocket PC 2003 device) but directly on
a device with a proper GPRS connection set. It kills the connection, I
mean after I "RasHangUp(connections.hrasconn);" the IE doesen't find
any address, application eather. But still the status on GPRS antena
symbol (uper-side of the touchscreen) looks like connected.
After closing (killing) the connection in this maner, the GPRS won't try
to reconnect no mather what. What I'm trying to obtain here efectively
the same effect like closing (hang up) the GPRS manualy. I guess this
code is closing the connection in a logical (software) way.
I have the same problem with my devices, because I have a iLancher
application that blocks the user access on the device and reduce it only
at two buttons. The user can not manually disconnect the GPRS, and after
a while the connection becomes dead (still the antena symbol up on the
screen looks connected), and no answer from webservices. The only way
was to disconnect and reconnect and all was just perfect. But for that I
have to dinamically disconnect the GPRS. Not killing the connection but
efectively the phone part in the device to be hanged up.
Any help will be greatfully apreciated.

Thanks and thanks any way for your code, with this I feel that I'm one
step closer to solve my issue.

Silviu
 
M

Miroslav Georgiev

Hi mikinde,

Thanks for your code it will be very helpful for me but I have a problem with disconnecting. The problem is that GetAllConnections() method returns an empty array and I cannot execute RasHangUp method because I have no connection found. What could be the problem ? It is very important to me to fix the problem as soon as I can. Hope you will help me ! Thanks a lot !
 
P

Paul G. Tobey [eMVP]

What code? Starting a new thread is the wrong thing to do. Reply to your
old thread, so we have some history!

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top