如何枚举本地的连接

来源:互联网 发布:sql delete like 编辑:程序博客网 时间:2024/05/08 07:38

一年前,用VC++做了一个取得和设置本地计算机的网络信息的程序。其中的网络信息包括,网卡,调制解调器,网络打印机等。一年后,另外的一个项目用到了调制解调器这一部分。打开以前的工程,看到当初那么熟悉的代码,感觉有点陌生了。

借这次重温的机会,把其中的一些东西写下来,即作为技术记录或总结,也可以供大家参考。虽然这次是重温,不过需要用C#来实现。如果时间上允许的话将把C#的那一部分也放上来。希望对他人能有些帮助。

首先,需要枚举出所有连接的名字。Windows中提供了RasEnumEntries函数。在MSDN中对这个函数的说明是“The RasEnumEntries function lists all entry names in a remote access phone book.”。其中“phone book”还不能用汉语正确的表述,不过我想应该是类是一个电话簿,其中记录了所有的网络连接。RasEnumEntries原型如下:

DWORD RasEnumEntries(

  LPCTSTR reserved,

  LPTCSTR lpszPhonebook,

  LPRASENTRYNAME lprasentryname,

  LPDWORD lpcb,

  LPDWORD lpcEntries

);

reserved,是保留参数,必须设置为NULL

lpszPhonebook,制定一个“phone-book”文件的名称,如果设置成NULL,那么系统使用的是默认的“phone-book”文件的名称。

lprasentrynameRASENTRYNAME结构体的缓冲区。这个函数会把每一个phone-book”填充到这个缓冲区数组中。

lpcb,传入RASENTRYNAME结构体的大小,返回成功写入的字节大小。

lpcEntries,返回找到连接的数量。

RasEnumEntries结构体格式如下:

typedef struct _RASENTRYNAME { 

  DWORD  dwSize; 

  TCHAR  szEntryName[RAS_MaxEntryName + 1]; 

#if (WINVER >= 0x500)

  DWORD dwFlags;

  TCHAR  szPhonebookPath[MAX_PATH + 1];

#endif

} RASENTRYNAME; 

MSDN中的说明是“The RASENTRYNAME structure contains an entry name from a remote access phone book.”可以看出,这个结构体存储着连接的名称。这就是我们想得到的。在使用这个结构体的时候,先要设置dwSize成员的值。

    如果你在MSDN中查找RasEnumEntriesRemarks里可以看到微软提供的一个实例,很好用的噢,呵呵。为了方便阅读把这个里贴在下面:

DWORD dwCb = sizeof(RASENTRYNAME);

    DWORD dwErr = ERROR_SUCCESS;

    DWORD dwRetries = 5;

    DWORD dwEntries = 0;

    RASENTRYNAME* lpRasEntryName = NULL;

    //

    // Loop through in case the information from RAS changes between calls.

    while (dwRetries--)

    {

        // If the memory is allocated, free it.

        //

        if (NULL != lpRasEntryName)

        {

            HeapFree(GetProcessHeap(), 0, lpRasEntryName);

            lpRasEntryName = NULL;

        }

        // Allocate the size need for the RAS structure.

        //

        lpRasEntryName = HeapAlloc(GetProcessHeap(), 0, dwCb);

        if (NULL == lpRasEntryName)

        {

            dwErr = ERROR_NOT_ENOUGH_MEMORY;

            break;

        }

        //

        // Set the structure size for version checking purposes.

        lpRasEntryName->dwSize = sizeof(RASENTRYNAME);

        //

        // Call the RAS API, bail on the loop if we are successful or an unknown

        // error occurs.

        dwErr = RasEnumEntries(

                    NULL,

                    NULL,

                    lpRasEntryName,

                    &dwCb,

                    &dwEntries);

        if (ERROR_BUFFER_TOO_SMALL != dwErr)

        {

            break;

        }

    }

    //

    // In the success case print the names of the entries.

    if (ERROR_SUCCESS == dwErr)

    {

        DWORD i;

        printf("Phone-book entries in the default phone book:/n/n");

        for (i = 0; i < dwEntries; i++)

        {

            printf("%s/n", lpRasEntryName[i]->szEntryName);

        }

    }

    else

    {

        printf("RasEnumEntries failed: Error = %d/n", dwErr);

    }

    //

    // Free the memory if necessary.

    if (NULL != lpRasEntryName)

    {

        HeapFree(GetProcessHeap(), 0, lpRasEntryName);

        lpRasEntryName = NULL;

       }

C#下使用API函数不能像VC++那样直接使用,需要进行
  重新声明。而所用到的结构体也要重新定义。VC++中有好
  多数据类型在C#下有不能得到支持,因此在重新定义结构体
  的时候还需要进行数据类型的替换。
  上面提到了RasEnumEntriesRASENTRYNAME
  它们在C#下的声明和定义如下:

  

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

C#下使用API函数不能像VC++那样直接使用,需要进行
  重新声明。而所用到的结构体也要重新定义。VC++中有好
  多数据类型在C#下有不能得到支持,因此在重新定义结构体
  的时候还需要进行数据类型的替换。
  上面提到了RasEnumEntriesRASENTRYNAME
  它们在C#下的声明和定义如下:

  

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

C#下使用API函数不能像VC++那样直接使用,需要进行
  重新声明。而所用到的结构体也要重新定义。VC++中有好
  多数据类型在C#下有不能得到支持,因此在重新定义结构体
  的时候还需要进行数据类型的替换。
  上面提到了RasEnumEntriesRASENTRYNAME
  它们在C#下的声明和定义如下:

  

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。

   ternal 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 RasEntryName

{

       public int dwSize;                  

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

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

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

       public string szEntryName;

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

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

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

       public string szEntryName;

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

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

      [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

   }

 

[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

           )

     具体的信息还请参考MSDN,这里更全面,更具有权威性。
原创粉丝点击