• 0

[C#] Get the name of the current connection


Question

Recommended Posts

  • 0

Thanks but I think it won't help me. I've tried to do it and it doesn't work. The weird thing is that when I use RasEnumEntries I can get an array with the right connections names, but when I do it with RasEnumConnections I get a weird string :blink: I wonder why is that.

Link to comment
Share on other sites

  • 0

Well that declaration has a bit of a conflict of interest.. RasEnumConnectionsA is the Ansi version but you have it set to unicode. Try this declaration of the API:

[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
public static extern int RasEnumConnections(.....whatever you have defined here.. I don't have time to figure out what the params are now

Dan

Link to comment
Share on other sites

  • 0

Still doesn't work :( I tried:

[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
  internal static extern int RasEnumConnections
  	(
    ref RASCONN lprasconn, // buffer to receive connections data
    ref int lpcb, // size in bytes of buffer
    ref int lpcConnections // number of connections written to buffer
  	);

And nothing. Whne I use EntryPoint="RasEnumConnectionsA" it works but I get a string full of weird characters.

Any idea ?

Link to comment
Share on other sites

  • 0

Thanks Andareed and dannyres - I think that the sites which dannyres potsed are really helpful. However, I still couldn't understand how to use InternetGetConnectedStateEx() :( It is just what I need, I think.

Can you please check my code and tell me what's wrong? The higlighted parts are the important ones. Basically it should put the Internet connection name to m_ConnectionName.

The code is from: http://www.codeproject.com/aspnet/webdialup.asp

Here it is:

using System;

using System.Runtime.InteropServices;

using System.Text;

namespace ConnectionStatus

{

internal enum RasFieldSizeConstants

{

? RAS_MaxDeviceType? ?  =16,

? RAS_MaxPhoneNumber? ? =128,

? RAS_MaxIpAddress? ? ? =15,

? RAS_MaxIpxAddress? ?  =21,

#if WINVER4

? RAS_MaxEntryName? ? ? =256,

? RAS_MaxDeviceName? ?  =128,

? RAS_MaxCallbackNumber =RAS_MaxPhoneNumber,

#else

? RAS_MaxEntryName? ? ? =20,

? RAS_MaxDeviceName? ?  =32,

? RAS_MaxCallbackNumber =48,

#endif

? RAS_MaxAreaCode? ? ?  =10,

? RAS_MaxPadType? ? ? ? =32,

? RAS_MaxX25Address? ?  =200,

? RAS_MaxFacilities? ?  =200,

? RAS_MaxUserData? ? ?  =200,

? RAS_MaxReplyMessage?  =1024,

? RAS_MaxDnsSuffix? ? ? =256,

? UNLEN? ? ? =256,

? PWLEN? ? ? =256,

? DNLEN? ? ? =15

}

[structLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

public struct GUID

{

? public uint?  Data1;

? public ushort?  Data2;

? public ushort?  Data3;

? [MarshalAs(UnmanagedType.ByValArray,SizeConst=8)]

? public byte[] Data4;

}

[structLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

internal struct RASCONN

{

? public int? ?  dwSize;

? public IntPtr? hrasconn;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]

? public string szEntryName;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceType+1)]

? public string? ? szDeviceType;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxDeviceName+1)]

? public string? ? szDeviceName;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]//MAX_PAPTH=260

? public string? ? szPhonebook;

? public int? ? dwSubEntry;

? public GUID? ? guidEntry;

? #if (WINVER501)

?  int? ?  dwFlags;

?  public LUID? ? ? luid;

? #endif

}

[structLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

internal struct LUID

{

? int LowPart;

? int HighPart;

}

[structLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

public struct RasEntryName

{

? public int dwSize;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]

? public string szEntryName;

#if WINVER5

? public int dwFlags;

? [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]

? public string szPhonebookPath;

#endif

}

[structLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]

public class RasStats

{

? public int dwSize=Marshal.SizeOf(typeof(RasStats));

? public int dwBytesXmited;

? public int dwBytesRcved;

? public int dwFramesXmited;

? public int dwFramesRcved;

? public int dwCrcErr;

? public int dwTimeoutErr;

? public int dwAlignmentErr;

? public int dwHardwareOverrunErr;

? public int dwFramingErr;

? public int dwBufferOverrunErr;

? public int dwCompressionRatioIn;

? public int dwCompressionRatioOut;

?[DllImport("rasapi32.dll", EntryPoint="RasEnumConnectionsA")]

  internal static extern int RasEnumConnections

  (

    ref RASCONN lprasconn, // buffer to receive connections data

    ref int lpcb, // size in bytes of buffer

    ref int lpcConnections // number of connections written to buffer

  );

  [DllImport("wininet.dll", EntryPoint="InternetGetConnectedStateExA")]

  internal static extern long InternetGetConnectedStateEx(ref long lpdwFlags, string lpszConnectionName, int dwNameLen, long dwReserved);l static extern long InternetGetConnectedStateEx(ref long lpdwFlags, string lpszConnectionName, int dwNameLen, long dwReserved);

? [DllImport("rasapi32.dll",CharSet=CharSet.Auto)]

? internal static extern uint RasGetConnectionStatistics(

?  IntPtr hRasConn,? ? ?  // handle to the connection

?  [in,Out]RasStats lpStatistics? // buffer to receive statistics

?  );

? [DllImport("rasapi32.dll",CharSet=CharSet.Auto)]

? public extern static uint RasHangUp(

?  IntPtr hrasconn? // handle to the RAS connection to hang up

?  );

? [DllImport("rasapi32.dll",CharSet=CharSet.Auto)]

? public extern static uint RasEnumEntries (

?  string reserved,? ? ? ? ? ? ? // reserved, must be NULL

?  string lpszPhonebook,? ? ? ?  // pointer to full path and

?  //? file name of phone-book file

?  [in,Out]RasEntryName[] lprasentryname, // buffer to receive

?  //? phone-book entries

?  ref int lpcb,? ? ? ? ? ? ? ? ? // size in bytes of buffer

?  out int lpcEntries? ? ? ? ? ?  // number of entries written

?  //? to buffer

?  );

?

? [DllImport("wininet.dll",CharSet=CharSet.Auto)]

? public extern static int InternetDial(

?  IntPtr hwnd,

?  [in]string lpszConnectoid,

?  uint dwFlags,

?  ref int lpdwConnection,

?  uint dwReserved

?  );

? public RAS()

? {

? }

? ? ? ?

}

public class RASDisplay

{

? private string m_duration;

? private string m_ConnectionName;

? private string[] m_ConnectionNames;

? private string m_TX;

? private string m_RX;

? private string m_TXB;

? private string m_RXB;

? private double m_TXD;

? private double m_RXD;

? private bool m_connected;

? private IntPtr m_ConnectedRasHandle;

?

? public RASDisplay()

? {

?  m_connected = true;

?  RAS lpras = new RAS();

?  RASCONN lprasConn = new RASCONN();

?  lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));

?  lprasConn.hrasconn = IntPtr.Zero;

?  int lpcb = 0;

?  int lpcConnections = 0;

?  int nRet = 0;

?  lpcb = Marshal.SizeOf(typeof(RASCONN));

? ? ? ? 

?  nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, ref

? ? lpcConnections);? 

?  if(nRet != 0)

?  {

? ? m_connected = false;

? ? return;

? ?

?  }

?  if(lpcConnections > 0)

?  {

? ?

? ? //for (int i = 0; i < lpcConnections; i++)RAS.InternetGetConnectedStateEx(ref flags, m_ConnectionName, lprasConn.dwSize, 0);ctedRasHandle = lprasConn.hrasconn;

? ?  RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);

long flags = 0;

m_ConnectionName = "";

? ?  RAS.InternetGetConnectedStateEx(ref flags, m_ConnectionName, lprasConn.dwSize, 0);

? ?  int Hours = 0;

? ?  int Minutes = 0;

? ?  int Seconds = 0;

? ?  Hours = ((stats.dwConnectDuration /1000) /3600);

? ?  Minutes = ((stats.dwConnectDuration /1000) /60) - (Hours * 60);

? ?  Seconds = ((stats.dwConnectDuration /1000)) - (Minutes * 60) - (Hours * 3600);

? ?

? ?  string SHours;

? ?  string SMinutes;

? ?  string SSeconds;

? ? 

? ?  if(Hours < 10)

? ?  {

? ? ? SHours = "0" + Hours;

? ?  }

? ?  else

? ?  {

? ? ? SHours = Hours.ToString();

? ?  }

? ?  if(Minutes < 10)

? ?  {

? ? ? SMinutes = "0" + Minutes;

? ?  }

? ?  else

? ?  {

? ? ? SMinutes = Minutes.ToString();

? ?  }

? ?  if(Seconds < 10)

? ?  {

? ? ? SSeconds = "0" + Seconds;

? ?  }

? ?  else

? ?  {

? ? ? SSeconds = Seconds.ToString();

? ?  }

? ? ?

? ?  m_duration = SHours? +? ":"? + SMinutes + ":" + SSeconds;

? ?  double r = stats.dwBytesXmited;

? ?  double r2 = stats.dwBytesRcved;

? ?  if(r < 1024)

? ?  {

? ? ? m_TX = r + " Bytes";

? ? ? m_RX = r2 + " Bytes";

? ?  }

? ?  if((r>1024) && (r <1048576))

? ? ? ? {

? ? ? m_TX = System.Math.Round((r/1024)) + " KB";

? ? ? m_RX = System.Math.Round((r2/1024)) + " KB";

? ? ? ? }

? ?  if((r>1048576) && (r <1073741824))

? ? ? ? {

? ? ? m_TX = System.Math.Round((r/1048576)) + " MB";

? ? ? m_RX = System.Math.Round((r2/1048576)) + " MB";

? ? ? ? }

? ?  if(r>1073741824)

? ? ? ? {

? ? ? m_TX = System.Math.Round((r/1073741824)) + " G";

? ? ? m_RX = System.Math.Round((r2/1073741824)) + " G";

? ? ? ? }

? ? ? m_TXB = r + " Bytes";

? ? ? m_RXB = r2 + " Bytes";

? ? ?

? ? ? m_TXD = r;

? ? ? m_RXD = r2;

? ? //}

?  }

?  else

?  {

? ? m_connected = false;

?  }

?  int lpNames = 1;

?  int entryNameSize = 0;

?  int lpSize = 0;

?  RasEntryName[] names = null;

?  entryNameSize=Marshal.SizeOf(typeof(RasEntryName));

?  lpSize=lpNames*entryNameSize;

?  names=new RasEntryName[lpNames];

?  names[0].dwSize=entryNameSize;

?  uint retval = RAS.RasEnumEntries(null,null,names,ref lpSize,out lpNames);

?  //if we have more than one connection, we need to do it again

?  if(lpNames > 1)

?  {

? ? names=new RasEntryName[lpNames];

? ? for(int i=0;i<names.Length;i++)

? ? {

? ?  names.dwSize=entryNameSize;

? ? }

? ? retval = RAS.RasEnumEntries(null,null,names,ref lpSize,out lpNames);

?  }

?  m_ConnectionNames = new string[names.Length];

? ? ?

?  if(lpNames>0)

?  {? ?

? ? for(int i=0;i<names.Length;i++)

? ? {

? ?  m_ConnectionNames = names.szEntryName;

? ? }

?  }

? }

? public string Duration

? {

?  get

?  {

? ? return m_connected ? m_duration : "N/A";

?  }

? }

?

? public string[] Connections

? {

?  get

?  {

? ? return m_ConnectionNames;

?  }

? }

?

? public string BytesTransmitted

? {

?  get

?  {

? ? return m_connected ? m_TX : "N/A";

?  }

? }

? public string BytesReceived

? {

?  get

?  {

? ? return m_connected ? m_RX :? "N/A";

? ?

?  }

? }

?

? public double BytesTransmittedD

? {

?  get

?  {

? ? return m_connected ? m_TXD : 0;

?  }

? }

? public double BytesReceivedD

? {

?  get

?  {

? ? return m_connected ? m_RXD :? 0;

? ?

?  }

? }

?

? public string BytesTransmittedB

? {

?  get

?  {

? ? return m_connected ? m_TXB : "N/A";

?  }

? }

? public string BytesReceivedB

? {

?  get

?  {

? ? return m_connected ? m_RXB :? "N/A";

? ?

?  }

? }

? public string ConnectionName

? {

?  get

?  {

? ? return m_connected ? m_ConnectionName : "N/A";

?  }

? }

? public bool IsConnected

? {

?  get

?  {

? ? return m_connected;

?  }

? }

? public int Connect(string Con:)ction)

? {

?  int temp = 0;

?  uint INTERNET_AUTO_DIAL_UNATTENDED = 2;

?  int retVal = RAS.InternetDial(IntPtr.Zero,Connection,INTERNET_AUTO_DIAL_UNATTENDED,ref temp,0);

?  return retVal;

? }

? public void Disconnect()

? {

?  RAS.RasHangUp(m_ConnectedRasHandle);

? }

}

}

I'm probably doing something wrong. I try to get the connection name through InternetGetConnectedStateEx().

Thanks in advance for your help :)

Link to comment
Share on other sites

  • 0

Well, the problem is probably in your declaration. In one case, you use the 'ref' keyword and the other you don't. My point is that you should try to copy the declaration from the working function.

Link to comment
Share on other sites

  • 0

This is the declaration for InternetGetConnectionStateEx:

  [DllImport("wininet.dll", CharSet=CharSet.Auto)]
  public static extern int InternetGetConnectedStateEx(out int lpdwFlags, string lpszConnectionName,	int dwNameLen, int dwReserved);


............

  	int flags;
  	string connName = string.Empty;

  	InternetGetConnectedStateEx(out flags, connName, 256,0);
  	MessageBox.Show(connName);

:)

Dan

Link to comment
Share on other sites

  • 0

You are really close - it works inside a MessageBox, but I show the text in another way, not using a MesageBox, and then I see only the first letter fo the connetion name and many weird characters after it :wacko:

I also have another problem - when I try to close the application, my computer get stuck and the application doesn't get closed until I shut it down by killing its process.

What can I do?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.