How to Marshal wchar**

dotnet framework


  • 1. COM+ Migration of 2000 to 2003 OS
    Hi, Would it be possible to know where I could find a document explaining risks and problems due to the migration of a .NET application with COM+ components ? Thanks. Claudy
  • 2. Slow moving data from ADO.Net DataTable to Excel
    I am moving data from an ADO.NET DataTable object in C# and I am having performance issues. QueryTables.Add, at least when called from C#, seems to spin off its own thread. So I go doing a bunch of other stuff to an invisible sheet AFTER I want the data transfer to occur, however, even after everything else completes (including showing the file) it is still executing. I can't use Range.CopyFromRecordset as that method seems to interface with my .NET DataTable. So I am essentially setting cell values like so: for (int i=0; i< myDTable.Rows.Count; i++) { for (int j=0; j< myDTable.Columns.Count; j++) { myCell.Value = myDTable.Rows[i][j]; //this is pseudo } } and this is horribly inefficient, even for modest sized data sets. Any way to dump this to the sheet faster. I wouldn't mind a non-looping approach to putting a DataRow object in some Range, I just don't know how to do it. Thanks. thomas
  • 3. Exception in Microsoft Outlook.ApplicationClass.CreateItem
    I try using this code: Dim objOutlook As New Outlook.ApplicationClass Dim objMail As Outlook.MailItem = objOutlook.CreateItem(Outlook.OlItemType.olMailItem) in 2003. This code works fine ... as long as the user running this code has administrator-rights (more excatly: i run this programm no an terminal server ... ). If the user don't posess this right the code terminates with an COMException indicating that the user don't posess the right to run CreateItem. My question is: where can i control the right to invoke CreateItem? Thanks for your help.
  • 4. managed app using unmanaged dll to access LPT
    hi, we've to develop a winform-app (.NET 1.1 / C#) , which controls a device connected to the lpt port. the producer of the device provides a set of unmanaged dlls. till now they have been used in a VC++/MFC app and worked properly. in our .NET-app the same function-calls don't return (for hours...). there's no exception, event-log-entry or anything else. [DllImport("lpt.dll")] internal static extern int GetValue(int devicehandle); the error appears on default xp (home/prof) installations. interestingly the calls work on systems (xp prof./win2003 server) with visual studio .NET installed?! are there any configuration-possibilities? any security-concerns? thanks in advance, stefan

How to Marshal wchar**

Postby RXJpYyBDYXJsc29u » Fri, 10 Dec 2004 00:27:03 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:

 http://www.**--****.com/ ;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!
Eric Carlson

Re: How to Marshal wchar**

Postby Mattias Sjren » Fri, 10 Dec 2004 04:21:15 GMT


Got some code to show? This should work.


Mattias Sjren [MVP]  mattias @
 http://www.**--****.com/ |  http://www.**--****.com/ 
Please reply only to the newsgroup.

Re: How to Marshal wchar**

Postby RXJpYyBDYXJsc29u » Sat, 11 Dec 2004 02:09:10 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 

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

//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'

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

Answers = Marshal.StringToCoTaskMemUni(str);


Similar Threads:

1.Marshal.DestroyStructure or Marshal.FreehGlobal

I'm using Marshal::StructureToPtr to well...marshal a struct over to 
unmanaged memory and get its pointer.

Which method should I use to destroy the memory the pointer represents?  
Marshal.DestroyStructure or Marshal.FreehGlobal?  Does it matter?


2.Marshal.FreeHGlobal or Marshal.FreeCoTaskMem for PIDLs


I use p/invoke to call SHParseDisplayName exposed by shell32.dll and
this call returns an address of PIDL that it allocated in unmanaged
memory. I am expected to release it.

I am confused if I can use FreeHGlobal or FreeCoTaskMem for this
purpose. Does it make a difference in this case? Or I should use
something entirely different?


3.Use of Marshal.FreeCoTaskMem after Marshal.PtrToStructure

The documentation is not clear on the exact behaviour of the
Marshal.PtrToStructure method and whether it copies the contents of the
IntPtr region to a new managed object or whether it creates the managed
object to reference the same region of memory.

Question 1: should I call Marshal.FreeCoTaskMem after calling

Question 2: the Marshal.PtrToStructure method seems to create an object for
the structure -- should I be concerned with converting it to a value object?


4.Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem REVISISTED

5.Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem

6. Marshalling WCHAR[] struct member?

7. How to read a text file with wchar?

8. how to convert from System::String _gc to wchar

Return to dotnet framework


Who is online

Users browsing this forum: No registered users and 70 guest