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
Developing with C# .NET: A Practical Guide and ExamplesDeveloping with C# .NET: A Practical Guide and ExamplesMay 12, 2025 am 12:16 AM

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

C# .NET: Understanding the Microsoft .NET FrameworkC# .NET: Understanding the Microsoft .NET FrameworkMay 11, 2025 am 12:17 AM

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

The Longevity of C# .NET: Reasons for its Enduring PopularityThe Longevity of C# .NET: Reasons for its Enduring PopularityMay 10, 2025 am 12:12 AM

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools