C++ calls c sharp's DLL implementation method

  • 2020-04-02 02:31:48
  • OfStack

SwfDotNet is written in C#, which is a particularly good library for reading and writing Swf files. This article describes how to make C++ call C# DLL dynamic link library files in a C++ project.

The specific implementation steps are as follows:

A, To create a C# DLL, you need to specify the application type as "class library" The code:


namespace CSLib
{
  public class Class1
  {
    private string name;

    public string Name
    {
      get
      {
        return name;
      }
      set
      {
        name = "Your Name: " + value;
      }
    }
  }
}

Second, C++ client, a console application The code:


#using "..debugCSLib.dll"
using namespace CSLib;

int _tmain(int argc, _TCHAR* argv[])
{
 Class1 ^c = gcnew Class1();

 c->Name = "zzj";

 printf("%sn", c->Name);

 return 0;
}

Three, here There are a few things to remember :

1. Use #using to refer to C# DLL instead of #include;

2. Don't forget using namespace CSLib;

3. Use C++/ CLR syntax to properly access managed objects, i.e., use '^' instead of asterisk '*'.
 


Related articles: