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