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 2 5598
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);
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.Unicode,
REntryPoint = "SomeMethod")]
R public static extern bool SomeMethod(IntPtr fileName);
R//PWSTR
Rand calling as
Rstring str = "roshan";
Rbool val = SomeMethod(Marshal.StringToHGlobalAnsi(str));
RMy native method has the signature
R bool SomeMethod(PWSTR 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.StringToHGlobalAuto() & Marshal.StringToHGlobalUni()
Rdon't work.
RAny help is greatly appreciated.
RThanks,
RRoshan
--
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:
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);
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.Unicode,
REntryPoint = "SomeMethod")]
R public static extern bool SomeMethod(IntPtr fileName);
R//PWSTR
Rand calling as
Rstring str = "roshan";
Rbool val = SomeMethod(Marshal.StringToHGlobalAnsi(str));
RMy native method has the signature
R bool SomeMethod(PWSTR 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.StringToHGlobalAuto() & Marshal.StringToHGlobalUni()
Rdon't work.
RAny help is greatly appreciated.
RThanks,
RRoshan
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
by: Hareth |
last post by:
string word = "cool";
in c#
how do i do that in c++
i searched online and i was lost.....
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |