RE: passing string array to C++
- From: garyle@xxxxxxxxxxxxxxxxxxxx (Gary Lewis)
- Date: Wed, 22 Jun 2005 20:34:15 GMT
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: Bruce Parker
- RE: passing string array to C++
- References:
- passing string array to C++
- From: Bruce Parker
- passing string array to C++
- Prev by Date: Re: Redirect stdout from unmanaged DLL
- Next by Date: LsaRegisterLogonProcess Help
- Previous by thread: RE: passing string array to C++
- Next by thread: RE: passing string array to C++
- Index(es):
Relevant Pages
|
Loading