Home > front end >  Using a c dll in c#
Using a c dll in c#

Time:12-14

If I have some c# code like this

   public const string CppFunctionsDLL = @"..\..\..\Release\CplusplusCode.dll";

    [DllImport(CppFunctionsDLL, CallingConvention = CallingConvention.Cdecl)]
    static extern int GetData(out IntPtr data, byte[] dataFromCard);

After building the solution..then running the executable on a different machine.. when GetData gets called does the actual c dll have to be present on the machine the executable is running on? or was everything loaded in when the build of the solution was run..?

Essentially.. how do I ensure that when the executable is run on a users machine.. that the c dll is present and that I can reference it in code?

CodePudding user response:

when GetData gets called does the actual c dll have to be present on the machine the executable is running on?

Yes. DllImport works internally by using LoadLibrary() and GetProcAddress() at runtime. When the C# function is called, the referenced DLL is loaded, and then the referenced DLL function is called, marshaling parameter data back and forth as needed.

or was everything loaded in when the build of the solution was run..?

No.

Essentially.. how do I ensure that when the executable is run on a users machine.. that the c dll is present and that I can reference it in code?

You must distribute and install the DLL alongside the built EXE. How you do that exactly is up to you. There are many ways to bundle up and distribute multi-file applications. For example, via a Windows Installer MSI package.

  • Related