0

我正在尝试获取有关我的电话簿条目之一的信息,RasGetEntryProperties但是当我这样做时,返回的 RASENTRY 结构包含除dwSizeand之外的所有元素的零或空白dwOptions

我不完全了解这些数据的工作原理,但我想我至少会看到存储在电话簿中的设备名称或电话号码......

这是我的代码:

        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);

电话簿条目属性

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

调试器监视中的输出

编辑 这是我的定义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;
    }
4

1 回答 1

0

感谢 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.
于 2015-07-14T10:20:58.777 回答