1

SWIG lets you specify the DLL to import in C# by using the -dllimport command line argument.

What about importing a DLL whose name depends on whether it is a Debug version or a Release one? This happens with DLLs that follow the Microsoft convention of appending the d suffix to the Debug version, e.g. ucrtbase.dll for the Release version, and ucrtbased.dll for Debug.

If -dllimport allowed to specify a symbolic constant, then the value of such constant could depend on whether DEBUG is defined or not, but that does not seem to be the case.

Eleno
  • 2,864
  • 3
  • 33
  • 39
  • I'm not sure using a preprocessor directive would help here - when SWIG runs it doesn't know if you're compiling the DLL itself with DEBUG or not. – Flexo Sep 28 '19 at 18:30

1 Answers1

0

I think you can do what you want using a static constructor and the SetDllDirectory of suggestion of this this answer.

To test this out I put together this example:

%module foobar

%pragma(csharp) imclasscode=%{
  [global::System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  static extern bool SetDllDirectory(string lpPathName);

  static $imclassname() {
    if (isDebug)
      SetDLLDirectory("path/to/directory/with/debugDll");
    else
      SetDLLDirectory("path/to/directory/with/regularDll");
  }
%}

You have to build it with SWIG like this in order to suppress the default static constructor:

swig3.0 -DSWIG_CSHARP_NO_IMCLASS_STATIC_CONSTRUCTOR -csharp test.i

Quite how you'll distinguish isDebug in real code I'm less clear on - maybe use this to find the current HMODULE and then GetModuleFilename to see if it's your debug build or not.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Thanks, but this will not work because the DLLs have different names. The problem is the DLL name that SWIG uses in `DllImport`, which is a literal string. – Eleno Sep 29 '19 at 15:48
  • @Elena That's my point exactly, I think you can make it work despite that fixed literal string: by using two different directories, rename, them so they each have the same name and then picking which directory to use at load-time. – Flexo Sep 29 '19 at 18:33
  • You can call swig with `-dllimport '"+somefunction()+"`, but that's not actually legal to do in C# annotations at all as far as I can see. – Flexo Sep 29 '19 at 18:35