RasGetEntryProperties 返回全零

RasGetEntryProperties returning all zeroes

我正在尝试使用 RasGetEntryProperties 获取有关我的电话簿条目之一的信息,但是当我这样做时,returns 的 RASENTRY 结构包含除 [=13= 之外的所有元素的零或空白] 和 dwOptions

我不完全了解这些数据是如何工作的,但我想我至少会看到存储在 phone 书中的设备名称或 phone 编号...

这是我的代码:

        uint dwEntryInfoSize = 0;
        uint i = RasGetEntryProperties(null, "", IntPtr.Zero, ref dwEntryInfoSize, IntPtr.Zero, IntPtr.Zero);


        RASENTRY getRasEntry = new RASENTRY();
        string entryName = "Dial-up Connection test";
        getRasEntry.dwSize = (int)dwEntryInfoSize;

        IntPtr ptrRasEntry = Marshal.AllocHGlobal((int)dwEntryInfoSize);
        Marshal.StructureToPtr(getRasEntry, ptrRasEntry, false);

        uint j = RasGetEntryProperties(null, entryName, ptrRasEntry, ref dwEntryInfoSize, IntPtr.Zero, IntPtr.Zero);

        RASENTRY outRasEntry = (RASENTRY)Marshal.PtrToStructure(ptrRasEntry, typeof(RASENTRY));
        Marshal.FreeHGlobal(ptrRasEntry);

这是 Visual Studio 调试器中 Watch 中 outRasEntry 的部分屏幕截图...

编辑 这是我对 RASENTRY

的定义
    const int MAX_PATH = 260;

    [StructLayout(LayoutKind.Sequential)]
    public struct RASENTRY
    {
          public int       dwSize;
          public int       dwfOptions;
          public int       dwCountryID;
          public int       dwCountryCode;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxAreaCode+1)]
          public string       szAreaCode;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxPhoneNumber+1)]
          public string       szLocalPhoneNumber;
          public int       dwAlternateOffset;
          public RASIPADDR   ipaddr;
          public RASIPADDR   ipaddrDns;
          public RASIPADDR   ipaddrDnsAlt;
          public RASIPADDR   ipaddrWins;
          public RASIPADDR   ipaddrWinsAlt;
          public int       dwFrameSize;
          public int       dwfNetProtocols;
          public int       dwFramingProtocol;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) MAX_PATH)]
          public string       szScript;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) MAX_PATH)]
          public string       szAutodialDll;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) MAX_PATH)]
          public string       szAutodialFunc;
          [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 =
                (int) RasFieldSizeConstants.RAS_MaxPadType + 1)]
          public string       szX25PadType;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxX25Address + 1)]
          public string       szX25Address;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxFacilities + 1)]
          public string       szX25Facilities;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxUserData + 1)]
          public string       szX25UserData;
          public int       dwChannels;
          public int       dwReserved1;
          public int       dwReserved2;
          public int       dwSubEntries;
          public int       dwDialMode;
          public int       dwDialExtraPercent;
          public int       dwDialExtraSampleSeconds;
          public int       dwHangUpExtraPercent;
          public int       dwHangUpExtraSampleSeconds;
          public int       dwIdleDisconnectSeconds;
          public int       dwType;
          public int       dwEncryptionType;
          public int       dwCustomAuthKey;
          public Guid        guidId;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) MAX_PATH)]
          public string       szCustomDialDll;
          public int       dwVpnStrategy;
          public int       dwfOptions2;
          public int       dwfOptions3;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxDnsSuffix)]
          public string       szDnsSuffix;
          public int       dwTcpWindowSize;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) MAX_PATH)]
          public string       szPrerequisitePbk;
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst =
                (int) RasFieldSizeConstants.RAS_MaxEntryName)]
          public string       szPrerequisiteEntry;
          public int       dwRedialCount;
          public int       dwRedialPause;
          RASIPV6ADDR ipv6addrDns;
          RASIPV6ADDR ipv6addrDnsAlt;
          public int       dwIPv4InterfaceMetric;
          public int       dwIPv6InterfaceMetric;
          RASIPV6ADDR ipv6addr;
          public int       dwIPv6PrefixLength;
          public int       dwNetworkOutageTime;
    }

public enum RasFieldSizeConstants
{
    RAS_MaxDeviceType = 16,
    RAS_MaxPhoneNumber = 128,
    RAS_MaxIpAddress = 15,
    RAS_MaxIpxAddress = 21,
    RAS_MaxEntryName = 256,
    RAS_MaxDeviceName = 128,
    RAS_MaxCallbackNumber = RAS_MaxPhoneNumber,
    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
}

    public struct RASIPADDR {
        byte a;
        byte b;
        byte c;
        byte d;
    }

    public struct RASIPV6ADDR
    {
        byte a;
        byte b;
        byte c;
        byte d;
        byte e;
        byte f;
    }

感谢 leppie 为我指明 dotras.codeplex.com 的方向 - 尽管我最终没有使用该库,但查看其源代码帮助我修复了自己的...

关键在于 RASENTRY 的定义...下面第一行中的更改使其生效

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 4)]
public struct RASENTRY
{
    public int       dwSize;
    public int       dwfOptions;
    public int       dwCountryID;
    public int       dwCountryCode;

// etc.