CPU Architecture Independent P/Invoke: Can the DllName or path be “dynamic”?

前端 未结 3 1415
有刺的猬
有刺的猬 2020-12-03 08:44

Is there a way to have the particular DLL referenced by a P/Invoke (DllImport) signature depend on the CPU architecture?

I\'m working on an application that loads a

3条回答
  •  死守一世寂寞
    2020-12-03 09:26

    I developed a special library for the target: InteropDotNet. It introduces new RuntimeDllImport attribute with dynamical library path resolving (on the fly). In the default way, you can write

    [RuntimeDllImport("NativeLib", 
       CallingConvention = CallingConvention.Cdecl, EntryPoint = "sum")]
    int Sum(int a, int b);
    

    And the library will be resolved depends on environment. For example, paths for Win/Linux, x86/x64:

    x86/NativeLib.dll
    x86/libNativeLib.so
    x64/NativeLib.dll
    x64/libNativeLib.so
    

提交回复
热议问题