-1

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?

Anıl Aşık
  • 360
  • 1
  • 15
Arashsyh
  • 609
  • 1
  • 10
  • 16
  • 2
    This 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 Answers2

3

The short answer would be

os.system("myapp.exe")
BugFinder
  • 17,474
  • 4
  • 36
  • 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
  • 4
    Why 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