How to call the function "RasEnumEntries" 
Author Message
 How to call the function "RasEnumEntries"

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;

Quote:
}

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.



Sat, 21 May 2005 22:27:38 GMT  
 How to call the function "RasEnumEntries"
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]


Quote:
> 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)]

- Show quoted text -

Quote:
> 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.



Mon, 23 May 2005 02:52:21 GMT  
 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.



Mon, 23 May 2005 15:13:45 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. The mysterious "Call" function

2. function call with "pipe"

3. what is called a "hush function"?

4. "Pure Virtual Function Call"

5. Passing "callback" function to a function

6. about "call by value" and "call by reference"

7. "c" calling Fortran, and Fortran calling "c"

8. remove() vrs fopen("""w")

9. Displaying binary data as ascii "1"'s and "0"'s

10. Looking for "Shroud"/"Obfus"

11. ""help with TSR""

12. Parse trees and "("")"

 

 
Powered by phpBB® Forum Software