Hi All,
I'm trying to pass a variable length string from unmanaged code to
managed code. It's actually from a DLL written for the Pocket PC 2003
in embedded Visual C++ 4 to an app written for the Pocket PC 2003
using the .Net Compact Framework in Visual Studio .Net.
What I'm trying to do is allocate the string in the unmanaged C++ and
pass it back as an out parameter into the C#. Though I can
successfully signal the length of the string from unmanaged to
managed, the char * itself (called retrievedString in my code) remains
null on the C# side. Any hints you can give would help loads.
Here's the code:
---------------------- Managed Code ----------------------
public unsafe class Form1 : System.Windows.Forms.Form
{
[snip]
[DllImport("StringTestUnmanaged2.dll")]
protected extern static void GetString(out int stringSize, out char
* gotString);
[snip]
private void getStringButton_Click(object sender, System.EventArgs
e)
{
int stringLength;
char * retrievedString;
GetString(out stringLength, out retrievedString);
stringTextBox.Text = new string(retrievedString);
}
}
---------------------- Unmanaged Code .h ----------------------
char toSend[] = {'h', 'a', 'l', 'l', 'o', '\0'};
extern "C" STRINGTESTUNMANAGED2_API void GetString( int * stringSize,
char * gotString );
(where STRINGTESTUNMANAGED2_API is defined to __declspec(dllexport))
---------------------- Unmanaged Code .cpp ----------------------
STRINGTESTUNMANAGED2_API void GetString( int * stringSize, char *
gotString )
{
gotString = (char *)malloc(STRING_SIZE);
for (int i = 0; i < STRING_SIZE; i++)
{
gotString[i] = toSend[i];
}
*stringSize = STRING_SIZE;
return;
}
------------------------------------------------------------------
Cheers,
Tim.
PS I've read Bart Jacobs' excellent post here
http://groups.google.com/groups?selm=1069024092.24124%40seven.kulnet.kuleuven.ac.be
http://tinyurl.com/2na6e but my code still isn't working so I wonder
if it's the unmanaged side that I've messed up.
Naveen K Kohli - 28 Jun 2004 00:40 GMT
Try using StringBuilder as your out parameter.
Naveen K Kohli
http://www.netomatix.com
> Hi All,
>
[quoted text clipped - 59 lines]
>
> PS I've read Bart Jacobs' excellent post here
http://groups.google.com/groups?selm=1069024092.24124%40seven.kulnet.kuleuven.ac.be
> http://tinyurl.com/2na6e but my code still isn't working so I wonder
> if it's the unmanaged side that I've messed up.