Ron, >public struct RIBInterfaceEntry >{ > [MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )] > public string name; > int entrytype; > int subtype >}
You don't need the MarshalAs attribute here. System.String will be marshalled to a null-terminated string of ANSI characters by default. (However, the docs claim that it gets marshalled to Unicode BSTR. Can someone confirm that the docs are incorrect here?) If you do want to declare the marshalling explicitly, you should use UnmanagedType.LPStr. You use UnmanagedType.ByValTStr for fixed-length platform-dependent character arrays within a struct, i.e. CHAR[] or WCHAR[]. HTH, Stefan >-----Original Message----- >From: [EMAIL PROTECTED] >[mailto:[EMAIL PROTECTED] On Behalf >Of [EMAIL PROTECTED] >Sent: Tuesday, February 25, 2003 7:25 PM >To: [EMAIL PROTECTED] >Subject: Need help with a P/Invoke function > > >Hello all, > >If someone could help me out with this, I'd appreciate it very much. > >I have this function signature in a Win32 .dll: > >S32 AIL_enumerate_filter_attributes( HPROVIDER filter, >HINTENUM *next, RIB_INTERFACE_ENTRY *dest ) > >the RIB_INTERFACE_ENTRY is a struct defined like so: > >struct RIB_INTERFACE_ENTRY >{ > char const * entry_name; > int type; > int subtype; >} > >I've attempted to define the struct in C# like so: > >public struct RIBInterfaceEntry >{ > [MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )] > public string name; > int entrytype; > int subtype >} > >and wrapped the c++ method like so: > >[DllImport( "mss32.dll", >EntryPoint="AIL_enumerate_filter_attributes" )] private static >extern int EnumFilterAttributes( IntPtr filter, ref int next, >ref RIBInterfaceEntry dest ); > >and call it like: > >IntPtr next = 0; > >public RIBInterfaceEntry[] GetFilterAttributes( DigitalFilter >filter ) { while ( true ) { > RIBInterfaceEntry entry = new RIBInterfaceEntry(); > if ( EnumFilterAttributes( filter.Handle, ref next, ref >entry ) == 0 ) break; > // add the entry to an arraylist >} > >// return arraylist.ToArray() >} > >The DigitalFilter is another struct populated from another >unmanaged call which holds all the available filters for a >digital audio driver. When the app is run, and the >GetFilterAttributes() method called, I don't get any run-time >arrays, and stepping through the code shows the >RIBInterfaceEntry[] array has a length of 2, but the >individual properties of a specific entry don't seem right. >The "Name" property has what appears to be a null terminator >in it, and the 2 int properties are 0 (which is a valid value >for the struct). But I was hoping to at least see a >descriptive name of the entry, as in a C++ application I have >that does basically the same thing (enumerates attributes, >printf them to console window). > >Thanks again, and in advance for any advice. > >Ron >