Jump to content
  • Advertisement
Sign in to follow this  
kag1

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

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!