0

I am working on a project where I write an application in C#. I want to use a .dll file and to bind the functions of it to my C# code. Does anybody has a step-by-step explanation how to do it? I am not an expert programmer but I understand the basics.

What I have: a DLL package (files: file.dll, file.lib, file.pdb)

I got the header file and there I can find the functions I want to bind. Theoretically, I should only bind these functions to my C# and then I don't care anymore about the .dll library or C code.

I have tried to solve the problem with: - C++/CLI - PInvoke

But it doesn't work for me.

// these is a part of the code of the header file. And that is all what I have to bind to my C# code.

class EXPORTINGDLL Application
{
    void * privatePtr;
protected: 

    bool   master;
public:

    Aplication(const char * UartName, bool isMaster);
    virtual ~Application();

    void Start();   // call only once please!

    void Connect();

    bool SendFrame (const uint8_t * data, size_t length);

    virtual void onConnected() = 0;
    virtual void onDisconnected() = 0;
    virtual void onDataReceived (const uint8_t * data, size_t length) = 0;
};
Alghedi
  • 1
  • 3
  • `I have tried to solve the problem with: - C++/CLI - PInvoke` Where is the code for that? – slow Jun 14 '19 at 12:10
  • _"...But it doesn't work for me...."_: what doesn't work for you? How can we help if you don't describe the problem(s) you are having. Please read [ask] with a [mcve]. – Richard Critten Jun 14 '19 at 12:13
  • Maybe that was not the right wording. I tried to understand C++/CLI and PInvoke and to apply the solution. However, it doesn't work for me. For this reason I am asking for a step-by-step explanation with the given code there. – Alghedi Jun 14 '19 at 12:15
  • Don't use C++ types (that includes your C++ classes) for P/Invoke. Unless you enjoy insufferable pain, stick to plain old C-style DLL exports. If you can't do that for whatever reason, you are probably better off creating a C++/CLI library where you implement managed classes that work directly with your native/unmanaged C++ type stuff... –  Jun 14 '19 at 12:17
  • "_a step-by-step explanation_" StackOverflow is a question&answer site, and thus a rather poor place for finding or requesting tutorials/guides/mentoring, unfortunately... –  Jun 14 '19 at 12:18
  • Thanks @elgonzo . Is not possible to use DllImport and then to use the functions there? I have read about wrapping and something like that. I don't know what to do here because it seems that there is a lot of ways to solve it. What is the plain old C-Style DLL exports? I have got the .dll and that is all what I have. I have also the source files of these .dll. And If I want to make a new .dll would mean, that I have to learn to code in C++, what is very improbable. – Alghedi Jun 14 '19 at 12:21
  • These are not just some C-style functions. Those functions are members of the your C++ class Application. There is no pure C unspoiled by C++ there anywhere in the code in your question. Again, once more: The DLL exports you want to P/Invoke should be just plain old C with no C++ stuff being involved. At all. (That would not only include stuff you write in your source code, but also extends to C++ compiler behavior such as C++ name mangling, for example) –  Jun 14 '19 at 12:23
  • I see. The library comes from C++ code, you are right. I have corrected the question. I don't know much about C neither C++. I thought there is a easy way to bind the class/functions from the code above in my C# code. – Alghedi Jun 14 '19 at 12:30
  • See [this](https://stackoverflow.com/questions/16332701/how-to-call-c-dll-in-c-sharp) question which is a duplicate of what you need. –  Jun 14 '19 at 14:10
  • Thanks @Drifter ... my header file doesn't look like this. – Alghedi Jun 14 '19 at 14:45
  • I will post another questions since I have the .c files and the .h files. How would I procede from there? I mean, I have the real source code so I can do my own .dll, but still no idea how to use it with c# :D . have a nice weekend – Alghedi Jun 14 '19 at 14:46
  • Hi @Drifter . Slowly I am understanding more about wrappers, dll and how to bin the C code in C#. And your post is very helpful. Thanks one more time! :) – Alghedi Jun 17 '19 at 07:15

0 Answers0