0

I am working on a C# application which is a port from a C++ application that utilizes a C++ DLL. The C++ DLL is from a third party which provides the .dll, a .lib, and a .h file with all of the extern functions contained within the DLL. No issues here, using interop I can call into the DLL from my C# app.

The issue comes in the fact that the third party has a couple functions in the .h for their DLL that they expect you to implement. It seems they provide function signatures in their .h file that they call in the DLL, so you must have an implementation of the function, even if it is just hollowed out.

For Example:

DLL header file will contain:

extern void Foo(void);      // Implemented in DLL
extern void YourFoo(void);  // Must be implemented in your app

So the class where I bring in these functions in my C# app will be something like:

[DllImport(MY_DLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void Foo();

// Need to show C++ DLL that I implemented YourFoo here
public static void YourFoo()
{
   // My logic
}

How do I provide my C# implementation of YourFoo to the C++ DLL?

EDIT #1: This question is not concerning using a callback between C#/C++. I need to provide a C# implementation for a function extern'd in the header file for a C++ DLL used by my C# application.

BackDoorNoBaby
  • 1,445
  • 2
  • 13
  • 20
  • See also e.g. https://stackoverflow.com/questions/7970128/passing-a-c-sharp-callback-function-through-interop-pinvoke and https://stackoverflow.com/questions/8247327/passing-c-sharp-function-pointers-into-c-cli-interop-dll, among others. – Peter Duniho Feb 28 '17 at 21:59
  • My issue isn't a callback function. I know how to use them with interop and pinvoke – BackDoorNoBaby Feb 28 '17 at 22:05
  • Unless I'm missing something.. There are function pointers defined in the header file for the DLL which are callbacks, I use delegates for these as unmanaged function pointers. What I'm dealing with in this question is simply providing the implementation for a function extern'd in the header file for the same C++ DLL – BackDoorNoBaby Feb 28 '17 at 22:07
  • The marked duplicate and other related questions concern not just delegate callbacks but also exposing your DLL members using COM interop. Your question is incredibly broad, lacking a good [mcve], so it's impossible to know 100% what exactly is needed in your case, but the answer will be in one of those three posts, possibly others. Unmanaged code can't call into a managed DLL directly, so you're going to have to implement _some_ kind of layer to act as the go-between, based on those answers. – Peter Duniho Feb 28 '17 at 22:08
  • Respectfully, I disagree that my question is too broad. I have a C# application which calls into a C++ DLL. That C++ DLL requires me to implement one of the functions it externs in its header file. My question regards how to provide that implementation. What other information should I provide? – BackDoorNoBaby Feb 28 '17 at 22:10
  • Please read [mcve]. Also read [ask]. And read _all_ of the articles linked at the bottom of [ask]. You haven't even explained what mechanism your third-party DLL even uses to find the `YourFoo()` function, never mind provided all of the necessary details for this to be a useful, answerable question. – Peter Duniho Feb 28 '17 at 22:11
  • With all due respect, I have been using SO for over 5 years and I am fully aware of the expected rules regarding posting questions. If you feel my question is too broad, I will fix it. But the content of the links you posted in the comments do not answer my question. They regard callback functions, which is not what my question is asking. – BackDoorNoBaby Feb 28 '17 at 22:16
  • Right, because my question is regarding that very same mechanism. I need to provide a way for the C++ DLL to find my implementation of `YourFoo`, that is the question I am asking – BackDoorNoBaby Feb 28 '17 at 22:18
  • Again, I'm confused by what other information I need to provide here. There is a C++ DLL, there is a C# application that uses said DLL. There is a function extern'd by the C++ DLL which needs to have its implementation in the C# application. – BackDoorNoBaby Feb 28 '17 at 22:20
  • _"my question is regarding that very same mechanism"_ -- what does that mean? Are you saying that your question is asking the Stack Overflow community to tell you how the third-party DLL you are using finds and calls the `YourFoo()` function in the client executable? How is anyone here supposed to know that? We don't even know what DLL you're using, never mind do we have the _code_ for that DLL? Frankly, it's doubtful you can get it to work directly...as I said, unmanaged code can't call into managed code directly. You need _some_ form of proxy to adapt the two pieces of code. – Peter Duniho Feb 28 '17 at 22:21
  • Perhaps you misunderstood me: I have a 3rd party DLL, i ALSO dont have the source code for it. I simply have the issue of being forced to implement a function that the DLL expects to use. – BackDoorNoBaby Feb 28 '17 at 22:23
  • That _you_ don't have the source code for the third-party DLL doesn't improve the chances of anyone in the Stack Overflow community being able to tell you how it works. I see two options: you use (in part) the techniques described in the marked duplicate and other related posts to write a layer to go between your third-party DLL and your executable (maybe require a secondary executable/process); or, you go talk to the author of the third-party DLL and ask them. A third option is, of course, to reimplement the third-party DLL as well, or find an existing replacement. – Peter Duniho Feb 28 '17 at 22:25
  • I am currently using my own C++ DLL to satisfy the requirements of the function required by the 3rd party DLL. I would like to get rid of my C++ DLL since my entire application is C# and if I can remove the extra project, great. But it sounds like that is not possible – BackDoorNoBaby Feb 28 '17 at 22:27
  • So again, just to reiterate, there is no way for a C# application to use a C++ DLL and define in the C# application the implementation for one of the functions extern'd in the header file for the C++ DLL? (without using a wrapper DLL, project, application, etc) – BackDoorNoBaby Feb 28 '17 at 22:33
  • _"there is no way for a C# application to use a C++ DLL and define in the C# application the implementation for one of the functions extern'd in the header file for the C++ DLL?"_ -- "C++ DLL" covers way too much ground for anyone to be able to answer that question. But, yes...assuming an average C++ DLL with no knowledge of managed code whatsoever, no you can't do that without adding some layer of code to act as the go-between. Managed code can't be called directly by unmanaged code. – Peter Duniho Feb 28 '17 at 22:35
  • Thank you for your assistance. I appreciate your time. – BackDoorNoBaby Feb 28 '17 at 22:37
  • See also http://stackoverflow.com/questions/3054460/possible-to-call-a-managed-dll-from-unmanaged-c, if you're looking for the direct "no, you can't do this" answer. – Peter Duniho Feb 28 '17 at 22:42
  • That link is concerning a DLL calling into another DLL, which is no my situation. This seems to be closer to what I'm looking for: http://stackoverflow.com/questions/225277/calling-managed-code-from-unmanaged-code – BackDoorNoBaby Feb 28 '17 at 22:44
  • I don't see any material difference between the two, but...whatever. It's all the same: unmanaged code can't call directly into managed code. Some type of adapting layer is always required. – Peter Duniho Feb 28 '17 at 22:49
  • Doesn't the second answer in that link prove otherwise? By using the `Unmanaged Exports` package, the managed code is exported and then called directly by the unmanaged code – BackDoorNoBaby Feb 28 '17 at 22:50
  • _"unmanaged code is exported and then called directly by the managed code"_ -- I'm assuming you wrote that backwards from what you mean. Anyway, you can try. The answer states you need to compile the unmanaged code with the .lib generated when you compile the managed code and you said you can't recompile the unmanaged code. But maybe it would work anyway. (And I will point out, this is not "layerless"...it's essentially the same as your current C++ shim solution, just a bit more automatic). – Peter Duniho Feb 28 '17 at 22:54
  • ahh yes, I absolutely wrote that backwards. Okay, I won't take up any more of your time. Thanks again, apologies if I came off as abrasive. – BackDoorNoBaby Feb 28 '17 at 22:56

0 Answers0