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

Tip: Looking for answers? Try searching our database.

How to Marshal wchar**

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric Carlson - 08 Dec 2004 15:27 GMT
I have a .Net Com Server being called by an unmanaged c++ program.  They both
implement a common interface which passes a string by means of a double
pointer (c++  interface: [in, out] wchar_t**).

In the .Net Com server I am supposed to free the memory and allocate new
memory for this string since I have to read the string it points to, and the
point it to a new string to pass data back out.

I have used "ref string" which did not work since it was not allocating new
memory space for the strings sent back out.

I tried "ref StringBuilder" but that did not work because of a current bug
in the .Net framework, more than 16 characters being sent in will cause an
error to occur, see the following article:

Article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317577

I used the workaround in this artice which says to use a pointer for this
("ref IntPtr") and I have tried this but i cannot get it to work.  I cannot
read the incoming string or set the string.  I just get garbled data or cause
memory errors to occur.

Any help would be appreciated!
Signature

-----------------
Eric Carlson

Mattias Sj?gren - 08 Dec 2004 19:21 GMT
Eric,

>I used the workaround in this artice which says to use a pointer for this
>("ref IntPtr") and I have tried this but i cannot get it to work.  I cannot
>read the incoming string or set the string.  I just get garbled data or cause
>memory errors to occur.

Got some code to show? This should work.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Eric Carlson - 09 Dec 2004 17:09 GMT
When using Marshal.PtrToStringUni to try and get a string from the incoming
pointer, the string just contains garble.  And when i try and write to the
pointer using Marshal.StringToCoTaskMemUni, the data is not correctly
written, since the c++ client has some kind of error (can't see the error
though).

public interface MyInterface
{
[PreserveSig]
int Validate([MarshalAs(UnmanagedType.LPWStr)] string PathName, ref IntPtr
Answers);

//The c++ interface definition:
//HRESULT Validate([in, string] wchar_t* PathName,[in, out, unique, string]
wchar_t** Answers);
}

public class ComServer : MyInterface
{
public ComServer()
{
}

public int Validate(string Pathname, ref IntPtr Answers)
{

string temp;

temp = Marshal.PtrToStringUni(Answers);

ValidatorForm VF = new ValidatorForm();

VF.PromptData.Text = CK_and_Dest_Pathname + "\n\r\n\r\n\r" + temp;

//Get some text to send back through 'Answers'
VF.ShowDialog();
           
Marshal.FreeCoTaskMem(Answers);

//Get text to send back
string str = VF.PromptAnswer;

Answers = Marshal.StringToCoTaskMemUni(str);

}

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.