473,324 Members | 2,501 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,324 software developers and data experts.

Using DLLImport with a function requiring char *

I'm an experienced programmer, but a newbie to c#, so please be kind :)

In a c++, unmanaged, DLL of my own making I have a function called

void XGL_Core_SetCaption(XGL_Core * core, char * caption);

Using DLLImport I've gotten all functions working except for the ones
requiring char * . Anyone care to inform me how to pass the contents of a
c# string to this c++ function? I've tried passing string and char[], with
no luck.

Any thoughts?
Nov 16 '05 #1
5 7042
How did you declare your PInvoke signature?
If you modify the string pointed to by char*, you have to pass a
StringBuilder with sufficient size to hold the new string + terminating
null.
Else you can pass a System.String.
Note that you need to help the interop marshaler a bit when your native code
is a UNICODE build, by applying the MarshalAs(UnmanagedType.LPWStr)]
attribute to the argument.
(...., [In,Out, MarshalAs(UnmanagedType.LPWStr)]StringBuilder s)
else the interop marshaler converts wide-char strings to multi-byte by
default.
(...., [In,Out]StringBuilder s)

Willy.

"Jason Bell" <ha*********@yahoo.com> wrote in message
news:Xn*********************************@198.161.1 57.145...
I'm an experienced programmer, but a newbie to c#, so please be kind :)

In a c++, unmanaged, DLL of my own making I have a function called

void XGL_Core_SetCaption(XGL_Core * core, char * caption);

Using DLLImport I've gotten all functions working except for the ones
requiring char * . Anyone care to inform me how to pass the contents of a
c# string to this c++ function? I've tried passing string and char[],
with
no luck.

Any thoughts?

Nov 16 '05 #2
Thanks, I got it working by marshaling it as LPStr. LPWStr just passed the
first character in the string. Here's what I used:

[DllImport("XGL.dll", EntryPoint="XGL_Core_SetCaption",

ExactSpelling=false,CallingConvention=CallingConve ntion.Cdecl)]
public unsafe static extern void SetCaption( void * Core,
[MarshalAs(UnmanagedType.LPStr)]string s );

Thanks again.

Nov 16 '05 #3
Jason,
Well, actualy all characters are passed (as unicode characters), it's just
the native code that looks for a single null (0x00) byte in the input buffer
as string terminator.
Note that there is no need to specify LPStr as it's the default.

Willy.

"Jason Bell" <ha*********@yahoo.com> wrote in message
news:Xn*********************************@198.161.1 57.145...
Thanks, I got it working by marshaling it as LPStr. LPWStr just passed
the
first character in the string. Here's what I used:

[DllImport("XGL.dll", EntryPoint="XGL_Core_SetCaption",

ExactSpelling=false,CallingConvention=CallingConve ntion.Cdecl)]
public unsafe static extern void SetCaption( void * Core,
[MarshalAs(UnmanagedType.LPStr)]string s );

Thanks again.

Nov 16 '05 #4
heh that's bizarre, I tried just passing a string without marshalling
originally, and it didn't work. Now it seems to. Ah well, I must have
overlooked something at point.

Thanks again for your help.
Nov 16 '05 #5
There is always marshaling involved when transitioning from managed to
unmanaged.
The point is that if you don't specify the attribute, by default the
marshaler converts Unicode to MBSC and vice-versa.

Willy.

"Jason Bell" <ha*********@yahoo.com> wrote in message
news:Xn********************************@198.161.15 7.145...
heh that's bizarre, I tried just passing a string without marshalling
originally, and it didn't work. Now it seems to. Ah well, I must have
overlooked something at point.

Thanks again for your help.

Nov 16 '05 #6

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

Similar topics

0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
5
by: Bucky Pollard | last post by:
I am calling a PVCS DLL function from C#. According to the documentation, I can pass NULL to any output parameter that I do not want to receive data back for. How do I do this? Every thing I've...
0
by: baker_tony | last post by:
Hi, I thought I'd post my notes on how I went about accessing MFC C++ code in my web app after spending a day or more gathering information on how to do it. Please, let me know if you have a...
5
by: Ole | last post by:
Hi, I have a native dll that has a byte array to which I want a pointer to in my C# application but I will need some help to do that. the dll exports this function: void DllFunction (PBYTE...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
3
by: Elikhom | last post by:
Hi, I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is...
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...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
1
by: mrssuper | last post by:
Hey All, How do I declare a following types: char** unsigned char** in C# DllImport function?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.