1

My application is in C# and the 3. party I am working with is closed source C++. I have access to the third-party library as .dll's and .h files.

I access the code using Platform Invoke

When using the third-party library I start by calling a function InitBase(...) which creates and returns a Base struct. But it is only allowed to have ONE base at a time.

One base can only perform one task. I want to be able to perform multiple tasks at a time (multithreaded).

Is it possible call the 3. party library with platform invoke from different threads where the threads don't interfere with each other in the third-party library? I would prefer each thread have a complete separate memory chuck while in the third-party library, so from the third-party libraries point of view, it is still the only instance existing.

If it is not possible I have to run my application in multiple processes as the only way to make it "multithreaded".

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr. JWolf
  • 1,375
  • 2
  • 14
  • 33
  • Look here http://stackoverflow.com/questions/4225064/load-dll-multiple-times-to-allow-multi-threading-in-net and here http://stackoverflow.com/questions/12530022/load-the-same-dll-multiple-times. Might find something intresting in comments. – norekhov Aug 29 '14 at 09:02
  • By the way you can write a wrapper in C++ for example to load dll for yourself. It's not that hard and some code could be found also. Load it in memory, organize, resolve links e.t.c. All for yourself and don't use LoadLibrary. – norekhov Aug 29 '14 at 09:04

1 Answers1

0

As I read your question, the library constrains you to a single instance of this object per instances of the DLL. Different threads in the same process will in the normal way of things use the same instance of a DLL. So it would seem that threads are not helpful to you.

One possible solution would be to use multiple processes, and IPC, as you yourself suggest.

Another option would be to load separate instances of the DLL into the process. The loader will re-use DLLs if they have the same name. If you make multiple copies of the DLL, the loader will not be able to re-use already loaded instances. You should be able to use hardlinks to avoid needing to use extra disk space for each copy, although I've never done that myself. Whenever I've done this I've copied the DLL to the temp folder and not worried about the disk space.

This multiple DLL instance technique won't always work (things may be shared other than global variables), but it often does. You would be well advised to contact the developer of the DLL for assistance.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I would prefer not to use multiple processes, because IPC is always a hell to get right. How do you use "hardlink" and how do you do the workaround with coping the DLL's and then platform invoke to them? – Mr. JWolf Aug 29 '14 at 08:58
  • Unfortunately it's not always safe. Because DLL's can use some shared resources like mutexes and lockfiles and just copying and loading them multiple times can break it. So should be very careful with it. There can be an external reason why "InitBase" returns always the same object. – norekhov Aug 29 '14 at 09:01
  • @norekhov is right. Multiple instances of the module may not work. But in my experience it would be unusual for that to be the case. However, you can but try. Talking to the developer of the dll would be shrewd. As toi how to do it, pinvoke `LoadLibrary` and `GetProcAddress`. – David Heffernan Aug 29 '14 at 09:06