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# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

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.

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools