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.

How to Marshaling LPSTR (MFC) to 'Ref String' in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mahesh b - 04 May 2004 20:14 GMT
We have a existing C++ component ( DLL ) that exposes a function ( signature shown below ) called ?GetNextItem?.
   C++ Signature:
   HRESULT GetNextItem(LPSTR szFolderEntryID, LPSTR szMessageEntryID, LPSTR szXMLout, LPSTR szDateFrom, LPSTR szDateTo);


When we set a reference to this dll in my c# project ( create an Interop ), the signature is changed as follows.
   C# Interop signature:
   public abstract new void GetNextItem ( System.String szFolderEntryID , System.String szMessageEntryID , System.String szXMLout ,
                                          System.String szDateFrom , System.String szDateTo )


With this, C# forces me to send parameter ?szXMLout? to C++ component ?by val?. In other words, I will not be able to read the changes done to the ?szXMLout?, within C++, in C# side of the code.

We want to send in the ?szXMLout? by reference to C++ from C# component, so that we can read back the changes to the parameter from the C# side of the code.

Question: How should I change the signature on the C++ side that will result in the interop will accept the parameter ?szXMLout? as ?By Ref? instead of the default ?by val??

Thanks,
Wang Jie - 17 Jun 2004 04:06 GMT
Hi, mahesh.
I suggest you use System.Text.StringBuilder instead of System.String to
receive string from your C++ DLL.
Here is a code block shows you how I call SendMessage() in user32.dll to get
the text of a text box.

// DLL import in my "UnsafeNativeMethods" class.
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int MSG, int wParam,
System.Text.StringBuilder lParam);

...

// Here we get the text from a text box.
int length = (int)UnsafeNativeMethods.SendMessage(this.Handle,
UnsafeNativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

if (length > 0)
{
     StringBuilder sb = new StringBuilder(length + 1);
     UnsafeNativeMethods.SendMessage(this.Handle,
UnsafeNativeMethods.WM_GETTEXT, sb.Capacity, sb);
     buffer = sb.ToString();
}

Hope this helps. :)

> We have a existing C++ component ( DLL ) that exposes a function ( signature shown below ) called ?GetNextItem?.
>     C++ Signature:
[quoted text clipped - 6 lines]
>
> With this, C# forces me to send parameter ?szXMLout? to C++ component ?by val?. In other words, I will not be able to read the changes done to the
?szXMLout?, within C++, in C# side of the code.

> We want to send in the ?szXMLout? by reference to C++ from C# component, so that we can read back the changes to the parameter from the C# side of
the code.

> Question: How should I change the signature on the C++ side that will result in the interop will accept the parameter ?szXMLout? as ?By Ref?
instead of the default ?by val??

> Thanks,
> --------------------------------
[quoted text clipped - 3 lines]
>
> <Id>jOLkAY13KEmZslQU6HtGEg==</Id

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.