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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KYAW KYAW OO
    New Member
    • Jan 2007
    • 30

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

    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
    Code:
     unsafe static void Main(string[] args)
    {
      IntPtr ld, data1, data2;
      string str = "test.txt";
      int UTF_8 = 1;
    
      // Using PInvoke 
    [System.Runtime.InteropServices.DllImportAttribute("Test.dll", EntryPoint = "LoadFromFile")]
    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);
    
    [System.Runtime.InteropServices.DllImportAttribute("Test.dll", EntryPoint = "LoadFromFile_cset")]
    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);
    
      // data1 and data2 should have some address
      // get some address in console application heredata1 = LoadFromFile(ld, IntPtr.Zero, str, 1);
    data2 = LoadFromFile_cset(ld, IntPtr.Zero, UTF_8, str, 1);
    
      // but I got can't get any address in C# Window Form
    
    }
    Here is origina C++ functions
    Code:
     struct Doc *API LoadFromFile (struct Loader *  aObj, struct Exception *  ex, const wchar_t *  aPath, int  aWritable) 
    
    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
    Last edited by Rabbit; Nov 19 '12, 01:23 AM. Reason: Please use code tags when posting code.
Working...