How C#/Python Call DLL

1. C# Call DLL
First, we may have a simple C function
​​ myFunction.c
#include "myFunction.h"
int mySum(int a, int b){
    return a+b;
}

myFunction.h
#ifndef _MYFUNCTIONS_H
#define _MYFUNCTIONS_H

#ifdef __cplusplus
    extern "C" {
#endif
__declspec(dllexport) int mySum(int a, int b);

#ifdef __cplusplus
    }
#endif
#endif

Compiled to a myFunction.dll
[DllImport("myFunction.dll", EntryPoint="mySum", CallingConvention=CallingConvention.Cdecl)]
public static extern int mySum(int a, int b);

private void button_Click(object sender, EventArgs e)
{
    int a = mySum(1,2);
    textBoxShow.Text = a.ToString();
}

2. In Python.
from ctypes import *
myDll = cdll.LoadLibrary('myFunction.dll')
myDll.mySum(1,2)



你可能感兴趣的:(Python,C#新手入门)