473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshalling string to PWSTR

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(IntP tr fileName); //PWSTR

and calling as

string str = "roshan";
bool val = SomeMethod(Mars hal.StringToHGl obalAnsi(str));

My native method has the signature

bool SomeMethod(PWST R 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.StringT oHGlobalAuto() & Marshal.StringT oHGlobalUni()
don't work.

Any help is greatly appreciated.

Thanks,
Roshan

Nov 29 '06 #1
2 5614
Hi Vadym,

I already tried using [MarshalAs(Unman agedType.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(LPWSt r)] string fileName);
RI am trying to call a method from a native DLL which takes a PWSTR as
Ran input param. From my managed code (C#), I want to pass a string as
Ra
Rparameter. How do I convert a C# string to a PWSTR?

RCurrently I am using

R[DllImport("Some .dll", SetLastError = true, CharSet =
RCharSet.Unicod e,
REntryPoint = "SomeMethod ")]
R public static extern bool SomeMethod(IntP tr fileName);
R//PWSTR

Rand calling as

Rstring str = "roshan";
Rbool val = SomeMethod(Mars hal.StringToHGl obalAnsi(str));

RMy native method has the signature

R bool SomeMethod(PWST R fileName)

RIf I print the received value using printf it works fine, but when I
Ruse it as a parameter to some Win32 APIs it fails. The problem seems
Rto
Rbe that I am converting the string to an ANSI string and not unicode
R/wide char (PWSTR) that the native method expects.

RUsing Marshal.StringT oHGlobalAuto() & Marshal.StringT oHGlobalUni()
Rdon't work.

RAny help is greatly appreciated.

RThanks,
RRoshan
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Nov 30 '06 #2
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(Unman agedType.BStr)] string fileName);

MH

Roshan wrote:
Hi Vadym,

I already tried using [MarshalAs(Unman agedType.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(LPWSt r)] string fileName);
RI am trying to call a method from a native DLL which takes a PWSTR as
Ran input param. From my managed code (C#), I want to pass a string as
Ra
Rparameter. How do I convert a C# string to a PWSTR?

RCurrently I am using

R[DllImport("Some .dll", SetLastError = true, CharSet =
RCharSet.Unicod e,
REntryPoint = "SomeMethod ")]
R public static extern bool SomeMethod(IntP tr fileName);
R//PWSTR

Rand calling as

Rstring str = "roshan";
Rbool val = SomeMethod(Mars hal.StringToHGl obalAnsi(str));

RMy native method has the signature

R bool SomeMethod(PWST R fileName)

RIf I print the received value using printf it works fine, but when I
Ruse it as a parameter to some Win32 APIs it fails. The problem seems
Rto
Rbe that I am converting the string to an ANSI string and not unicode
R/wide char (PWSTR) that the native method expects.

RUsing Marshal.StringT oHGlobalAuto() & Marshal.StringT oHGlobalUni()
Rdon't work.

RAny help is greatly appreciated.

RThanks,
RRoshan
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Nov 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5779
by: Rookie | last post by:
Hi, I had a question on DllImport. On importing a function from a VC++ dll using DllImport (to a C# program), the function argument data types and the return types may be of a type that is not supported by C#. In this case if I am not mistaken the system performs default marshalling - matching the data types to its most similar equivalent...
1
2679
by: Reynardine | last post by:
I am calling a C/C++ DLL from C# and I am marshalling the parameters to the API call by doing a type conversion for each parameter. For example, here is my C++ API method : short int XENO_API XcDatabaseCodes ( HWND hwnd, char FAR * pszDatabase, char FAR * HUGE * FAR * pszItemCategories
3
1532
by: awk | last post by:
Hi All I have a com dll written in VB6 (it's a User Function Library for my crystal reports - this allows me to write custom functions for Crystal which can be applied in Crystal formulas - none of this is relevant to the problem though (((I think)))). Anyway, the VB dll has one method that takes a string as an argument and returns a...
2
5539
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long
2
2476
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall GetChildren(GetChildrenParm *, Results *);
6
1495
by: Hareth | last post by:
string word = "cool"; in c# how do i do that in c++ i searched online and i was lost.....
2
3329
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I need to pass an array of RADIO_INFO2 structures to be filled by a function in the DLL. This is how the structure is defined in the C++ example that...
2
2138
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use SendMessage() with WM_COPYDATA to send a string data between two instances of a program. As part of the debugging I've written two methods one where a...
1
2523
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use SendMessage() with WM_COPYDATA to send a string data between two instances of a program. As part of the debugging I've written two methods one where a...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.