C#调用C语言dll

C#调用C语言dll

在拥有C/C++学习基础上学习C#

在winform平台上,有时需要与另外的C语言对接传输数据,在C语言已封装成dll情况下,在VS2019上如何调用dll实现调用C语言定义的函数。

使用DLLImport

// An highlighted block
[DllImport("AAA.dll", EntryPoint = "BBB",
            CallingConvention = CallingConvention.Cdecl)]
 public unsafe static extern CCC BBB();

其中AAA为C语言封装好的dll,BBB为入口点函数的名称,CCC为调用函数时使用的数据类型。

举例

// An highlighted block
[DllImport("Workstation.dll", EntryPoint = "getIP",
            CallingConvention = CallingConvention.Cdecl)]
        public unsafe static extern IntPtr getIP()

从封装好的Workstation.dll中调用函数实现模拟传感器数据输入及显示准备工作

// An highlighted block
[DllImport("Workstation.dll", EntryPoint = "getIP",
            CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern IntPtr getIP();
 [DllImport("Workstation.dll", EntryPoint = "initSensors",
            CallingConvention = CallingConvention.Cdecl)]
public static extern int initSensors();
[DllImport("Workstation.dll", EntryPoint = "getCodedDataPackage_ModeNormal",
            CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern IntPtr getCodedDataPackage_ModeNormal(int sensorID);    
[DllImport("Workstation.dll", EntryPoint = "decodeDataPackage_ModeNormal",
            CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int* decodeDataPackage_ModeNorma(string codeDataPackage);

你可能感兴趣的:(C#调用C语言dll)