|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Marshalling string to PWSTRI am trying to call a method from a native DLL which takes a PWSTR as an input param. From my managed code (C#), I want to pass a string as a parameter. How do I convert a C# string to a PWSTR? Currently I am using [DllImport("Some.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SomeMethod")] public static extern bool SomeMethod(IntPtr fileName); //PWSTR and calling as string str = "roshan"; bool val = SomeMethod(Marshal.StringToHGlobalAnsi(str)); My native method has the signature bool SomeMethod(PWSTR fileName) If I print the received value using printf it works fine, but when I use it as a parameter to some Win32 APIs it fails. The problem seems to be that I am converting the string to an ANSI string and not unicode /wide char (PWSTR) that the native method expects. Using Marshal.StringToHGlobalAuto() & Marshal.StringToHGlobalUni() don't work. Any help is greatly appreciated. Thanks, Roshan Hello, Roshan!
Try [DllImport("Some.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SomeMethod")] public static extern bool SomeMethod(([MarshalAs(LPWStr)] string fileName); R> I am trying to call a method from a native DLL which takes a PWSTR as R> an input param. From my managed code (C#), I want to pass a string as R> a R> parameter. How do I convert a C# string to a PWSTR? R> Currently I am using R> [DllImport("Some.dll", SetLastError = true, CharSet = R> CharSet.Unicode, R> EntryPoint = "SomeMethod")] R> public static extern bool SomeMethod(IntPtr fileName); R> //PWSTR R> and calling as R> string str = "roshan"; R> bool val = SomeMethod(Marshal.StringToHGlobalAnsi(str)); R> My native method has the signature R> bool SomeMethod(PWSTR fileName) R> If I print the received value using printf it works fine, but when I R> use it as a parameter to some Win32 APIs it fails. The problem seems R> to R> be that I am converting the string to an ANSI string and not unicode R> /wide char (PWSTR) that the native method expects. R> Using Marshal.StringToHGlobalAuto() & Marshal.StringToHGlobalUni() R> don't work. R> Any help is greatly appreciated. R> Thanks, R> Roshan Hi Vadym,
I already tried using [MarshalAs(UnmanagedType.LPWStr)]. It doesn't work. In my native DLL if I print the parameter using printf, I get just the first character. Regards, Roshan Vadym Stetsyak wrote: Show quote > Hello, Roshan! > > Try > > [DllImport("Some.dll", SetLastError = true, CharSet = > CharSet.Unicode, > EntryPoint = "SomeMethod")] > public static extern bool SomeMethod(([MarshalAs(LPWStr)] string fileName); > > > R> I am trying to call a method from a native DLL which takes a PWSTR as > R> an input param. From my managed code (C#), I want to pass a string as > R> a > R> parameter. How do I convert a C# string to a PWSTR? > > R> Currently I am using > > R> [DllImport("Some.dll", SetLastError = true, CharSet = > R> CharSet.Unicode, > R> EntryPoint = "SomeMethod")] > R> public static extern bool SomeMethod(IntPtr fileName); > R> //PWSTR > > R> and calling as > > R> string str = "roshan"; > R> bool val = SomeMethod(Marshal.StringToHGlobalAnsi(str)); > > R> My native method has the signature > > R> bool SomeMethod(PWSTR fileName) > > R> If I print the received value using printf it works fine, but when I > R> use it as a parameter to some Win32 APIs it fails. The problem seems > R> to > R> be that I am converting the string to an ANSI string and not unicode > R> /wide char (PWSTR) that the native method expects. > > R> Using Marshal.StringToHGlobalAuto() & Marshal.StringToHGlobalUni() > R> don't work. > > R> Any help is greatly appreciated. > > R> Thanks, > R> Roshan > > > -- > Regards, Vadym Stetsyak > www: http://vadmyst.blogspot.com Hello, Roshan!
printf() is used to print ANSI strings, since you're passing UNICODE string then you have to use wprintf(...); R> I already tried using [MarshalAs(UnmanagedType.LPWStr)]. It doesn't R> work. In my native DLL if I print the parameter using printf, I get R> just the first character. R> Regards, R> Roshan R> Vadym Stetsyak wrote: Show quote >> Hello, Roshan! >> Try >> [DllImport("Some.dll", SetLastError = true, CharSet = >> CharSet.Unicode, >> EntryPoint = "SomeMethod")] >> public static extern bool SomeMethod(([MarshalAs(LPWStr)] string >> fileName); >> R> I am trying to call a method from a native DLL which takes a PWSTR >> as >> R> an input param. From my managed code (C#), I want to pass a string >> as >> R> a >> R> parameter. How do I convert a C# string to a PWSTR? >> R> Currently I am using >> R> [DllImport("Some.dll", SetLastError = true, CharSet = >> R> CharSet.Unicode, >> R> EntryPoint = "SomeMethod")] >> R> public static extern bool SomeMethod(IntPtr fileName); >> R> //PWSTR >> R> and calling as >> R> string str = "roshan"; >> R> bool val = SomeMethod(Marshal.StringToHGlobalAnsi(str)); >> R> My native method has the signature >> R> bool SomeMethod(PWSTR fileName) >> R> If I print the received value using printf it works fine, but when >> I >> R> use it as a parameter to some Win32 APIs it fails. The problem >> seems >> R> to >> R> be that I am converting the string to an ANSI string and not >> unicode >> R> /wide char (PWSTR) that the native method expects. >> R> Using Marshal.StringToHGlobalAuto() & Marshal.StringToHGlobalUni() >> R> don't work. >> R> Any help is greatly appreciated. >> R> Thanks, >> R> Roshan >> -- >> Regards, Vadym Stetsyak >> www: http://vadmyst.blogspot.com Try declaring the C++ function param as BSTR;
bool SomeMethod(BSTR fileName); [DllImport("Some.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SomeMethod")] public static extern bool SomeMethod(([In, MarshalAs(UnmanagedType.BStr)] string fileName); MH Roshan wrote: Show quote > Hi Vadym, > > I already tried using [MarshalAs(UnmanagedType.LPWStr)]. It doesn't > work. In my native DLL if I print the parameter using printf, I get > just the first character. > > Regards, > Roshan > > Vadym Stetsyak wrote: > > Hello, Roshan! > > > > Try > > > > [DllImport("Some.dll", SetLastError = true, CharSet = > > CharSet.Unicode, > > EntryPoint = "SomeMethod")] > > public static extern bool SomeMethod(([MarshalAs(LPWStr)] string fileName); > > > > > > R> I am trying to call a method from a native DLL which takes a PWSTR as > > R> an input param. From my managed code (C#), I want to pass a string as > > R> a > > R> parameter. How do I convert a C# string to a PWSTR? > > > > R> Currently I am using > > > > R> [DllImport("Some.dll", SetLastError = true, CharSet = > > R> CharSet.Unicode, > > R> EntryPoint = "SomeMethod")] > > R> public static extern bool SomeMethod(IntPtr fileName); > > R> //PWSTR > > > > R> and calling as > > > > R> string str = "roshan"; > > R> bool val = SomeMethod(Marshal.StringToHGlobalAnsi(str)); > > > > R> My native method has the signature > > > > R> bool SomeMethod(PWSTR fileName) > > > > R> If I print the received value using printf it works fine, but when I > > R> use it as a parameter to some Win32 APIs it fails. The problem seems > > R> to > > R> be that I am converting the string to an ANSI string and not unicode > > R> /wide char (PWSTR) that the native method expects. > > > > R> Using Marshal.StringToHGlobalAuto() & Marshal.StringToHGlobalUni() > > R> don't work. > > > > R> Any help is greatly appreciated. > > > > R> Thanks, > > R> Roshan > > > > > > -- > > Regards, Vadym Stetsyak > > www: http://vadmyst.blogspot.com |
|||||||||||||||||||||||