Can I embed Windows Python in C# or VC++?

sturlamolden sturlamolden at yahoo.no
Fri Dec 7 05:27:51 EST 2007


On 7 Des, 08:07, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:

> > Does it mean, I can embed Python in C# as well with the same APIs?
>
> No; you can use the Python API in a native C++ application (the Python
> code is plain C, but all the include files have the 'extern "C" {}'
> declarations). For .NET there are IronPython and PythonNet, but I cannot
> comment on them, surely someone else may help. See  http://www.python.org/about/

The answer is YES. C# can access C functions exported by any DLL with
platform invoke. Since the Python C API is plain C and not C++ you can
gain access to it from C#. Import System.Runtime.InteropServices and
write wrappers like

[DllImport("Python25.dll"), CallingConvention=CallingConvention.Cdecl]
public static void Py_Initialize();

etc.


You can also combine C++ and C# in a project in MS Visual Studio, if
you prefer to access Python from C++. That way you don't have to write
platform invoke wrappers for the Python C API.







More information about the Python-list mailing list