1

I was working with two external functions in Dymola via two DLLs. I encountered a problem which made me think about how the compiler actually finds the external function. So the functions within the two different DLLs have the same name and the same set of input and output variables but were doing different tasks.

void Execute(int in_1, bool flag_in, bool* flag_out, int* out_1);

I defined them as the following in the external function interface in Modelica:

function testFunc1 

  input Integer in_1;
  input Boolean flag_in;
  output Boolean flag_out;
  output Integer[5] out_1;

  external C Execute(int in_1, bool flag_in, bool* flag_out, int* out_1)   
  annotation(Library = "DLL1");
end testFunc1;

and the second function was called within another modelica function called testFunc2 just like above. What I observed was, since the external functions have the same names and the same set of input and output variables, in spite of the defined Library name, the compiler would mistakenly pick the wrong function from the other DLL and execute it.

I was wondering if there is a way to force the compiler to only look into a particular DLL when looking for an external function? or the external functions are not supposed to have identical names at all? Or is there a better way to introduce the DLL to Modelica?

Shaga
  • 170
  • 8

2 Answers2

3

If you really need the symbols to be named the same thing, use LoadLibrary and so on from the win32 API. Else, you should probably give the functions unique names for cross-tool and cross-platform compatibility.

sjoelund.se
  • 3,468
  • 12
  • 15
  • 1
    Thank you for your answer. I am not actually familiar with LoadLibrary from the win32 API. Could you maybe add a link so that I can find some information? Thanks! – Shaga Nov 19 '15 at 12:48
  • 2
    [MSDN: LoadLibraryEx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx) – xmojmr Nov 19 '15 at 14:20
  • I just used LoadLibrary to load the dll simply by using `HMODULE hModule = LoadLibrary(L"C:\mylib.dll")`. Although the library is found in VM, dymola is unable to find it and hModule is returned as null. What could be the reason? – Shaga Jan 12 '16 at 19:01
  • @sjoelund.se sorry for the multiple comments, I was not able to edit my previous comment. – Shaga Jan 12 '16 at 19:11
  • Try using C:/ or C:\\ instead of C:\. It's strings, so you need to escape the character. I get `warning: unknown escape sequence '\m'` and my C-compiler ignores the \. – sjoelund.se Jan 13 '16 at 05:39
  • @sjoelund.se Dymola still doesn't find the path when I use C:\ or C:\\. My problem is, I don't know how to specify a path that dymola can find it. I ran my code in VS2012 (the compiler that my Dymola instance is also using) and everything works fine, but I don't understand why the path is not found when I use the same compiler in Dymola. – Shaga Jan 14 '16 at 14:10
  • ok, it finally worked! :) I had to include the static library as well in the `Library` section of the external function call. – Shaga Jan 14 '16 at 14:45
1

Does the library have both a DLL and a LIB file or just a DLL?

If there is a LIB-file Dymola will link with that - and it could cause the problem above, but without a LIB-file Dymola 2016 should go directly to the DLL.

But using different names is normally simpler.

Hans Olsson
  • 11,123
  • 15
  • 38