komodosp
komodosp

Reputation: 3646

RasGetEntryProperties returning all zeroes

I am trying to get information about one of my Phonebook entries using RasGetEntryProperties but when I do, the RASENTRY struct that returns contains zeroes or blanks for all elements except for dwSize and dwOptions.

I don't have a full understanding of how this data works but I would have thought I'd at least see the device name or phone number which is stored in the phonebook...

Here is my code:

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

Phonebook entry properties

And here is a partial screenshot of outRasEntry in the Watch in Visual Studio debugger...

Output in debugger watch

EDIT Here is my definition of 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;
    }

Upvotes: 0

Views: 374

Answers (1)

komodosp
komodosp

Reputation: 3646

Thanks leppie for pointing me in the direction of dotras.codeplex.com - although I didn't end up using that library, looking at its source code helped me fix my own...

The key was in the definition of RASENTRY... The change in the first line below made it work

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

// etc.

Upvotes: 0

Related Questions