RasEnumConnections managed wrapper

  • Thread starter Thread starter Francesco
  • Start date Start date
F

Francesco

Hi,

I need to write a wrapper for the RasEnumConnections function, in c#,
but I can't understand c, and my code sistematically doesn't work :|

Have you got some code already written? c# or vb.net is the same, i
can translate.

Thanks
Francesco
 
I used the well-known hack of the opaque block of memory:

1) The function prototype:
[ DllImport("coredll.dll", SetLastError=true) ]
private static extern UInt32 RasEnumConnections(
byte[] rasconn, ref UInt32 size, ref UInt32 count);

2) The definition of the structure used to interpret the block of memory
private const int RasConnSize = 52;//4+4+21*2;
private class RasConn // managed equivalent of the native RASCONN struct
{
public RasConn(byte[] data, int offset)
{
_data = new byte[RasConnSize];
Buffer.BlockCopy(data, offset, _data, 0, RasConnSize);
}
public string Name
{
get
{
string retVal = null;
retVal = Encoding.Unicode.GetString(_data, ConnNameOffset,
20).TrimEnd('\0');
return retVal;
}
}
public IntPtr ConnectionId
{
get
{
IntPtr retVal = IntPtr.Zero;
retVal = (IntPtr) BitConverter.ToInt32(_data, ConnIdOffset);
return retVal;
}
}
private byte[] _data;
public const int SizeOffset = 0;
public const int ConnIdOffset = 4;
public const int ConnNameOffset = 8;
}

3) The call:
UInt32 enumCount = 5;
UInt32 enumSize = MaxConn * RasConnSize;
byte[] enumData = new byte[MaxConn * RasConnSize];
byte[] rasConnSizeBytes = BitConverter.GetBytes(RasConnSize);
Buffer.BlockCopy(rasConnSizeBytes, 0, enumData, 0, 4);
UInt32 error = RasEnumConnections(enumData, ref enumSize, ref enumCount);

I hope this helps.
 
www.componentscience.net, get the ElementsEx library. It's free, full
source, and has RAS.

--
Floyd Burger

Jose Luis Balsera said:
I used the well-known hack of the opaque block of memory:

1) The function prototype:
[ DllImport("coredll.dll", SetLastError=true) ]
private static extern UInt32 RasEnumConnections(
byte[] rasconn, ref UInt32 size, ref UInt32 count);

2) The definition of the structure used to interpret the block of memory
private const int RasConnSize = 52;//4+4+21*2;
private class RasConn // managed equivalent of the native RASCONN struct
{
public RasConn(byte[] data, int offset)
{
_data = new byte[RasConnSize];
Buffer.BlockCopy(data, offset, _data, 0, RasConnSize);
}
public string Name
{
get
{
string retVal = null;
retVal = Encoding.Unicode.GetString(_data, ConnNameOffset,
20).TrimEnd('\0');
return retVal;
}
}
public IntPtr ConnectionId
{
get
{
IntPtr retVal = IntPtr.Zero;
retVal = (IntPtr) BitConverter.ToInt32(_data, ConnIdOffset);
return retVal;
}
}
private byte[] _data;
public const int SizeOffset = 0;
public const int ConnIdOffset = 4;
public const int ConnNameOffset = 8;
}

3) The call:
UInt32 enumCount = 5;
UInt32 enumSize = MaxConn * RasConnSize;
byte[] enumData = new byte[MaxConn * RasConnSize];
byte[] rasConnSizeBytes = BitConverter.GetBytes(RasConnSize);
Buffer.BlockCopy(rasConnSizeBytes, 0, enumData, 0, 4);
UInt32 error = RasEnumConnections(enumData, ref enumSize, ref enumCount);

I hope this helps.


Francesco said:
Hi,

I need to write a wrapper for the RasEnumConnections function, in c#,
but I can't understand c, and my code sistematically doesn't work :|

Have you got some code already written? c# or vb.net is the same, i
can translate.

Thanks
Francesco
 
Thanks to both of you, I just came back to work, I'll try jose's code.
Floyd, thanks for your segnalation but it seems that that library is
designed for the full framework. I need compact framework (I didn't
siad that, but i'm posting in a compactframework newsgroup...) thanks
anyhow.

Bye
francesco
 
Uhm.... In the code the variable MaxConn isn't defined. What is it? an
integer? a constant? How much is its value?
anyone can help?

thanks
francesco
 
Back
Top