I wrote a C# code for connecting to Microsoft SharePoint, but i need to call it from python it means I wanna ask python to run this code, is it possible? if yes, how can I do this?
Asked
Active
Viewed 1.1k times
-1
-
2This is not a duplicate as this is NOT the same question as calling an external script from Python. See answers for details. – Jonathan B. Jan 14 '20 at 21:57
2 Answers
3
The short answer would be
os.system("myapp.exe")

BugFinder
- 17,474
- 4
- 36
- 51
-
-
4if you provide an answer for this obvious dupe, at least do it properly. `os.system` is deprecated, `subprocess` module is the preferred way. And return code should be checked. – Jean-François Fabre May 22 '17 at 07:51
0
It is actually pretty easy. Just use NuGet to add the "UnmanagedExports" package to your .Net project. See https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports for details.
You can then export directly, without having to do a COM layer. Here is the sample C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
class Test
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int TestExport(int left, int right)
{
return left + right;
}
}
You can then load the dll and call the exposed methods in Python (works for 2.7)
import ctypes
a = ctypes.cdll.LoadLibrary(source)
a.add(3, 5)

Hyrein
- 391
- 2
- 12
-
4Why the answer is copied from [here](https://stackoverflow.com/questions/7367976/calling-a-c-sharp-library-from-python)? Instead you could have provided the url... – Karthick Raju Nov 14 '19 at 10:27