
How to call the function "RasEnumEntries"
Hello Nicholas,
thank you for helping me, now everything works fine !!
My solution:
[DllImport("rasapi32.dll")]
public static extern int RasEnumEntries (
[MarshalAs(UnmanagedType.LPTStr)] string sReserved,
[MarshalAs(UnmanagedType.LPTStr)] string sPhonebook,
IntPtr ipBuf,
ref int nSize,
out int nEntries
);
public static int EnumEntries( out string[] p_asEntries )
{
p_asEntries = null;
int iSize, iEntries;
RASENTRYNAME cFirstRasEntry = new RASENTRYNAME();
int iStructSize = Marshal.SizeOf( cFirstRasEntry );
cFirstRasEntry.dwSize = iStructSize;
IntPtr ipBuf = Marshal.AllocHGlobal( iStructSize );
Marshal.StructureToPtr( cFirstRasEntry, ipBuf, true );
iSize = iStructSize;
int iResult = RasApi.RasEnumEntries( null, null, ipBuf, ref iSize, out
iEntries );
Marshal.FreeHGlobal( ipBuf );
if ( iEntries > 0 )
{
ipBuf = Marshal.AllocHGlobal( iSize );
Marshal.StructureToPtr( cFirstRasEntry, ipBuf, true );
iResult = RasApi.RasEnumEntries( null, null, ipBuf, ref iSize, out
iEntries );
p_asEntries = new String[iEntries];
int iPointer = (Int32) ipBuf;
IntPtr ipTemp;
for( int i = 0; i < iEntries; i++ )
{
ipTemp = (IntPtr) (iPointer + ( i * iStructSize));
Marshal.PtrToStructure( ipTemp, cFirstRasEntry );
p_asEntries[i] = cFirstRasEntry.szEntryName;
}
Marshal.FreeHGlobal( ipBuf );
}
return iEntries;
Quote:
}
----------------------------------------------------------------------
Quote:
> Robert,
> When using P/Invoke, you can not marshal c-style arrays from the
> unmanaged realm, as the runtime doesn't know how many elements to marshal.
> In order to get around this, you will have to marshal the array yourself,
> allocating the unmanaged memory and then reading the memory into the
> structure. The static PtrToStructure, AllocCoTaskMem and FreeCoTaskMem
> methods on the Marshal class should help you.
> Hope this helps.
> --
> - Nicholas Paldino [.NET/C# MVP]
> > Hi,
> > I am working on Win 2000 Service Pack 2. Microsoft .NET Framework
Service
> > Pack 2.
> > I want to call the Function "RasEnumEntries" from the "rasapi32.dll".
> > My Sourcecode:
> > [DllImport("rasapi32.dll")]
> > public static extern int RasEnumEntries (
> > [MarshalAs(UnmanagedType.LPTStr)] string sReserved,
> > [MarshalAs(UnmanagedType.LPTStr)] string sPhonebook,
> > [In, Out, MarshalAs(UnmanagedType.LPArray)] RASENTRYNAME[]
> acRasEntryNames,
> > ref int nSize,
> > out int nEntries
> > );
> > The Structure RASENTRYNAME is declared as following:
> > [StructLayout(LayoutKind.Sequential)]
> > public class RASENTRYNAME {
> > public int dwSize = 0;
> > [MarshalAs(UnmanagedType.ByValTStr,
> SizeConst=Constants.RAS_MaxEntryName+1)]
> > public string szEntryName;
> > }
> > When i marshal the RASENTRYNAME Structure as UnmanagedType.LPStruct,
> > everything works fine but i only get the first entry.
> > With LPArray it does not work. ErrorCode 632 "ERROR_INVALID_SIZE".
> > Any solution how to call the function "RasEnumEntries" ????
> > Thanx, Robert.