1

I have a C# GUI, and a dll I compiled in matlab for C++. I would like to call from my C# code to the dll functions. I tried first to convert the C# code to CLI, but then saw it is not recommended. Can I create one solution with 2 projects - one the C# GUI and one - wrap the c++ dll with C++ code and call it from the C# ?

Any reference will be appreciated.

Thanks

matlabit
  • 838
  • 2
  • 13
  • 31
  • possible duplicate of this: http://stackoverflow.com/questions/772041/using-c-library-in-c-sharp – Davecz Dec 29 '13 at 08:53

1 Answers1

2

You can use C++ dll's in C# by using [DllImport] attribute, it is described in this MSDN thread.

Ilya Tereschuk
  • 1,204
  • 1
  • 9
  • 21
  • Thanks, I used this, but I'm having a problem, In the dll I have a function fl(int x, arr y), where arr type is defined in a c++ header file, which is included in the dll, but it is not recognized in the C# code – matlabit Dec 29 '13 at 09:21
  • @matlabit You can assign it to [dynamic](http://msdn.microsoft.com/en-us/library/dd264741.aspx) variable in C# and use it in the same way as in C++. `dymanic` keyword was introduced right to simplify inter-operational routine – Ilya Tereschuk Dec 29 '13 at 09:46
  • Not so sure I get it, my dll function gets a type that C# cant recognize, I can compile it using dynamic, but how do I call the function ? for example: [DllImport("myDll.dll")] extern static void myfunc(dynamic a, dynamic b); now say I want to use it, how can I assign a and b ? – matlabit Dec 29 '13 at 10:13
  • @matlabit `dynamic` are the variables behavior of which are **not** being checked explicitly. For example, if `a` and `b` are C++ integer arrays, `Int32 aValue = (Int32)a[0]` must work. – Ilya Tereschuk Dec 29 '13 at 10:16