[.net] Calling a C++ Function from C#

Started by
3 comments, last by Arild Fines 18 years, 9 months ago
Hello, I want to do just what the title says, well..actually right now I have a std::vector that I would like to return..but I don't think that's possible..so I guess I can turn it into an array of structs...but i'm a bit confused..it seems i'll need a lot of marshalling here.. Can anyone suggest a good way to do this? the struct looks like this in C++ currently

struct Link 
{
string Title;
string o_Address;
string n_Address;
string File_Location;
string Status_Message;
};
I'm thinkin i'll have to change all those std::strings to character arrays? And then make a similar structure in C# (with marshaled as char array strings)..and then..it should work? Sorry, I'm at work right now and can't test this idea out..I was just thinking about it and wondering if this would be the way to do it.. Thanks, kag1 [Edited by - kag1 on August 16, 2005 9:15:26 AM]
Advertisement
P/Invoke is what you're looking for:
[DllImport("SomeCppLibrary.dll")]static extern void SomeFunction(Link theLink);

This completely depends on how you declared it in your compiled DLL. You must declare the C# Link struct the same way you declared the C++ one, except with the C# one, you'll be using the StructLayout attribute:

[StructLayout(LayoutKind.Sequential)]public struct Link {    string Title;    string o_Address;    string n_Address;    string File_Location;    string Status_Message;}

This is the method taken with most of the C++ to C# ports, like Tao and FMOD Ex. Enjoy, and good luck!
Rob Loach [Website] [Projects] [Contact]
Thanks for your reply rob..

I started messing around with returning the structs but got absolutely no where..I decided I'd try with some easier types and I still have no luck..maybe someone can give me some pointers of what i'm doing wrong..

here's a basic C Function
extern "C" __declspec(dllexport) LPCTSTR SayHello(){	return (LPCTSTR)"Hello";}

btw the only reason i'm using LPCTSTR is because I read thats what the CLR expects? If I can use something else let me know..because i'm not too familier with LPCTSTR at all..I don't even know if my cast works correctly..or if that's the way i'm suppose to do it

and here we have my C# P/Invoke
public class DLL_Function{            [DllImport("DLL_Function.dll", CharSet = CharSet.Auto)]             public static extern String SayHello();}


later on in the code
string Hello = Project.Main.DLL_Function.SayHello();

returns just jibberish.


Do I have to Marshal something as something? And If so...what was all that non-sense about using LPCSTRs?

Thanks,
kag1
Yes, for strings you'll probably have to perform marshalling. Strings in C# are a good bit more complex than they are in C++, where did you hear that LPCTSTR was expected? I've never heard that before.
Quote:Original post by Tape_Worm
Yes, for strings you'll probably have to perform marshalling.

The .NET interop marshaller should handle strings for you.

OP: Take a look at this MSDN topic: http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmarshalingstrings.asp. There are samples.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement