RE: passing string array to C++
- From: Bruce Parker <bparkerhsd@xxxxxxxxxxxxx>
- Date: Thu, 23 Jun 2005 10:49:07 -0700
Thanks for the help.
Does this apply to all marshalling of passing arrays of pointers? I have
other challenges in this area such as passing an array of pointers to class
instances. I am at the point where I am going to let Embedded C++ managed
these arrays and provide C++ methods for the C# to work with them.
In addition, will we see any improvements in this area with NETCF 2.0?
"Gary Lewis" wrote:
>
> OK, this may seem a bit convoluted, but here is the solution:
>
> The normal code signature to be called would look like this:
>
> private static extern int MyAdapt( int gbl, string[] strings, int count
> );
>
> where strings is an array of strings and count is the number of
> strings. gbl is irrelevant in this context.
>
> This signature cannot be used directly because NETCF 1.0 does not support
> it. The solution is to write C# code to convert the string array into a
> single delimited string (using a StringBuilder) and have C++ code convert
> it back into the desired form. Here is the C# code:
>
> // I assume you have code to fill this
> private string[] m_strings;
>
> // Signature of the C++ wrapper function
> [DllImport("Unmanaged.DLL")]
> private static extern int MyAdapt( int gbl, StringBuilder
> stringList, int count );
>
> // Compute size of StringBuilder (or guess :-)
> int size = 1; // Allows for terminator
>
> foreach ( string sz in m_strings )
> size += sz.Length + 1;
>
> // Fill a StringBuilder
> StringBuilder sb = new StringBuilder( size );
>
> foreach ( string sz in m_strings )
> {
> sb.Append( sz );
> sb.Append( '\0' );
> }
>
> sb.Append( '\0' );
>
> // Call the unmanaged code
> int rc = MyAdapt( 42, sb, m_strings.Length );
>
> Next, we create a C++ wrapper function that converts the string back into
> an array of strings. Note that this implementation converts the Unicode
> strings from NETCF to ANSI strings. The code can be simplified a good deal
> if this is not required. Just allocate pStrs as a WCHAR** and fill in the
> pointers directly by computing the proper offsets into stringList. No
> additional memory allocations are required in this case. If your C++ code
> can work with Unicode this I recommend NOT converting them to ANSI.
>
> Here is C++ code that includes conversion:
>
> UNMANAGED_API int MyAdapt( int gbl, LPCWSTR stringList, int count )
> {
> int ix, iy, iz; // Indexes
> int retCode = 0; // Return code to caller
>
> // Allocate array of string pointers
> char** pStrs = new char * [ count ];
>
> // Scan the embedded strings
> for ( ix = iz = 0; stringList[ ix ] != 0; ix++ )
> {
> // Compute start and length of current Unicode string
> LPCWSTR pszBegin = &stringList[ ix ];
>
> // Look for end of string
> for ( iy = 0; stringList[ ix + iy ] != 0; iy++ )
> ;
>
> // Allocate ANSI string area allowing for string termination
> pStrs[ iz ] = new char[ iy + 1 ];
>
> // Translate the string
> int ret = WideCharToMultiByte( CP_ACP, 0, pszBegin, iy + 1,
> pStrs[ iz ], iy + 1, NULL, NULL );
>
> // Set up for next string
> iz += 1; // Next output string
> ix += iy; // Next input string
> }
>
> // Now call the internal function (the function we are wrapping)
>
> // retcode = InternalMyAdapt( gbl, pStrs, count );
>
> // Must free strings we allocated - skip this if the internal
> function
> // is responsible for the string array.
> for ( ix = 0; ix < count; ix++ )
> delete [] pStrs[ ix ];
>
> // Delete the array itself (see above)
> delete pStrs;
>
> // Return result
> return retCode;
> }
>
> Disclaimer for all above code:
>
> // THIS CODE AND INFORMATION IS PROVIDED "AS-IS" WITHOUT WARRANTY OF
> // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
> // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
> // PARTICULAR PURPOSE.
>
>
.
- Follow-Ups:
- RE: passing string array to C++
- From: Gary Lewis
- RE: passing string array to C++
- From: Gary Lewis
- RE: passing string array to C++
- References:
- passing string array to C++
- From: Bruce Parker
- RE: passing string array to C++
- From: Gary Lewis
- passing string array to C++
- Prev by Date: Re: CAPICOM: Utilities.ByteArrayToBinaryString and odd number of bytes
- Next by Date: Re: Redirect stdout from unmanaged DLL
- Previous by thread: RE: passing string array to C++
- Next by thread: RE: passing string array to C++
- Index(es):
Relevant Pages
|