Marshalling string to PWSTR

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
 
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
 
M

mehr13

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
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top