美文网首页
C# 调用C++ DLL示例

C# 调用C++ DLL示例

作者: XBruce | 来源:发表于2021-01-07 15:37 被阅读0次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace DemoCSharp
{
    public static class SdkHelper
    {
        //DLL所在目录
        const string dllPath = "sdk.dll";

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct NwkDeviceParam
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string mStrSN;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string mIP;

            //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            //public string[] Array;
        };

        public enum WorkMode_E
        {
            WModUnknown,
            WMod1,
            WMod2,
        };

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct Image
        {
            //Image data buffer.
            //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
            //public string image;
            public IntPtr image;

            //Image size in bytes.
            public int imageSize;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct Rect
        {
            public int ltPosX;//left top positionX
            public int ltPosY;//left top positionY
            public int w;//width
            public int h;//height
        }

        #region callback
        //search
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate void CallBackFoundDevice(NwkDeviceParam value);
        
        //take picture
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate void CallBackReceiveImage(Image image);
        [DllImport(dllPath, EntryPoint = "_registerCbReceiveImage@4", CallingConvention = CallingConvention.StdCall)]
        public static extern void RegisterCbReceiveImage([MarshalAs(UnmanagedType.FunctionPtr)] CallBackReceiveImage callbackPointer);
      
        #endregion

        #region function
        [DllImport(dllPath, EntryPoint = "_Initialize@0", CallingConvention = CallingConvention.StdCall)]
        public static extern void SdkInitialize();

        [DllImport(dllPath, EntryPoint = "_doConnectAsync@8", CallingConvention = CallingConvention.StdCall)]
        public static extern void ConnectDevices(string devSN, string ip);

        [DllImport(dllPath, EntryPoint = "_setROI@8", CallingConvention = CallingConvention.StdCall)]
        public static extern void SetROI(Rect[] rects, int rectCnt);  //rectCnt    Must be 4

        [DllImport("kernel32.dll")]
        public static extern bool WriteFile(IntPtr hFile, IntPtr lpBuffer, int NumberOfBytesToWrite, out int lpNumberOfBytesWritten, IntPtr lpOverlapped);
        
        #endregion
    }
}

C#中使用

            //take image
            SdkHelper.CallBackReceiveImage cbReceiveImg = (SdkHelper.Image img) => 
            {
                m_image.image = img.image;
                m_image.imageSize = img.imageSize;
                m_bTakeImage = true;
                FileStream file = new FileStream("c:\\"+ (mImageIndex++) +"_"+ exposure + ".jpg", FileMode.Create, FileAccess.Write);
                int writen;
                WriteFile(file.Handle, img.image, img.imageSize, out writen, IntPtr.Zero);
                file.Dispose();
            };
            SdkHelper.RegisterCbReceiveImage(cbReceiveImg);

相关文章

网友评论

      本文标题:C# 调用C++ DLL示例

      本文链接:https://www.haomeiwen.com/subject/syyzoktx.html