c++/cli: passing to a function that takes char*

A

Abubakar

Hi,

I compiled a simple code in c++/cli 2k5

public ref class Class1
{
public : void doit (char * c )
{

}
};

its now built into a managed dll. Now I create a new C# project and include
the reference of above compiled dll into this new project.

cppmngdlib.Class1 c = new cppmngdlib.Class1();
string s = "hello";

now I want to call the "doit" function. I cant just pass in the string of
C#. So how do I call it?

Regards,

-ab.
 
C

Carl Daniel [VC++ MVP]

Abubakar said:
Hi,

I compiled a simple code in c++/cli 2k5

public ref class Class1
{
public : void doit (char * c )
{

}
};

its now built into a managed dll. Now I create a new C# project and
include the reference of above compiled dll into this new project.

cppmngdlib.Class1 c = new cppmngdlib.Class1();
string s = "hello";

now I want to call the "doit" function. I cant just pass in the
string of C#. So how do I call it?

You'd be much better off declaring the parameter as System::String^ instead
of the char*, since that's what C# and other .NET languages will be passing
to the function by default.

If you really want the function parameter to be char *, then you need to use
Marshal.StringToHGlobalAnsi to get a pointer to a narrow-character string
(as type IntPtr) and then (unsafe) cast that to System::Byte*, since that's
what the "char *" in your C++ function is in .NET terms. You'd also then be
responsible for cleaning up the HGlobal containing your string since it
won't be GC'd. But really, don't do this - change your function to take
System::String^ to be a good .netizen.

-cd
 
G

Guest

I compiled a simple code in c++/cli 2k5
public ref class Class1
{
public : void doit (char * c )
{

}
};

its now built into a managed dll. Now I create a new C# project and include
the reference of above compiled dll into this new project.

cppmngdlib.Class1 c = new cppmngdlib.Class1();
string s = "hello";

now I want to call the "doit" function. I cant just pass in the string of
C#. So how do I call it?

If your intention is to create a .NET class library for use in other
languages, you best use System::String^ instead of char*.

System::String is a true .NET string, whereas char* is a simple character
pointer.

Otherwise you will make life much harder for the users of your class library.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
?

=?iso-8859-1?Q?Kim=20Gr=e4sman?=

Hi Abubakar,
public ref class Class1
{
public : void doit (char * c )
{
}
};
its now built into a managed dll. Now I create a new C# project and
include the reference of above compiled dll into this new project.

Adding to the other replies, if you actually need a char* inside your class,
you can use Marshal::StringToHGlobalAnsi etc, as mentioned by Carl, in your
class instead of at every call site.

Something like this:

public ref class Class1
{
public:
void doit(System::String^ s)
{
// re-package as char*
realDoIt(c);
}

private:
void realDoIt(char* c)
{
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top