-1

I have a C# .NET DLL (call it CS_Code.dll) which is using

[DllImport("C_Code.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

C_Code.dll contains mainly C code in extern "C" clauses.

When I will be using this assembly (CS_Code.dll) later in a project. I want to save from having to deliver two DLLs. All should be in one DLL.

Would it be possible to include (compile) the C-code into the .NET (e.g. as a library) making one .NET DLL out of it?

Krischu
  • 1,024
  • 2
  • 15
  • 35
  • Do you have the actual source code or just the compiled DLL? – D Stanley Nov 14 '17 at 16:16
  • You do realize this is how **every** program out there works, right? I challenge you to show me a [serious] program that comes with a single DLL, even if it's pure .NET code. – JuanR Nov 14 '17 at 16:17
  • I removed "clumsy". I just want to have one DLL exporting a C function. The wrapping C# DLL would be just for giving the user a Class interface freeing from using any [dllimport] clauses, etc. – Krischu Nov 14 '17 at 16:46
  • @D Stanley: yes, I'm owner/developer of the code. – Krischu Nov 14 '17 at 18:49
  • @Juan: I don't understand you. Sure I know that DLLimport is a common way. My idea is to include the C code into the .NET (managed) DLL, so I just have one DLL I can pass to my users. – Krischu Nov 14 '17 at 18:51
  • I don't see why my post is a duplicate "Embedding unmanaged dll into a managed C# dll". There the DLL will be loaded at runtime. It's a different thing. But I think I can use an exported C++ class in the .NET DLL. – Krischu Nov 14 '17 at 19:00
  • @Krischu: The idea behind DLLs is precisely that, sharing code. DLL stands for Dynamic Link Library. If you want to include your code without multiple DLLs, you might as well port it to C# (since you own the code) and compile it as part of the main one. But wait a moment, why would you go through all that trouble when you already have a **dynamic library** that you can just **link** to? – JuanR Nov 14 '17 at 19:30
  • @Juan Romer: I know the meaning of DLL. Porting it to C# would be too lengthy and tedious at the moment. I want to distribute 1 (one) .NET DLL, that the user/programmer can load into his project. Giving him/her 2 DLLs (one for the managed Class interface, the other to call the C-code from within that .NET DLL) is not what I want. Yes, I can create a C# or C++ .NET Classlibrary and I tried it out with doing a [dllimport] of that C-code DLL. Mixed-mode assembly like ->Lloyd is mentioning below is worth to have a closer look at. Thanks. – Krischu Nov 15 '17 at 11:06

1 Answers1

1

You can create a mixed-mode assembly which contains both native and managed code, however it's quite a topic so I suggest you read the section about it here on MSDN:

Mixed (Native and Managed) Assemblies

Lloyd
  • 29,197
  • 4
  • 84
  • 98