RE: passing string array to C++



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.
>
>
.



Relevant Pages

  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: invalid lvalue in assignment
    ... it's supposed to be an array of strings, ... there's no way of associating nrows and ncolumns with that. ... void choparray(char **s, int nrows, int ncolumns); ...
    (comp.lang.c)
  • Re: reading strings from file
    ... > it opens the file correctly but then the sscanf returns 0, ... those pointers need to point to something. ... strings to random locations in memory. ... in itself an array of char, so to allocate an array of strings, you need ...
    (comp.lang.c)
  • passing an array of LPSTR to C++ by reference
    ... I have written a VB program that passes an array of strings to a C++ ... The c++ dll is supposed to fill the array of strings with the ... GetJobIds(LPSTR & strReturnedJobId, int ...
    (comp.lang.cpp)
  • Re: simple array question
    ... I am trying to pass a function an array of strings, ... int smallest; ... void s38sort(e_type *array, size_t nmemb) ...
    (comp.lang.c)