Using a C++ DLL in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    Using a C++ DLL in C#

    I have a DLL written in C++, and it contains functionality that I need for a C# program I'm writing.

    I know the names of the methods that I need, in fact I have all the method names in the DLL file, it's just using them that I'm having a problem with. Originally I thought I was out of luck because I couldn't add the DLL as a reference to my solution, but I found out how to use DllImport and tried that, but I couldn't get anywhere, so I searched Google and came up with something. This is my code for one of the functions in FATXLib.dll:

    Code:
    [DllImport("FATXLib.dll")]
    public static extern List<string> CheckDivices(FATX.DeviceType.Types DeviceToSearchFor);
    Yeah, the word device is spelled wrong in the dll, so I kept it that way.

    Anyway, I then have this method:

    Code:
    public static List<string> Main2()
    {
        return CheckDivices(FATX.DeviceType.Types.FATX_DEVICE_360HDD);
    }
    And finally I have a button on my form which, when clicked, does this:

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
        List<string> devices = Main2();
        MessageBox.Show(devices.Count.ToString());
    }
    At which point I get this error:

    Cannot marshal 'return value': Generic types cannot be marshaled.

    I'm stumped. How can I get around this?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Are you sure your C++ dll returns a List<> ?? Seems unlikely since List<> is a .NET type. Not impossible, just highly unlikely. I would sooner suspect it returns an array, or an address to an array.

    It's a fairly pricey library. Surely the maker must supply some support and instructions for it's use.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Yah, it is expensive, but it came with a program I downloaded and I wanted to try using it. I found an alternative library that's not only free, it's open source, and it does exactly what I want.

      Comment

      Working...