Hi,
I have a (Complied) C Library that needs to be called using c# code, some of
the function require char* parameter(in and out)
Here is the problem,
There is a function that expect an input char* parameter
for example
int AddString(const char* someStr)
And then there is another function to retrieve the value I pass in using the
above function
for example
int GetString(char* rntStr)
In C#
[DllImport(..... .)]
Int32 AddString(strin g someStr);
[DllImport(..)]
Int32 GetString(strin g rntString);
So when I input "aaaa" into someStr and get the return by calling GetString,
the rntString variable only return the first character ("a") and the length
of the string is 1.
Anyone know what's wrong with my code?
It the AssString declaration problem or the GetString declaration problem
(or both)?
thanks
Gasnic 9 20374
Yes, I do. You may have to set the Char type in the DllImport to
probably Ansi. Try that. If anyone reads this and gets an example for
char** and also for bools in a C++ struct then would like to see an
example of that. I think I know how to do it though.
Curtis http://www.ghostclip.com
The Premier Help System For Developers
I have tried to declare the function like this
Int32 AddString([MarshalAs(Unman agedType.AnsiBS tr) string someStr)
Int32 GetString([MarshalAs(Unman agedType.AnsiBS tr) ref string rntStr)
But I got the same thing.
I also tried UnmanagedType.L PStr and UnmanagedType.L PTStr, but both doesn't
work.
Actually should I use string as the parameter type?
thanks
Gnic
"Light" <Li********@gma il.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com... Yes, I do. You may have to set the Char type in the DllImport to probably Ansi. Try that. If anyone reads this and gets an example for char** and also for bools in a C++ struct then would like to see an example of that. I think I know how to do it though. Curtis http://www.ghostclip.com The Premier Help System For Developers
>[DllImport(..)] Int32 GetString(strin g rntString);
For output string parameters you should use StringBuilder as the
parameter type.
Also make sure that if you set the CharSet property in the DllImport
attribute, it should be set to Ansi.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. [DllImport(..)] Int32 GetString(strin g rntString);
For output string parameters you should use StringBuilder as the parameter type.
Also make sure that if you set the CharSet property in the DllImport attribute, it should be set to Ansi.
Doesn't C# default to CharSet.Ansi?
> Doesn't C# default to CharSet.Ansi?
It does, but since the original poster wrote
[DllImport(..)]
I couldn't know for sure whether or not he or she left the default or
specified something else.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hello Gnic,
I'm not a C programmer, but I think that's the expected behaviour: Your C library has a return value of char*, not char[]*.
Regards.
"Gnic" <ga****@hotmail .com> escribió en el mensaje news:O5******** ******@TK2MSFTN GP14.phx.gbl...
| Hi,
|
| I have a (Complied) C Library that needs to be called using c# code, some of
| the function require char* parameter(in and out)
|
| Here is the problem,
|
| There is a function that expect an input char* parameter
|
| for example
| int AddString(const char* someStr)
|
| And then there is another function to retrieve the value I pass in using the
| above function
| for example
| int GetString(char* rntStr)
|
| In C#
|
| [DllImport(..... .)]
| Int32 AddString(strin g someStr);
|
| [DllImport(..)]
| Int32 GetString(strin g rntString);
|
| So when I input "aaaa" into someStr and get the return by calling GetString,
| the rntString variable only return the first character ("a") and the length
| of the string is 1.
|
| Anyone know what's wrong with my code?
| It the AssString declaration problem or the GetString declaration problem
| (or both)?
|
| thanks
|
| Gasnic
"Gnic" <ga****@hotmail .com> wrote in message
news:OS******** *****@TK2MSFTNG P15.phx.gbl... I have tried to declare the function like this
Int32 AddString([MarshalAs(Unman agedType.AnsiBS tr) string someStr)
Int32 GetString([MarshalAs(Unman agedType.AnsiBS tr) ref string rntStr)
But I got the same thing.
I also tried UnmanagedType.L PStr and UnmanagedType.L PTStr, but both doesn't work.
Actually should I use string as the parameter type?
Mattias already suggested using StringBuilder for your GetString function
since the contents of the buffer look like it needs to be modified.
Here's an example of some code that works. Hopefully it will be somewhat
applicable to what you want to do:
..CPP file:
#include <windows.h>
char m_Text[1000];
extern "C" __declspec(dlle xport)
int AddString(const char* someStr)
{
strcpy(m_Text, someStr);
return 0;
}
extern "C" __declspec(dlle xport)
int GetString(char* rntStr)
{
strcpy(rntStr, m_Text);
return 0;
}
..CS file:
using System.Text;
using System.Runtime. InteropServices ;
class Program
{
[DllImport("Cool StringLibrary.d ll")]
static extern int AddString(strin g someStr);
[DllImport("Cool StringLibrary.d ll")]
static extern int GetString(Strin gBuilder rntStr);
static void Main(string[] args)
{
AddString("aaaa ");
StringBuilder rntStr = new StringBuilder() ;
GetString(rntSt r);
}
}
Thanks for all the helps, I have the problem fixed by using StringBuilder.
But now I have another problem:
The C Library have a struct that contain a unsigned char* field.
I read the MSDN PInvoke doc and it said unsigned char should map to byte in
..NET.
But what about unsigned char*?
should I use byte[] in the class in .NET ( but then I don't know the length
in advance and I have to pass the unsigned char* for the c library to fill
some data in)?
any comment?
thanks
"Gnic" <ga****@hotmail .com> wrote in message
news:O5******** ******@TK2MSFTN GP14.phx.gbl... Hi,
I have a (Complied) C Library that needs to be called using c# code, some of the function require char* parameter(in and out)
Here is the problem,
There is a function that expect an input char* parameter
for example int AddString(const char* someStr)
And then there is another function to retrieve the value I pass in using the above function for example int GetString(char* rntStr)
In C#
[DllImport(..... .)] Int32 AddString(strin g someStr);
[DllImport(..)] Int32 GetString(strin g rntString);
So when I input "aaaa" into someStr and get the return by calling GetString, the rntString variable only return the first character ("a") and the length of the string is 1.
Anyone know what's wrong with my code? It the AssString declaration problem or the GetString declaration problem (or both)?
thanks
Gasnic
"Gnic" <ga****@google. ca> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. Thanks for all the helps, I have the problem fixed by using StringBuilder.
But now I have another problem:
The C Library have a struct that contain a unsigned char* field. I read the MSDN PInvoke doc and it said unsigned char should map to byte in .NET. But what about unsigned char*?
should I use byte[] in the class in .NET ( but then I don't know the length in advance and I have to pass the unsigned char* for the c library to fill some data in)?
any comment?
I think you need to use IntPtr. Marshal.AllocHG lobal(),
Marshal.FreeHGl obal(), Marshal.ReadByt e(), and Marshal.WriteBy te() should
help you out. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andreas Müller |
last post by:
hi @all,
I have a char* string array and i want to fill it up in a while
loop:
while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);
...
}
after this the array should look like this:
|
by: Pokerkook |
last post by:
Hello,
If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:
49
49
10
49
52
|
by: Angel |
last post by:
I'm exporting a C-style function call with this syntax:
int getDate(char *date);
I'm trying to export to my c# app like this:
public static extern int getDate(System.IntPtr ptr);
public void getDate()
{
|
by: Jordan Tiona |
last post by:
How can I make one of these? I'm trying to get my program to store a string
into a variable, but it only stores one line.
--
"No eye has seen, no ear has heard, no mind can
conceive what God has prepared for those who love him"
1 Cor 2:9
|
by: gustavo |
last post by:
I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:
------------------------
struct prival PrivacyValues =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
| |
by: Michael B Allen |
last post by:
Hello,
Early on I decided that all text (what most people call "strings" )
in my code would be unsigned char *. The reasoning is that the elements
of these arrays are decidedly not signed. In fact, they may not even
represent complete characters. At this point I think of text as simple
binary blobs. What charset, character encoding and termination they use
should not be exposed in the interface used to operate on them.
But now I have...
|
by: Hongyu |
last post by:
Hi,
I have a datetime char string returned from ctime_r, and it is in the
format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars
including the last terminate char '\0', and i would like to remove the
weekday information that is "Wed" here, and I also would like to
replace the spaces char by "_" and also remove the "\n" char. I didn't
know how to truncate the string from beginning or replace some chars
in a string with another...
|
by: JohnCox |
last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode).
The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this:
SIMPLELIB_API int StrFirst(LPTSTR str, long num);
The second function is essentially the same thing, but with the order of the...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |