By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,153 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,153 IT Pros & Developers. It's quick & easy.

Marshalling from C++ DLL to CSharp for const wchar_t * using Visual Studio 2008 SP1

P: 30
Dear All,

I am stuck on these C++ data types const wchar_t * since last week.

It worked well in C# console application under mentioned below
Expand|Select|Wrap|Line Numbers
  1.  unsafe static void Main(string[] args)
  2. {
  3.   IntPtr ld, data1, data2;
  4.   string str = "test.txt";
  5.   int UTF_8 = 1;
  7.   // Using PInvoke 
  8. [System.Runtime.InteropServices.DllImportAttribute("Test.dll", EntryPoint = "LoadFromFile")]
  9. public static extern System.IntPtr LoadFromFile(System.IntPtr aObj, System.IntPtr ex, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string aPath, int aWritable);
  11. [System.Runtime.InteropServices.DllImportAttribute("Test.dll", EntryPoint = "LoadFromFile_cset")]
  12. public static extern System.IntPtr LoadFromFile_cset(System.IntPtr aObj, System.IntPtr ex, int aEncoding, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string aPath, int aWritable);
  14.   // data1 and data2 should have some address
  15.   // get some address in console application heredata1 = LoadFromFile(ld, IntPtr.Zero, str, 1);
  16. data2 = LoadFromFile_cset(ld, IntPtr.Zero, UTF_8, str, 1);
  18.   // but I got can't get any address in C# Window Form
  20. }
Here is origina C++ functions
Expand|Select|Wrap|Line Numbers
  1.  struct Doc *API LoadFromFile (struct Loader *  aObj, struct Exception *  ex, const wchar_t *  aPath, int  aWritable) 
  3. struct Doc *API LoadFromFile_cset (struct Loader * aObj, struct Exception *     ex, int     aEncoding, const char * aPath, int aWritable) 
So far I knew Marshalling or PInvoke used in anywhere console or window form, the result should be the same.

But very strange for this time since I am using other party libraries so I couldn't debug inside.

I hope some one will advise me since I need to close the case by tomorrow.

Thanks and best regards
Nov 18 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.