C# System.String parameter passed into an unmanaged C++ DLL
Lost..... How do I call an unmanaged C++ DLL class member function from C# code? I can change any and all of these definitions. I just need to get a string from the C# world to the C++ world without pulling out the rest of my hair.
Using VS 2008
Unmanaged C++ Code in a DLL:
class MyClass
{
public:
bool Foo( char* str);
}
C# code that won't compile:
System.String test = "Hi there!";
MyClass c = new MyClass();
c.Foo( test );
Re: C# System.String parameter passed into an unmanaged C++ DLL
Well, if the string will be modified from within the C++ dll you should pass in a StringBuilder. Anyhow, that does not seem to be your problem as you say your code does not compile. If that is the case, your function definition in C# is incorrect, so show us that.
Re: C# System.String parameter passed into an unmanaged C++ DLL
Don't have one.
Why, you ask? Since I have included the DLL as a reference, I didn't think I needed a C# function definition. I'm calling all kinds of other functions from the DLL with no problem. If I leave out the string param or change it to an int, everything works as expected. It's just this string stuff that's stopping me.
Oh, and it won't be modified, just passed.
Disclosure: Long time C/C++ guy. Not a C# expert. Feel free to learn me. I'm all ears.
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Re: C# System.String parameter passed into an unmanaged C++ DLL
I'm confused. What "important bits" am I missing? I thought I had included all that was needed, else I would have included the rest. Can you be more specific?
Re: C# System.String parameter passed into an unmanaged C++ DLL
Originally Posted by thumperj
I'm confused. What "important bits" am I missing? I thought I had included all that was needed, else I would have included the rest. Can you be more specific?
Thanks.
Pretty much everything
Are you compiling your C++ as C++/CLI? How are you invoking the C++ function from C#. Whats the full class declaration in C# including the definition of the method you're calling. You've already provided the error which was also needed Can you also include the source for functions which you can call successfully?
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Re: C# System.String parameter passed into an unmanaged C++ DLL
Der... yeah. That would make sense:
Error The best overloaded method match for 'MyClass.MyClassClass.Foo(sbyte*)' has some invalid arguments
Error Argument '1': cannot convert from 'string' to 'sbyte*'
Re: C# System.String parameter passed into an unmanaged C++ DLL
Hmmm.... I guess I'm even more confused than I thought I was. I thought I had answered all those questions. Let me try again.
The C++ is unmanaged, which is what I thought meant it was not CLI. It's definitely not CLI. Just plain ol' C++.
I'm invoking the function from C# exactly like in the example in my original post. I new the object and call the function.
I don't have a class definition in C# for the C++ object. It's included as a Reference as the DLL. I never needed to put anything in the C# except "using MyClass;"
As for an example of functions I can call successfully, just take out the char* param in the C++ DLL function and the string param out of the C# code and I can call that same function all day. In fact, change it all to an int and I can call it all day. It's the string parameter that's halting my progress.
Re: C# System.String parameter passed into an unmanaged C++ DLL
Originally Posted by thumperj
Hmmm.... I guess I'm even more confused than I thought I was. I thought I had answered all those questions. Let me try again.
The C++ is unmanaged, which is what I thought meant it was not CLI. It's definitely not CLI. Just plain ol' C++.
I'm invoking the function from C# exactly like in the example in my original post. I new the object and call the function.
I don't have a class definition in C# for the C++ object. It's included as a Reference as the DLL. I never needed to put anything in the C# except "using MyClass;"
As for an example of functions I can call successfully, just take out the char* param in the C++ DLL function and the string param out of the C# code and I can call that same function all day. In fact, change it all to an int and I can call it all day. It's the string parameter that's halting my progress.
If the DLL was written in "plain ol' C++" then you could only import functions using P/Invoke like so
Re: C# System.String parameter passed into an unmanaged C++ DLL
Instead of continuing the back and forth, I've created a simple example that demostrates the issue I'm trying to resolve. I've attached it to this thread.
Check out Form1.cs, line 27. Just uncomment the line and you'll see the compiler error.
Re: C# System.String parameter passed into an unmanaged C++ DLL
Thanks, vcdebugger. After your suggestion I've been off reading about MarshalAs. All new to me.
However, unless I've misunderstood or misplaced something, adding that to the code doesn't help with the compiler error referencing Foo:
>> Error: The best overloaded method match for 'MyClass.Class1.Foo(sbyte*)' has some invalid arguments
You can check out the attached zip file to see the error. It's all laid out nicely in the code, short, sweet and right to the point.
Re: C# System.String parameter passed into an unmanaged C++ DLL
If you were calling an exported function from the DLL, you would use the PInvoke as described by Chris. However, since you want to execute a member function of a class inside your DLL, things may be a bit more complicated.
Basically, you can either compile your C++ DLL with /clr. Or, you have to create another C++/CLI DLL that wraps your native DLL in a .NET class. The mechanism is frequently referred to as "It Just Works" or "IJW". Generally in these wrapper DLLs, you have to marshal the data types manually. All in all, it can be a real pain the first time you do it.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.