Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

Marshaling a String Array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rodger Brennan - 16 Dec 2004 18:11 GMT
I need to marshal as string array in C# to an IntPtr.  Ist there any way to
do this.  for instance.

string[] str = { "this", "should", "have", "a", "pointer" };
IntPtr ptr = Marshal.AllocHGlobal(Marhsal.SizeOf(str.Length * str.Length);
Marshal.StructureToPtr(str,ptr,false);

Thanks in Advance for any help.

Rodger
Mattias Sj?gren - 19 Dec 2004 23:04 GMT
Rodger,

>I need to marshal as string array in C# to an IntPtr.

How should the array be laid out in native memory?

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Rodger Brennan - 19 Dec 2004 23:18 GMT
It should be a PCWSTR "Pointer to an array of null-terminated Unicode
strings that contain the names of the attributes to retrieve for each
selected object"  Here is exactly what I need this for
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ad/ad/dsop_init
_info.asp

I'm trying to set the apwzAttributeNames.

Thanks,
Rodger

> Rodger,
>
[quoted text clipped - 3 lines]
>
> Mattias
Mattias Sj?gren - 05 Jan 2005 22:13 GMT
Rodger,

>It should be a PCWSTR "Pointer to an array of null-terminated Unicode
>strings that contain the names of the attributes to retrieve for each
>selected object"  Here is exactly what I need this for
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ad/ad/dsop_init
_info.asp

>I'm trying to set the apwzAttributeNames.

Sorry for the delay. Here's one way to do it that I think should work

int n = str.Length;
IntPtr[] stringPtrs = new IntPtr[n];
IntPtr array = Marshal.AllocHGlobal(IntPtr.Size * n);
for ( int i = 0; i < n; i++ ) {
 stringPtrs[i] = Marshal.StringToHGlobalUni(str[i]);
 Marshal.WriteIntPtr(array, i * IntPtr.Size, stringPtrs[i]);
}
dsop_init_info.apwzAttributeNames = array;

// call the API here

for ( int i = 0; i < n; i++ )
 Marshal.FreeHGlobal(stringPtrs[i]);
Marshal.FreeHGlobal(array);

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.