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