简体   繁体   中英

Reference C++/CLI class library (compiled dll) in C# project

I have a very simple C++ class library that has two account types in it called Checking and Savings . I have built this project and compiled it into a .dll called Accounts. I have referenced the Accounts.dll in my C# Console application. To no avail, I am trying to use the static classes deposit and withdraw from the Savings and Checking classes respectively. I do not see the functions show up in the Object Browser or Intellisense and I am not able to get it to build when I try to access those functions, otherwise I am able to build and run when I comment out the call to mySavings.deposit(arg1,arg2)...

Any idea what I am doing incorrectly here? I often reference .dlls from other projects and third parties but this is the first time referencing a C++ .dll within a C# project.

C++ Class Library

#pragma once
using namespace System;

namespace Accounts {

  public ref class Savings
  {
    public:
        unsigned accountNumber;
        double balance;
        static double deposit(Savings s, double amount)
        {
            s.balance += amount;
            return s.balance;
        }
  };

  public ref class Checking
  {
    public:
        unsigned accountNumber;
        double balance;
        static double withdraw(Checking c, double amount)
        {
            c.balance -= amount;
            return c.balance;
        }
  };
}

C# Console Application that references the above compiled dynamic link library

using Accounts;

class Program
{
  static void Main(string[] args)
  {
    Savings mySavings = new Savings();  // works but object is empty
    mySavings.deposit(mySavings, 100);  // still breaks
  }
}

I am getting the following error: 'Accounts.Savings' does not contain a definition for 'deposit' and no extension method 'deposit' accepting a first argument of type 'Accounts.Savings' could be found (are you missing a using directive or an assembly reference?)

Any help would be greatly appreciated.

You're thinking that C# can access C++ classes... it can't. But C++/CLI can create .NET types that C# can use. However, these follow the rules of .NET, which are sometimes different from C++.

First, you must use ref class (or ref struct or value class or value struct ) in your C++/CLI code:

public ref class Savings

Then, you need to initialize your C# reference to point to an object. Simply declaring a variable in C# is not enough to get an object default constructed (not for reference types, where the variable will be null or uninitialized, and not for value types, where the content will be all-zero or uninitialized, with no constructor called).

Savings mySavings = new Savings();

After that, you'll discover that C++/CLI can't reference .NET reference types except via tracking handles... so instead of

Savings&

you need

Savings% // tracking reference, but C# doesn't know what to do
         // with a function whose parameter is like this

or

Savings^ // tracking pointer, C# likes it fine, C++/CLI will need -> to access members

Finally, the syntax for calling a static function from C# is

Savings.deposit(mySavings, 100);
// ^^ class name, not object

But that probably should be a non-static member anyway, right?.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM