Groups | Blog | Home
all groups > dotnet interop > january 2007 >

dotnet interop : How to convert String[] to IntPtr (LPCWSTR) ?


sergio.rykov NO[at]SPAM gmail.com
1/21/2007 4:53:18 AM
Marshal.StringTo... methods convert only one string to unmanged memory.
Native method takes LPCWSTR - an array of strings. How to convert
string[] to IntPtr ?

I couldn't test one my suggestion
.... = Marshal.StringToHGlobalUni("lalala\0lalala2\0lalala3");
sergio.rykov NO[at]SPAM gmail.com
1/21/2007 5:20:31 AM

[quoted text, click to view]

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

public IntPtr apwzAttributeNames;
}

dsopInitInfo.apwzAttributeNames = refToStringArray;

I could change atrributes in struct defenition:

[MarshalAs(UnmanagedType.LPWStr)]
public string apwzAttributeNames;

but it seems don't work...
Michael Phillips, Jr.
1/22/2007 10:42:44 AM
You could try using this declaration for your IntrPtr array:
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] lpwstrNames]

//Allocate managed memory for the wide strings
string[] wstrNames = new string[numberofStrings];

// Marshal the IntPtr[] to string[]
for ( int i = 0; i < numberofStrings; i++ )
wstrNames[i] = Marshal.PtrToStringUni(lpwstrNames[i]);

// Allocate memory for the IntPtr array
IntPtr[] lpwstrNames = new InPtr[numberofStrings];

// Marshal string[] to IntPtr[]
for ( int i = 0; i < numberofStrings; i++ )
lpwstrNames[i] = Marshal.StringToHGlobalUni(wstrNames[i]);



[quoted text, click to view]

AddThis Social Bookmark Button