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 / June 2004

Tip: Looking for answers? Try searching our database.

Returning variable length string from eVC++ 4 to .Net CF

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TimRegan - 23 Jun 2004 13:43 GMT
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.

Rate this thread:







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.