search
HomeBackend DevelopmentC#.Net TutorialHow to call the function interface and callback function in C++ DLL in C#

1. Most of the basic data types in C# are the same as the basic data types of C++, so the conversion of basic data types is relatively simple. What needs to be paid attention to is the byte size. 2. What you need to pay attention to when converting C++ arrays and C# arrays is that you need to add a statement such as [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] to define an array with 10 array elements in C++. 3. The callback function interface of C++ needs to use the delegate function method in C#. In the callback function setting in C++, the address value is generally used, so the managed function needs to be displayed in C#. 4. For pointer arrays in C++, IntPtr in C# can be used; if it is just an address pointer, the reference type in C# can be used. Add ref

before the type for example: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;

namespace IPNBSVTD_CSharp { public delegate void StatusCallBack(IntPtr dwInstance, IntPtr wParam, IntPtr lParam) ; public delegate void VoiceCallBack(IntPtr dwInstance, byte ucDeviceIndex, IntPtr pInDataBuf, ref int pInDataLen, IntPtr pOutDataBuf, int nOutDataLen);

public enum VT_MESSAGE
{
    VT_MESSAGE_SET_DEVICE_INFO,
    VT_MESSAGE_UPDATE_DEVICE_INFO,
    VT_MESSAGE_UPDATE_MIXER_IN_CTRL,
    VT_MESSAGE_UPDATE_MIXER_OUT_CTRL,
    VT_MESSAGE_MISSED_CALLS,                // 未接来电};public enum VT_TASK_TYPE //终端状态{
    VT_TASK_TYPE_NULL,                //空闲(无任务)
    VT_TASK_TYPE_TO_MONITOR,        //发起监听
    VT_TASK_TYPE_BY_MONITOR,        //被监听
    VT_TASK_TYPE_TO_BROADCAST,        //发起采播
    VT_TASK_TYPE_BY_BROADCAST,        //接收采播
    VT_TASK_TYPE_TO_TALK,            //发起对讲
    VT_TASK_TYPE_BY_TALK,            //接收对讲
    VT_TASK_TYPE_MULTITALK,            //多方通话
    VT_TASK_TYPE_OFFLINETALK,       //离线对讲};public enum VT_TASK_STATE //终端任务状态        {
    VT_TASK_STATE_NULL,                // 无任务
    VT_TASK_STATE_STOP,                // 监听停止,对讲停止,广播停止
    VT_TASK_STATE_CONNECTING,        // 监听:连接中
    VT_TASK_STATE_CALLING,            // 对讲:呼叫中
    VT_TASK_STATE_BEGIN_BC_PROMPT,    // 广播:开始提示音
    VT_TASK_STATE_END_BC_PROMPT,    // 广播:结束提示音
    VT_TASK_STATE_ONGOING,            // 监听中,对讲中,广播中};public enum VT_TARGET_STATE // 目标状态{
    VT_TARGET_STATE_NULL = 0,            // 未知
    VT_TARGET_STATE_OFFLINE,            // 目标脱机
    VT_TARGET_STATE_STOP,                // 目标停止
    VT_TARGET_STATE_REJECT,                // 目标拒绝
    VT_TARGET_STATE_BUSY,                // 目标呼叫忙};

[StructLayout(LayoutKind.Sequential)]public struct VT_TASK_TARGET
{    public int nCount;                                // 监听,对讲,广播选择的目标数量
    public int nEditPos;                            // 当前正在编辑的位置
    public VT_TASK_TYPE xeTaskType;                            // 任务类型
    public VT_TASK_STATE xeTaskState;                        // 任务组的任务状态
    public VT_TARGET_STATE xeTargetState;                        // 对讲目标状态
    public bool bIsBegin;                            // 任务是否已开始(呼叫...)
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]    public VT_TASK_STATE[] pxeTerState;    // 各终端的任务状态  VT_DEVICE_MAX_COUNT 6
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]    public ushort[] pwID;        // 终端ID(0:1号终端...)或分区号(1:分区1...) VT_BROADCAST_MAX_COUNT            128
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]    public int[] piSerIdx;    // 各终端对应的服务器编号
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]    public byte[] pucPanelNo;    // NAS-8521,呼叫到面板,终端对应的面板序号
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]    public char[] strFileName; // 文件广播时,当前广播的文件名
    public uint offlineTargetIP; //离线目标IP
    public ushort offlineTargetPort;  //离线目标端口};

