473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

data type in pInvoke?


I am trying to use pInvoke and I need to pass some data from a C++ dll
to C# app and vice-a-versa.

The data which needs to be passed to C# app is WCHAR* in the dll. How do
I marshal them? Do I need to allocate memory to the string in the dll?

I used this in the DLL
void GetMsg(LPTSTR msg)
{
wcsncpy(msg,lpszTheData,256);
}
and this in C# app

[DllImport("MyDll.dll")]
public static extern int GetMsg([MarshalAs(UnmanagedType.LPStr)]
ref string msg);

static void Main(string[] args)
{
string msg="Hello from .NET";
try
{
GetMsg(ref msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(msg);//this gives exception saying msg is
null
Console.ReadKey();
}

I also tried without the ref keyword and tried UnmanagedType.LPTStr &
UnmanagedType.LPWStr but none is working.

Please help.

Thanks & Regards,
Jeff
Jun 27 '08 #1
2 2039
Jeff A wrote:
>
I am trying to use pInvoke and I need to pass some data from a C++
dll to C# app and vice-a-versa.

The data which needs to be passed to C# app is WCHAR* in the dll. How
do I marshal them? Do I need to allocate memory to the string in the
dll?

I used this in the DLL
void GetMsg(LPTSTR msg)
{
wcsncpy(msg,lpszTheData,256);
}
and this in C# app

[DllImport("MyDll.dll")]
public static extern int
GetMsg([MarshalAs(UnmanagedType.LPStr)] ref string msg);

static void Main(string[] args)
{
string msg="Hello from .NET";
try
{
GetMsg(ref msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(msg);//this gives exception saying msg
is null Console.ReadKey();
}

I also tried without the ref keyword and tried UnmanagedType.LPTStr &
UnmanagedType.LPWStr but none is working.

Please help.

Thanks & Regards,
Jeff
Hi Jeff,

if you marshal your string as a lpstr you won't need the ref keyword,
the interop services will handle that for you. Have you looked at your
calling conventions? Visual C++ by default uses cdecl IIRC and the
interop system will assume stdcall (WinAPI) by default I think.

Regards Tim.

--

Jun 27 '08 #2
For pInvoke don't marshall your string as LPStr, its for char*.
Since you are using WCHAR*, use LPTStr or LPWStr, also don't pass the
strings as ref.

something like this,
public static extern int
GetMsg([MarshalAs(UnmanagedType.LPTStr)]string msg);

Now, one thing you need to take care is the size of the string.

if you just do this
string s1;
GetMsg(s1);

you won't get anything from the dll.

The string you pass should be long enough to hold the data you will be
passing back.
So, use something like this,
String s1 = new String('\0',512);
GetMsg(s1);

Secondly, in the C++ dll, you can't assign memory to the string/pointer
like this

void GetMsg(LPTSTR msg)
{
msg = new WCHAR[1024]; //this will compile fine, but your data won't be
passed to the calling .net application.
}

MSFT GUY, please correct me if I am wrong!!

Regards,
Ashutosh

Jeff A wrote:
>
I am trying to use pInvoke and I need to pass some data from a C++ dll
to C# app and vice-a-versa.

The data which needs to be passed to C# app is WCHAR* in the dll. How do
I marshal them? Do I need to allocate memory to the string in the dll?

I used this in the DLL
void GetMsg(LPTSTR msg)
{
wcsncpy(msg,lpszTheData,256);
}
and this in C# app

[DllImport("MyDll.dll")]
public static extern int GetMsg([MarshalAs(UnmanagedType.LPStr)]
ref string msg);

static void Main(string[] args)
{
string msg="Hello from .NET";
try
{
GetMsg(ref msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(msg);//this gives exception saying msg is
null
Console.ReadKey();
}

I also tried without the ref keyword and tried UnmanagedType.LPTStr &
UnmanagedType.LPWStr but none is working.

Please help.

Thanks & Regards,
Jeff
Jun 27 '08 #3

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

Similar topics

3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
2
by: Brian Henry | last post by:
I want to list out a directory listing along with showing the file type name (like explorer does when it says something like "MyDoc.DOC - Microsoft Word Document" How do I get that file type name...
0
by: Dgdege | last post by:
Hi all, I want to wrapp an exiting API written in C++ to .NET using managed C++ extensions. I want my managed C++ class library to link with the unmanaged dll. So I don't use PInvoke for...
0
by: Petar Popara | last post by:
I've got this error calling one DLL func from my VB.net app: An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in Test.exe Additional...
0
by: Marek | last post by:
Hi I need to call various functions in a native C++ DLL (FORTRAN eventually too) - passing integers, doubles, (pointers and arrays to both of these as well) and ultimately structures too. I was...
4
by: Iouri | last post by:
I have tried the code posted by Microsoft here http://support.microsoft.com/?scid=kb;EN-US;322090 It works fine in VS2003. But when I try the same code in VS2005 I am getting the following error...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
0
by: emitojleyes | last post by:
Hi everyone! i'm new at this forum as well as programming in .net I have to develop a module that can use and configure a fingerprint detector; its documentation is written in C, as you will see...
20
by: =?Utf-8?B?ZW1pdG9qbGV5ZXM=?= | last post by:
Hi everyone: i read from the documentation of a dll that there is a struct that uses char for storing an IP address. How can it be? and how come i can get to representate the same value in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.