2

Is it possible to use a specific instance of a DLL using ctypes? Basically my application is as follows: We have a C DLL accessed by a C# DLL and then a C# WPF front end. I wrote a separate wrapper using ctypes to access the C DLL directly and bypass the C# all together.

We currently use an internal IronPython window in our program to allow users to script things in Python, and that connects to the middle C# layer and through that to the C dll. Because of the limitations of IronPython, I would like to see if it's possible to allow users to write Python code that can, using my library, directly access the C dll, but the same instance of it as the C# is using so they can be synced. Is this even remotely possible?

zaknotzach
  • 977
  • 1
  • 9
  • 18

1 Answers1

0

You have a program which loads a C# DLL which loads a C DLL. The same program also runs IronPython and you want Python to load the same C DLL.

I assume you are using ctypes.windll.LoadLibrary(path) in Python. So you just need to find the right path. For C# some ideas are here: How to get the location of the DLL currently executing?

Specifically, you can try this: https://msdn.microsoft.com/en-us/library/system.reflection.assembly.codebase(v=vs.110).aspx

You may be able to do it from IronPython directly, but if not, you can do it in your C# DLL and expose it.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So the issue is that I want to load the same instance of the DLL. So for example if you have a C DLL that has an internal variable called foo that stores an int, if I set foo from the C# and then get foo from the python I want that to be the same foo. – zaknotzach Aug 05 '16 at 19:29
  • Have you tried it? Does Windows actually give you a *de novo* instance of the same DLL? I would have thought loading the very same DLL twice in one process would only result in one copy of it. – John Zwinck Aug 05 '16 at 20:47
  • Yes, for example if I load my library twice with ctypes, run a function to change a value in one of them, and then get that value in both the values will be different since it was only changed in one instance. – zaknotzach Aug 10 '16 at 18:22