[StructLayout(LayoutKind.Sequential)]public struct tagSERVER
{    public uint ipServer;                // 服务器IP (字节反序)
    public ushort wServerPort;            // 服务器接收端口 (字节反序)
    public bool bFixPort;                // 是否固定接收端口
    public ushort wHostPort;            // 本机接收端口 (字节反序)
    public bool bOnline;                // 是否联机
    public bool bIsPCM;                    // 是否是PCM/ADPCM压缩
    public bool bIs8K;                    // 是否是8K/22K采样
    public bool bIsMultiCast;            // 是否是组播/单播
    public bool bRecord;                // 是否录音
    public uint ipRecord;                // 录音IP
    public ushort wRecordPort;            // 录音端口};

[StructLayout(LayoutKind.Sequential)]public struct VT_DEVICE_INFO
{    public ushort wDeviceId;            // 设备编号(从0开始,显示时要加1)
    public uint ipHost;                    // 主机IP (字节反序)
    public bool bIsRegister;            // 是否注册
    public ushort dwVol;                // 当前输出音量
    public int iServerCount;            // 服务器的数量
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]    public tagSERVER[] pxsServer;       // 服务器
    public int nTaskCount;                // 任务数量
    public int nCurTask;                // 当前任务(对应于xsTaskTarget的下标)
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]    public VT_TASK_TARGET[] pxsTaskArray;        // 任务目标};/// <summary>/// 跨平台调用方法/// </summary>public class NativeMethod{    /// <summary>
    /// 路径最大大小
    /// </summary>
    public const int MAX_PATH = 260;

    [DllImport("IPNBSVTD.dll")]    public static extern void IPNBSVTD_DeleteDevice();

    [DllImport("IPNBSVTD.dll")]    public static extern byte IPNBSVTD_CreateDevice(byte ucCount);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_DeviceMute(ref bool pbMute, bool bIsAudioOut, bool bIsSet);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_DeviceVolume(ref int pdwVolume, bool bIsAudioOut, bool bIsSet);

    [DllImport("IPNBSVTD.dll")]    public static extern void IPNBSVTD_SetINIFilePath(byte ucDeviceIndex, string pINIFilePath);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_OpenServer(byte ucDeviceIndex, IntPtr hParentWnd, uint nMsg);    //[DllImport("IPNBSVTD.dll")]
    //public static extern bool IPNBSVTD_SetBCFromFile(byte ucDeviceIndex, bool bBCFromFile, bool bLoop,
    //    [MarshalAs(UnmanagedType.LPArray)]string[] pStrFile, short sFileCount);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetBCFromFileEx(byte ucDeviceIndex, bool bBCFromFile, bool bLoop, string strFile, bool bAdd);    public static void SetBCFromFile(byte ucDeviceIndex, bool bBCFromFile, bool bLoop, List<string> files, short sFileCount)    {        for (int i = 0; i < files.Count; i++)
            IPNBSVTD_SetBCFromFileEx(ucDeviceIndex, bBCFromFile, bLoop, files[i], (i == 0) ? false : true);
    }

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetBCToTerm(byte ucDeviceIndex);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_GetCurTaskTarget(byte ucDeviceIndex, ref VT_TASK_TARGET pTaskTarget, int iTaskPos);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetCurTaskTarget(byte ucDeviceIndex, ref VT_TASK_TARGET pTaskTarget, int iTaskPos);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_TaskBegin(byte ucDeviceIndex, int iTaskPos);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_TaskEnd(byte ucDeviceIndex, int iTaskPos);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_GetDeviceInfo(byte ucDeviceIndex, ref VT_DEVICE_INFO pDeviceInfo);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_GetBCFileCurrent(byte ucDeviceIndex, StringBuilder strFile);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetStatusCallBack(byte ucDeviceIndex, StatusCallBack dwCallBack, IntPtr dwInstance);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetVoiceCallBack(byte ucDeviceIndex, VoiceCallBack dwCallBack, IntPtr dwInstance);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetFileBCNext(byte ucDeviceIndex);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetFileBCPause(byte ucDeviceIndex, bool bPause);        

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetFileBCPrevious(byte ucDeviceIndex);

    [DllImport("IPNBSVTD.dll")]    public static extern uint IPNBSVTD_GetBCFileCurrentPlayPos(byte ucDeviceIndex, ref uint pBcFileLen);

    [DllImport("IPNBSVTD.dll")]    public static extern bool IPNBSVTD_SetFileBCPlayListPos(byte ucDeviceIndex, byte ucPlayListPos);
}

}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing C# .NET Applications: Unit, Integration, and End-to-End TestingTesting C# .NET Applications: Unit, Integration, and End-to-End TestingApr 09, 2025 am 12:04 AM

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Advanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewAdvanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewApr 08, 2025 am 12:06 AM

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# .NET Interview Questions & Answers: Level Up Your ExpertiseC# .NET Interview Questions & Answers: Level Up Your ExpertiseApr 07, 2025 am 12:01 AM

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

Building Microservices with C# .NET: A Practical Guide for ArchitectsBuilding Microservices with C# .NET: A Practical Guide for ArchitectsApr 06, 2025 am 12:08 AM

C#.NET is a popular choice for building microservices because of its strong ecosystem and rich support. 1) Create RESTfulAPI using ASP.NETCore to process order creation and query. 2) Use gRPC to achieve efficient communication between microservices, define and implement order services. 3) Simplify deployment and management through Docker containerized microservices.

C# .NET Security Best Practices: Preventing Common VulnerabilitiesC# .NET Security Best Practices: Preventing Common VulnerabilitiesApr 05, 2025 am 12:01 AM

Security best practices for C# and .NET include input verification, output encoding, exception handling, as well as authentication and authorization. 1) Use regular expressions or built-in methods to verify input to prevent malicious data from entering the system. 2) Output encoding to prevent XSS attacks, use the HttpUtility.HtmlEncode method. 3) Exception handling avoids information leakage, records errors but does not return detailed information to the user. 4) Use ASP.NETIdentity and Claims-based authorization to protect applications from unauthorized access.

In c language: What does it meanIn c language: What does it meanApr 03, 2025 pm 07:24 PM

The meaning of colon (':') in C language: conditional statement: separating conditional expressions and statement block loop statement: separating initialization, conditional and incremental expression macro definition: separating macro name and macro value single line comment: representing the content from colon to end of line as comment array dimension: specify the dimension of the array

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.