R
Roshan
Hi List,
I 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
I 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