Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Marshalling nested structures

46 views
Skip to first unread message

Youcef Laribi

unread,
Jun 21, 2002, 11:51:46 AM6/21/02
to
I'm new to .NET interop and I'm trying to call the following Win32 API in
"netapi32.dll"

DWORD DsGetDcName(LPCTSTR ComputerName, LPCTSTR DomainName,
GUID* DomainGuid, LPCTSTR
SiteName, ULONG Flags,
PDOMAIN_CONTROLLER_INFO *
DomainControllerInfo);

Where DOMAIN_CONTROLLER_INFO is defined as:

typedef struct _DOMAIN_CONTROLLER_INFO {
LPCTSTR DomainControllerName;
LPCTSTR DomainControllerAddress;
ULONG DomainControllerAddressType;
GUID DomainGuid;
LPCTSTR DomainName;
LPCTSTR DnsForestName;
ULONG Flags;
LPCTSTR DcSiteName;
LPCTSTR ClientSiteName;
} * PDOMAIN_CONTROLLER_INFO;

To call it from my C# program, I defined the following:


[StructLayout(LayoutKind.Sequential)]

public class GUID

{

public ulong Data1;

public ushort Data2;

public ushort Data3;

[MarshalAs(UnmanagedType.LPArray, SizeConst=8)]

public byte[] Data4;

}

[StructLayout(LayoutKind.Sequential)]

public class DOMAIN_CONTROLLER_INFO

{

public string DomainControllerName;

public string DomainControllerAddress;

public ulong DomainControllerAddressType;

[MarshalAs(UnmanagedType.Struct)]

public GUID DomainGuid;

public string DomainName;

public string DnsForestName;

public ulong Flags;

public string DcSiteName;

public string ClientSiteName;

}

And the prototype of the function as:

[DllImport("C:\\WINDOWS\\System32\\netapi32.dll")]

public static extern int DsGetDcName(string ComputerName, string DomainName,


[MarshalAs(UnmanagedType.LPStruct)] GUID DomainGuid,

string
SiteName, ulong Flags,

ref
DOMAIN_CONTROLLER_INFO DomainControllerInfo);


I get the following TypeLoadException exception when I make the call:

Exception occurred: System.TypeLoadException: Can not marshal field
DomainGuid of type TestProject.DOMAIN_CONTROLLER_INFO: The type definition
of this field has no layout information.

Any ideas why ? Thanks.

NETMaster

unread,
Jun 22, 2002, 7:00:21 AM6/22/02
to
Note, C++ ULONG is not 'ulong' in C#...

I don't have a DC to test, but try this code:

// =========================================================================

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto )]
public struct DOMAIN_CONTROLLER_INFO


{
public string DomainControllerName;
public string DomainControllerAddress;

public int DomainControllerAddressType;
public Guid DomainGuid;


public string DomainName;
public string DnsForestName;

public int Flags;


public string DcSiteName;
public string ClientSiteName;
}

[StructLayout(LayoutKind.Sequential)]
public class GuidAsClass // workaround: Guid as 'ref'/null
{
public Guid DomainGuid;
}


class NetApiTesting
{
[DllImport("netapi32.dll", ExactSpelling=true)]
public extern static int NetApiBufferFree( IntPtr bufptr );

[DllImport("netapi32.dll", CharSet=CharSet.Auto)]
public static extern int DsGetDcName( string ComputerName, string DomainName,
[In] GuidAsClass DomainGuid, string SiteName, int Flags, out IntPtr pDCI );

[MTAThread]
static void Main( string[] args )
{
DOMAIN_CONTROLLER_INFO dci;
IntPtr pDCI = IntPtr.Zero;

// GuidAsClass gid = new GuidAsClass();
// gid.DomainGuid = new Guid( "6B29FC40-CA47-1067-B31D-00DD010662DA" );
int ns = DsGetDcName( null, null, null, null, 0, out pDCI );
// todo: error handling
dci = (DOMAIN_CONTROLLER_INFO) Marshal.PtrToStructure( pDCI, typeof(DOMAIN_CONTROLLER_INFO) );
NetApiBufferFree( pDCI ); pDCI = IntPtr.Zero;

}
}
// =========================================================================


--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html

"Youcef Laribi" <youcef...@ntlworld.com> wrote in message news:10246747...@proxy1.ctxuk.citrix.com...

Youcef Laribi

unread,
Jun 24, 2002, 10:52:55 AM6/24/02
to
Thanks for your help, that did the trick !

youcef

--


0 new messages