Re: Special use case for ctypes

Zak Smolen <[email protected]> Fri, 5 May 2017 09:52:28 -0400
Newsgroups gmane.comp.python.ctypes
Message-ID <CAODUn57P-y6tahVwWDT40nrZSH_kHJYQOK1H3dQ7T45e_CfWgA@mail.gmail.com>
--===============5138908095919875706==
Content-Type: multipart/alternative; boundary=001a113d164e4521d8054ec7335d

--001a113d164e4521d8054ec7335d
Content-Type: text/plain; charset=UTF-8

So my thought behind this is that it must be possible... somehow, because
of debuggers. You can attach a debugger to an existing process and see it's
memory and such. I don't need simultaneous access from both the C# wrapper
and the python at the same time. I can have the C# not touch the C while
the python is running. It looks like debuggers do things with signals and
such.

I believe the issue with python for .NET, is that, while the C# would call
my python a little more directly, the python still gets its own instance of
the DLL. Even if I used IronPython, with a totally integrated control, that
is still the same issue.

Thanks for your response! I'm starting to think it's impossible, but not
quite ready to accept that...

On Fri, May 5, 2017 at 12:45 AM, eryk sun <[email protected]> wrote:

> On Thu, May 4, 2017 at 9:37 PM, Zak Smolen <[email protected]> wrote:
> >
> > My application has a C later, accessed by C# layers. I also have a python
> > wrapper for that C layer written using ctypes. I would like to have the
> C#
> > call python scripts which would then, using my library, do things to the
> > same instance of the C DLL as the C# is using.
>
> A DLL can use a shared memory section (i.e. a #pragma data_seg with
> the shared attribute) for data that's shared across all processes, but
> otherwise its global data is private to each process. Use VMMap [1] to
> look at how pages in the .data section are mapped in your DLL.
>
> [1]: https://technet.microsoft.com/en-us/sysinternals/vmmap.aspx
>
> > Also, the CDLL class is able to take a handle pointer to use instead of
> > calling LoadLibrary.
>
> An HMODULE 'handle' is a pointer to the base address where the DLL is
> mapped in the current process. It won't necessarily load at the same
> address in every process. Even if it's mapped at the same address, its
> global .data will be mapped to private pages.
>
> > Anyone have any ideas that may help with this task?
>
> You could look into embedding Python into you .NET process. I thought
> Python for .NET did that, but I've never used it and rarely touch any
> of the .NET universe.
>
> Alternatively, if you're the author of the DLL you could put the
> global variables that need to be shared in a .shared section. But
> there are restrictions on what can be shared. For example, each
> process has its own address space, so sharing pointers should be
> avoided. Also, most handles can't be shared (e.g. kernel handles such
> as File handles are privately defined in the handle table of each
> process).
>

--001a113d164e4521d8054ec7335d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">So my thought behind this is that it must be possible... s=
omehow, because of debuggers. You can attach a debugger to an existing proc=
ess and see it&#39;s memory and such. I don&#39;t need simultaneous access =
from both the C# wrapper and the python at the same time. I can have the C#=
 not touch the C while the python is running. It looks like debuggers do th=
ings with signals and such.<div><br></div><div>I believe the issue with pyt=
hon for .NET, is that, while the C# would call my python a little more dire=
ctly, the python still gets its own instance of the DLL. Even if I used Iro=
nPython, with a totally integrated control, that is still the same issue.</=
div><div><br></div><div>Thanks for your response! I&#39;m starting to think=
 it&#39;s impossible, but not quite ready to accept that...</div></div><div=
 class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Fri, May 5, 2017 a=
t 12:45 AM, eryk sun <span dir=3D"ltr">&lt;<a href=3D"mailto:eryksun@gmail.=
com" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><blockquo=
te class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc so=
lid;padding-left:1ex"><span class=3D"">On Thu, May 4, 2017 at 9:37 PM, Zak =
Smolen &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&g=
t; wrote:<br>
&gt;<br>
&gt; My application has a C later, accessed by C# layers. I also have a pyt=
hon<br>
&gt; wrapper for that C layer written using ctypes. I would like to have th=
e C#<br>
&gt; call python scripts which would then, using my library, do things to t=
he<br>
&gt; same instance of the C DLL as the C# is using.<br>
<br>
</span>A DLL can use a shared memory section (i.e. a #pragma data_seg with<=
br>
the shared attribute) for data that&#39;s shared across all processes, but<=
br>
otherwise its global data is private to each process. Use VMMap [1] to<br>
look at how pages in the .data section are mapped in your DLL.<br>
<br>
[1]: <a href=3D"https://technet.microsoft.com/en-us/sysinternals/vmmap.aspx=
" rel=3D"noreferrer" target=3D"_blank">https://technet.microsoft.com/<wbr>e=
n-us/sysinternals/vmmap.aspx</a><br>
<span class=3D""><br>
&gt; Also, the CDLL class is able to take a handle pointer to use instead o=
f<br>
&gt; calling LoadLibrary.<br>
<br>
</span>An HMODULE &#39;handle&#39; is a pointer to the base address where t=
he DLL is<br>
mapped in the current process. It won&#39;t necessarily load at the same<br=
>
address in every process. Even if it&#39;s mapped at the same address, its<=
br>
global .data will be mapped to private pages.<br>
<span class=3D""><br>
&gt; Anyone have any ideas that may help with this task?<br>
<br>
</span>You could look into embedding Python into you .NET process. I though=
t<br>
Python for .NET did that, but I&#39;ve never used it and rarely touch any<b=
r>
of the .NET universe.<br>
<br>
Alternatively, if you&#39;re the author of the DLL you could put the<br>
global variables that need to be shared in a .shared section. But<br>
there are restrictions on what can be shared. For example, each<br>
process has its own address space, so sharing pointers should be<br>
avoided. Also, most handles can&#39;t be shared (e.g. kernel handles such<b=
r>
as File handles are privately defined in the handle table of each<br>
process).<br>
</blockquote></div><br></div>

--001a113d164e4521d8054ec7335d--


--===============5138908095919875706==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--===============5138908095919875706==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users

--===============5138908095919875706==--