C++ 封装 DLL 供 C# 调用详细介绍

目录
  • 1、VLC代码封装
    • 1.1 QT(C++)工程
    • 1.2static 声明 m_instance 优化效率
    • 1.3封装 DLL
    • 1.4应用程序的导出函数
    • 1.5 vlc 简单封装的具体实现
  • 2、C# 调用
    • 2.1C# 回调函数声明与定义
    • 2.2C# 导出普通函数调用

1、VLC代码封装

1.1 QT(C++)工程

首先需要配置可使用 VLC 正常播放的 QT(C++)工程,获取VLC每一帧并渲染到Qwidget

Libvlcapi

public static class LIBVLCAPI
    {

        #region[libvlc.dll 导出函数]

        // 创建一个libvlc实例,它是引用计数的
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_new(int argc, IntPtr argv);

        // 释放libvlc实例
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_release(IntPtr libvlc_instance);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern String libvlc_get_version();

        // 从视频来源(例如Url)构建一个libvlc_meida
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);

        // 从视频来源(例如Url)抓图
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern int libvlc_video_take_snapshot(IntPtr libvlc_mediaplayer, int num, IntPtr filepath, int i_width, int i_height);

        // 从本地文件路径构建一个libvlc_media
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_release(IntPtr libvlc_media_inst);

        // 创建libvlc_media_player(播放核心)
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);

        // 将视频(libvlc_media)绑定到播放器上
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);

        // 设置图像输出的窗口
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);

        // 解析视频资源的媒体信息(如时长等)
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_parse(IntPtr libvlc_media);

        // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);

        // 当前播放的时间
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);

        // 设置播放位置(拖动)
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);

        // 获取和设置音量
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);

        // 设置全屏
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);

        // 设置屏幕因子
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_video_set_scale(IntPtr libvlc_media_player, float f_factor);

        #endregion

        #region[VLC方法]

        public struct PointerToArrayOfPointerHelper
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
            public IntPtr[] pointers;
        }

        public static IntPtr libvlc_new(string[] arguments)
        {
            PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
            argv.pointers = new IntPtr[11];

            for (int i = 0; i < arguments.Length; i++)
            {
                argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
            }

            IntPtr argvPtr = IntPtr.Zero;
            try
            {
                int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
                argvPtr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(argv, argvPtr, false);

                return libvlc_new(arguments.Length, argvPtr);
            }
            finally
            {
                for (int i = 0; i < arguments.Length + 1; i++)
                {
                    if (argv.pointers[i] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(argv.pointers[i]);
                    }
                }
                if (argvPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(argvPtr);
                }
            }
        }

        public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);
            }
            finally
            {
                if (pMrl != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pMrl);
                }
            }
        }

        public static int libvlc_video_take_snapshot(IntPtr libvlc_mediaplayer, int num, string path, int width, int height)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_video_take_snapshot(libvlc_mediaplayer, num, pMrl, width, height);
            }
            finally
            {
                if (pMrl != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pMrl);
                }
            }
        }

        public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);
            }
            finally
            {
                if (pMrl != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pMrl);
                }
            }
        }

        #endregion

    }

VLCPlayer

 public class VLCPlayer
    {
        private IntPtr libvlc_instance_;
        private IntPtr libvlc_media_player_;

        private double duration_;

        public VLCPlayer(string pluginPath, bool is_record)
        {
            if (is_record == true)
            {
                string plugin_arg = "--plugin-path=" + pluginPath;
                string filename = "c:\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".mp4";
                string record_paramter = "--sout=#duplicate{dst=display,dst=std{accs=file,mux=ts,dst=" + filename;
                string[] arguments = { "-I", "--fullscreen", "dummy", "--ignore-config", "--no-video-title", "--width=100", "--height=100", plugin_arg, record_paramter };// "--sout=#duplicate{dst=display,dst=std{accs=file,mux=ts,dst=c:\\1.mp4" };
                libvlc_instance_ = LIBVLCAPI.libvlc_new(arguments);
                libvlc_media_player_ = LIBVLCAPI.libvlc_media_player_new(libvlc_instance_);
            }
            else
            {
                string plugin_arg = "--plugin-path=" + pluginPath;
                string[] arguments = { "-I", "--fullscreen", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
                libvlc_instance_ = LIBVLCAPI.libvlc_new(arguments);
                libvlc_media_player_ = LIBVLCAPI.libvlc_media_player_new(libvlc_instance_);
                //float f1=0.1f;
                //LIBVLCAPI.libvlc_video_set_scale(libvlc_media_player_,f1);
            }
        }

        public void Vlc_release()
        {
            if (libvlc_instance_ != IntPtr.Zero)
                LIBVLCAPI.libvlc_release(libvlc_instance_);

            // if (libvlc_media_player_ != IntPtr.Zero)
            // LIBVLCAPI.libvlc_media_release(libvlc_media_player_);

        }
        public void SetRenderWindow(int wndHandle)
        {
            if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0)
            {
                LIBVLCAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);
            }
        }

        public void PlayFile(string filePath)
        {
            IntPtr libvlc_media = LIBVLCAPI.libvlc_media_new_path(libvlc_instance_, filePath);
            if (libvlc_media != IntPtr.Zero)
            {
                LIBVLCAPI.libvlc_media_parse(libvlc_media);
                duration_ = LIBVLCAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;

                LIBVLCAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
                LIBVLCAPI.libvlc_media_release(libvlc_media);

                LIBVLCAPI.libvlc_media_player_play(libvlc_media_player_);
            }
        }

        public void PlayFile_rtsp(string filePath)//libvlc_media_new_location
        {
            IntPtr libvlc_media = LIBVLCAPI.libvlc_media_new_location(libvlc_instance_, filePath);
            if (libvlc_media != IntPtr.Zero)
            {
                // LIBVLCAPI.libvlc_media_parse(libvlc_media);
                // duration_ = LIBVLCAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;

                LIBVLCAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
                LIBVLCAPI.libvlc_media_release(libvlc_media);

                LIBVLCAPI.libvlc_media_player_play(libvlc_media_player_);
            }
        }

        public void Pause()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LIBVLCAPI.libvlc_media_player_pause(libvlc_media_player_);
            }
        }

        public void take_snapshot()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                string filepath = "c:\\";
                LIBVLCAPI.libvlc_video_take_snapshot(libvlc_media_player_, 0, filepath, 0, 0);
            }
        }

        public void full_screen()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {

                LIBVLCAPI.libvlc_set_fullscreen(libvlc_media_player_, 0);
            }
        }

        public void Stop()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LIBVLCAPI.libvlc_media_player_stop(libvlc_media_player_);
            }
        }

        public double GetPlayTime()
        {
            return LIBVLCAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
        }

        public void SetPlayTime(double seekTime)
        {
            LIBVLCAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000));
        }

        public int GetVolume()
        {
            return LIBVLCAPI.libvlc_audio_get_volume(libvlc_media_player_);
        }

        public void SetVolume(int volume)
        {
            LIBVLCAPI.libvlc_audio_set_volume(libvlc_media_player_, volume);
        }

        public void SetFullScreen(bool istrue)
        {
            LIBVLCAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0);
        }

        public double Duration()
        {
            return duration_;
        }

        public string Version()
        {
            return LIBVLCAPI.libvlc_get_version();
        }
    }

1.2static 声明 m_instance 优化效率

如下:

#pragma once
#include <memory>
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#include "vlc/vlc.h"
#include <mutex>

struct libvlc_media_track_info_t;
struct libvlc_media_t;
struct libvlc_instance_t;
struct libvlc_media_player_t;
struct libvlc_event_t;

class context;

enum  MediaState {

    NothingSpecial = 0,
    Opening = 1,
    Buffering = 2,
    Playing = 3,
    Paused = 4,
    Stopped = 5,
    Ended = 6,
    Error = 7
};

class  TestVlcVideo
{
public:
    TestVlcVideo();
     void init( std::function<void(int)> eventCallback);
     void setHwnd(const int64_t iHwnd) ;
     bool loadMedia(const char* &url) ;
     int  play() ;
     void pause() ;
     void stop() ;
     void setRatio(const char* &ratio) ;
     int getVolume() ;
     int setVolume(const int volume) ;
     int getMediaState() ;
    libvlc_instance_t * getVlcInstance();
    libvlc_media_player_t * getVlcMediaPlayer();
private:
    static void vlcEvents(const libvlc_event_t *ev, void *param);
    static libvlc_instance_t *m_instance;
    libvlc_media_player_t *m_mediaPlayer = nullptr;
    int64_t m_durationMS;
    std::function<void(int)> m_eventCallback;
    MediaState m_currentMediaState;
};

上面 static 声明的 m_instance 是为了优化效率,不必每次播放视频的时候都新建。

这是第二步工作。

1.3封装 DLL

需要封装真正的 DLL 了,向C#暴露的也是这个类里面的方法。

#pragma once

typedef  int(*CallBackMediaState)(int);

#ifdef DLLVLC_EXPORTS // 用来导出函数
#define DLLVLC_API __declspec(dllexport)
#else // 用来标识为导入函数,对于引用该头文件的外部模块来说dllimport这个标记对编译优化有作用
#define DLLVLC_API __declspec(dllimport)
#endif

#include "Testvlcvideo.h"

namespace TestVLCDLL {

extern "C" {
    /*
     *  @brief   VLC Instance和Player实例初始化
     *  @param   CallBackMediaState callback  回调函数为媒体播放状态
     *  @return  每次vlcInit会返回一个VLC的Player ID,此ID唯一,后面的接口都需要此ID找到对应的Player
     */
    DLLVLC_API int   vlcInit(CallBackMediaState callback);
    /*
     *  @brief  VLC 媒体加载接口
     *  @param  int index   PlayerID
     *  @param  const char *path   媒体路径
     */
    DLLVLC_API bool  vlcLoad(int index, const char *path);
    /*
     *  @brief  设置句柄,如不设置则为默认窗口播放
     *  @param  const int64_t iHwnd windows窗口句柄
     */
    DLLVLC_API bool  vlcSetHwnd(int index,const int64_t iHwnd);
    DLLVLC_API bool  play(int index);
    DLLVLC_API bool  pause(int index);
    DLLVLC_API bool  stop(int index);
    /*
     *  @brief  设置播放窗口比例
     *  @param  形如  16:9  4:3 等字符串
     */
    DLLVLC_API bool  setRatio(int index,const char* ratio);
    /*
     *  @brief  设置媒体播放音量
     */
    DLLVLC_API bool  setVolume(int index, int volume);
    /*
    *  @brief  获取媒体总时长
    */
    DLLVLC_API int64_t  getMediaLength(int index);
    /*
    *  @brief  获取当前播放状态
    */
    DLLVLC_API int   getMediaState(int index);
    /*
    *  @brief  销毁VLC Player
    */
    DLLVLC_API bool  vlcDisponse(int index);

    }
}

首先在最开始定义了 CallBackMediaState 回调函数,对应C++ 层使用函数指针和std::function 都可以。然后使用 DLLVLC_EXPORTS 指示本类为导出类,然后再使用 DLLVLC_API 宏定义导出函数,这些方法都是 dll 暴露给外部调用的方法。

1.4应用程序的导出函数

// DLLVLC.cpp : 定义 DLL 应用程序的导出函数。
#define DLLVLC_EXPORTS
#include "DLLVLC.h"
#include "Testvlcvideo.h"
#include <iostream>
#include <map>
#include <mutex>
#include <atomic>

std::map<int, TestVlcVideo*> g_mapVLC;
std::atomic_int g_iIndex = 0;
std::mutex g_mt;

DLLVLC_API int TestVLCDLL::vlcInit(CallBackMediaState callback)
{
    //如果是初次调用,则初始化instance,否则复用instance
    std::lock_guard<std::mutex> l(g_mt);
    ++g_iIndex;
    TestVlcVideo *vlcVideo = new TestVlcVideo;
    g_mapVLC.emplace(g_iIndex, vlcVideo);
    g_mapVLC.at(g_iIndex)->init(callback);
    return g_iIndex;
}

DLLVLC_API bool  TestVLCDLL::play(int index)
{
    std::lock_guard<std::mutex> l(g_mt);
    TestVlcVideo *vlcVideo = g_mapVLC.at(index);
    if (nullptr == vlcVideo)
    {
        return false;
    }
    vlcVideo->play();
    return true;
}

.......

因为我们采用的是导出接口方法,而不是导出类(导出类比较麻烦,自己测试未能成功),因此在制作 dll 库时,使用静态 map 保存相关实例,使用对应的 init方法和 dispose 方法借助 id 参数创建和销毁对象。

1.5 vlc 简单封装的具体实现

下来再看下我们第一段代码的 cpp 文件,就是 vlc 简单封装的具体实现:

#include "Testvlcvideo.h"
#include <iostream>

libvlc_instance_t *TestVlcVideo::m_instance = nullptr;

TestVlcVideo::TestVlcVideo()
: m_mediaPlayer(nullptr)
, m_durationMS(0)
, m_eventCallback(nullptr)
{

}

void TestVlcVideo::init(std::function<void(int)> eventCallback)
{
    getVlcInstance();
    {
        getVlcMediaPlayer();
        libvlc_event_manager_t *em = libvlc_media_player_event_manager(m_mediaPlayer);
        {
            libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerNothingSpecial, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerOpening, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerBuffering, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);
            libvlc_event_attach(em, libvlc_MediaPlayerPositionChanged, vlcEvents, this);
        }
        m_eventCallback = std::move(eventCallback);
    }
}

void TestVlcVideo::setHwnd(const int64_t iHwnd)
{
    libvlc_media_player_set_hwnd(m_mediaPlayer, (void *)iHwnd);
}

bool TestVlcVideo::loadMedia(const char* &url)
{
    libvlc_media_t *m_media = nullptr;
    std::string url_ = url;
    if (url_.find("://") == std::string::npos)
    {
        m_media = libvlc_media_new_path(getVlcInstance (), url);
    }
    else
    {
        m_media = libvlc_media_new_location(getVlcInstance(), url);
    }
    if (nullptr == m_media)
    {
        m_currentMediaState = MediaState::Error;
        return false;
    }

    libvlc_media_player_set_media(getVlcMediaPlayer (), m_media);
    libvlc_media_parse(m_media);
    m_durationMS = libvlc_media_get_duration(m_media);
    libvlc_media_release(m_media);
    return true;
}

libvlc_instance_t *  TestVlcVideo::getVlcInstance()
{
    if (nullptr == m_instance)
    {
        m_instance = libvlc_new(0, NULL);
    }
    return m_instance;
}

libvlc_media_player_t * TestVlcVideo::getVlcMediaPlayer()
{
    if (nullptr == m_mediaPlayer)
    {
        m_mediaPlayer = libvlc_media_player_new(m_instance);
    }
    return m_mediaPlayer;
}

int TestVlcVideo::play()
{
    return libvlc_media_player_play(m_mediaPlayer);
}

void TestVlcVideo::pause()
{
    if(libvlc_media_player_is_playing(m_mediaPlayer))
    {
        libvlc_media_player_set_pause(m_mediaPlayer, 1);
    }
    else
    {
        libvlc_media_player_set_pause(m_mediaPlayer, 0);
    }
}

到这儿,一般情况下我们还需要配置 def 文件,以避免导出的函数名被增加额外的信息,而不是简短的“play”等。但是可以看到我们在所有的导出函数前增加了“extern C ”标识。意思是这些函数按照 C 标准进行编译,由于C++ 的函数重载,再加上各个编译器的不同,导致编译而出的函数名被(mangled name),且各不相同,但是C不支持重载,因此采用统一的编译规定,同时也可以保证此函数被 C 正确调用,所以我们就无需写 def 文件也可以保证函数名不被破坏。

2、C# 调用

上面简要说完了 C++ 端关于 DLL 的封装,再总结一下大概就是这几点:

  • 至少需要两个文件,一个是自己对具体实现的封装类,一个是导出方法文件,本文中我们没有使用类,而是直接导出函数。
  • 回调函数像这样 typedef int(*CallBackMediaState)(int); 去定义。
  • 导出文件添加宏 dllexport
#ifdef DLLVLC_EXPORTS // 用来导出函数
#define DLLVLC_API __declspec(dllexport)
#else // 用来标识为导入函数,对于引用该头文件的外部模块来说dllimport这个标记对编译优化有作用
#define DLLVLC_API __declspec(dllimport)
#endif

导出函数添加 extern "C" DLLVLC_API 声明。

2.1C# 回调函数声明与定义

[DllImport(@"C:\Users\HiWin10\Desktop\DLLVLC\DLLVLC\DLLVLC\x64\Release\DLLVLC.dll", EntryPoint = "vlcInit",
        SetLastError = true,
        CharSet = CharSet.Ansi,
        ExactSpelling = false,
        CallingConvention = CallingConvention.Cdecl)]
        public extern static int vlcInit(DllcallBack pfun);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate int DllcallBack(int MediaState);

C# 的回调函数即为委托,需要提前定义委托 DllcallBack ,然后我们假定它是被 C++ 认可的,作为参数传入 vlcInit。在下面我们需要写此委托函数具体的实现:

public static int CsharpCall(int MediaState)
        {
            Console.WriteLine(MediaState);
            return MediaState;
        }

使用的时候:

static int index;
        static void Main(string[] args)
        {
            DllcallBack mycall;
            mycall = new DllcallBack(Program.CsharpCall);
            index = vlcInit(mycall);
            ......
        }

经过验证,此种方式的回调函数能被 C++ 承认,对应于C++的 std::function。

2.2C# 导出普通函数调用

[DllImport(@"C:\Users\HiWin10\Desktop\DLLVLC\DLLVLC\DLLVLC\x64\Release\DLLVLC.dll", EntryPoint = "vlcLoad",
        CallingConvention = CallingConvention.Cdecl)]
        public extern static bool vlcLoad(int index, string path);

        [DllImport(@"C:\Users\HiWin10\Desktop\DLLVLC\DLLVLC\DLLVLC\x64\Release\DLLVLC.dll", EntryPoint = "vlcSetHwnd",
        CallingConvention = CallingConvention.Cdecl)]
        public extern static bool vlcSetHwnd(int index, int iHwnd);

上面是 C# 关于普通导出函数的加载方法,在 main 函数中直接进行调用即可。

static int index;
        static void Main(string[] args)
        {
            DllcallBack mycall;
            mycall = new DllcallBack(Program.CsharpCall);
            index = vlcInit(mycall);
            Console.WriteLine(vlcLoad(index, @"D:\1.mp4"));
            Console.WriteLine(getMediaLength(index));
            play(index);
            setRatio(index,"16:9");

其实 C# 端的调用还是比较简单的,上面的方式是采用静态加载的方式,需要将C++ 的 dll 放到 C# 工程 bin 目录下,而动态加载的方式笔者未进行尝试。

整个过程就完成了,使用 C++ 封装的方式可以使用一些只支持 C++,只有 C++ API 的强大库,也可以防止反编译,还可以使代码更好的分层等。

下面附上demo链接,有需要的小伙伴可下载运行(VS2015)

download.csdn.net/download/u0…

到此这篇关于C++ 封装 DLL 供 C# 调用详细介绍的文章就介绍到这了,更多相关C++ 封装 DLL 供 C# 调用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#调用C++版本dll时的类型转换需要注意的问题小结

    C#对于C++的dll引用时,经常会遇到类型转换和struct的转换 1. C++ 里的Char类型是1 个字节,c#里的Char是两个字节,不可以对应使用:可使用c#里的byte对应 2. structType temp = (structType)Marshal.PtrToStructure(IntPtr, typeof(structType));说明:此方式转换只针对包含c++基本类型的结构体,如果包含指针数组的结构体,使用泛型函数比较方便. 3. [StructLayoutAttribu

  • C#调用C++dll方法步骤

    C#调用C++dll的方法和步骤 其他分享涉及到的概念和方法对于像我这样比较菜的选手看起来比较费劲并且很难抓住重点,这里我总结了一段时间的研究成果供初学者救济之用,简单明了. 1.新建项目->Visual C++->Win32项目 MyDLL 注意:C++编写的dll一般是不能直接拿来C#调用,需要先新建个C++的工程把dll里的方法重新封装成可被C#外部调用的函数. 2.MyDLL.cpp里的代码如下: extern "C" _declspec(dllexport)int

  • C# 调用C++写的dll的实现方法

    dll的编写,首先是打开VS新建一个C++的控制台程序,下一步后选择dll以及空文档即可.然后就是添加一个类添加一个方法.方法排头固定格式 extern"C"__declspec(dllexport) 后面加方法即可. 例如如下代码: C++dll代码: 复制代码 代码如下: extern"C"__declspec(dllexport) char* ShowImages(BYTE img[],int w,int h){;} C#调用dll基本也是固定格式,如下样式,

  • 关于C#调用C++dll传指针释放内存问题

    一.传入dll前,在C#中申请内存空间 c#里面的指针即 IntPtr 申请如下: IntPtr SrcImgData = Marshal.AllocHGlobal(length); 这种需要提前知道空间大小,否则无法确定空间大小,会导致dll内部处理时越界报错. c#里面申请空间了,那么c++里面一般就是在这些空间里面操作了,一般不会重新分配内存,那么就不需要加引用了. c++: uchar* SrcImg c#导入dll函数时申明: IntPtr SrcImg 那么内存释放自然也是由c#来进

  • C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型可以对应.这时怎么办,第一反应是C#也定义结构体,然后当成参数传弟.然而,当我们定义完一个结构体后想传递参数进去时,会抛异常,或者是传入了结构体,但是返回值却不是我们想要的,经过调试跟踪后发现,那些值压根没有改变过,代码如下. [DllImport(

  • C++调用C#的DLL程序实现方法

    把C#编译成DLL或者Axtive控件,再由C调用!比如使用C++调用C#的DLL. SwfDotNet是.net下输出flash的类库.SwfDotNet是C#编写的,作者的C#水平,真是令我佩服.这是个特别好的读写Swf文件的库.但是,我要用在C++项目中,怎么让C++调用C#的DLL呢.今天一上午都在琢磨这个问题,耽误了很多时间,原因是编译是出现:warning C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 Unicode 格式以防止数据丢失. 接着就是

  • C++与C#互调dll的实现步骤

    本文实例展示了C++与C#互调dll的实现步骤,在进行大型项目共享dll动态链接库中可以用到.具体方法如下: 一.C#调用C++ dll步骤(只能导出方法): 1. c++建立空项目->源文件文件夹中添加cpp文件和函数 2. c++属性设置中,配置类型设置为动态库dll,公共语言运行时支持改为/clr 3. c#引用c++的dll 4. c#声明c++的方法,并添加 DllImport特性 5. c#工程属性设置为:目标平台x86 6. 注意方法的类型匹配 7. 引发PInvokeStackI

  • C++调用C#的DLL实现方法

    SwfDotNet是C#编写的,这是个特别好的读写Swf文件的库.本文讲述了在C++项目中,怎么让C++调用C#的DLL动态链接库文件. 具体的实现步骤如下: 一.创建C# DLL,需要指定应用类型为"类库",代码: namespace CSLib { public class Class1 { private string name; public string Name { get { return name; } set { name = "Your Name: &qu

  • 基于C#调用c++Dll结构体数组指针的问题详解

    C#调用c++dll文件是一件很麻烦的事情,首先面临的是数据类型转换的问题,相信经常做c#开发的都和我一样把学校的那点c++底子都忘光了吧(语言特性类). 网上有一大堆得转换对应表,也有一大堆的转换实例,但是都没有强调一个更重要的问题,就是c#数据类型和c++数据类型占内存长度的对应关系. 如果dll文件中只包含一些基础类型,那这个问题可能可以被忽略,但是如果是组合类型(这个叫法也许不妥),如结构体.类类型等,在其中的成员变量的长度的申明正确与否将决定你对dll文件调用的成败. 如有以下代码,其

  • C++ 封装 DLL 供 C# 调用详细介绍

    目录 1.VLC代码封装 1.1 QT(C++)工程 1.2static 声明 m_instance 优化效率 1.3封装 DLL 1.4应用程序的导出函数 1.5 vlc 简单封装的具体实现 2.C# 调用 2.1C# 回调函数声明与定义 2.2C# 导出普通函数调用 1.VLC代码封装 1.1 QT(C++)工程 首先需要配置可使用 VLC 正常播放的 QT(C++)工程,获取VLC每一帧并渲染到Qwidget Libvlcapi public static class LIBVLCAPI

  • React组件封装中三大核心属性详细介绍

    目录 1.介绍 2.state 概念 演示 3.props 概念 props与state区别 4.refs 概念 refs种类 5.父子组件 什么是父子组件 父子组件之间传值 1.介绍 React组件中默认封装了很多属性,有的是提供给开发者操作的,其中有三个属性非常重要:state.props.refs. 2.state 概念 state是类组件的一个默认属性,用于标识类组件的状态,负责更新UI,让页面动态变化,当state变化时,组件将被重新渲染. 函数组件没有对象属性(babel默认开启了局

  • C++下如何将TensorFlow模型封装成DLL供C#调用

    目录 生成dll 使用C#调用dll 生成dll IDE使用visual studio 2015. 首先新建一个C++win32工程,选择dll,包括导出符号,但不要预编译头. 模板会提供一个工程同名的头文件和一个源文件. 在源文件中将TensorFlow头文件和其他需要使用的头文件引用进来. 在源文件中定义需要export的函数. 在头文件中给出需导出函数的声明. extern 关键字后加“C”保证在C#调用时不生成中间函数名. 编译环境选择X64,配置好include.lib目录以及附加依赖

  • 把js文件编译成dll供页面调用的方法

    1. 在解决方案中添加一个项目:JSControl 2. 在这个项目添加一个js文件(JScript1.js) 脚本的内容: function showAlert(){ alert('Today is a good dary'); } 3. 改变JScript1.js的属性,Build Action为Embedded Resource(嵌入的资源) 4. 在JSControl项目的AssemblyInfo.cs文件中添加一行:(注意JSControl.JScript1.js,JSControl是

  • JavaScript中的方法调用详细介绍

    JavaScript中,如果function属于一个对象,那么通过对象来访问该function的行为称之为"方法调用".与普通的函数调用不同的是,在进行方法调用时,function中的this指代将发生变化 - this将指代用于调用该function的对象(该对象将成为方法调用的invocation context): 复制代码 代码如下: var x = 99; var sample = {   x:1,   act:function(a){     this.x = a*a;//

  • Python详细介绍模型封装部署流程

    目录 一.桌面应用软件 二.Pyside2&Qt designer 三.模型封装部署 四.Pyinstaller 五.总结 一.桌面应用软件 桌面应用软件是基于GUI(Graphical User Interface,图形用户界面)交互式程序,需要实现GUI库实现前端交互. Python常见的GUI库 TKinter:基于Tk的Python库,这是Python官方采用的标准库,优点是作为Python标准库.稳定.发布程序较小,缺点是控件相对较少: wxPython:基于wxWidgets的Pyt

  • C# 编译生成dll文件供程序调用的两种方法

    一.使用vs2017 创建动态dll文件 方法一: 1.新建-项目-类库 2.创建一个.cs文件 写入代码,例如:建立一个Windows窗体 略丑,简单明了... using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System

  • Java超详细介绍封装与访问控制修符

    概念:我们在写入一个类的时候,为了保护里边的属性不被随意的调用这是我们可以使用特殊的修饰符进行相应的保护,而这样的话我们似乎只能在该类中调用使用了,出现某些特殊情况时就会无法发调用,虽然挺高了安全性但也降低了灵活性,这个时候我们的包装类就出现了,我们通过对某个方法的进行特殊方法的包装来对其进行相应的调用与赋值.就相当于银行为了保护财产会选择将金钱放进保险柜中来确保其的安全,但是当我们要取钱时,银行就要拿钥匙打开保险柜.修饰符相当于银行的保险柜,封装相当于保险柜的钥匙. 访问修饰符如下: 1) p

  • SpringCloud Gateway DispatcherHandler调用方法详细介绍

    目录 前言 DispatcherHandler类声明 handle方法 最后总结一下 前言 之前几节我们分析了请求是如何调用到HttpWebHandlerAdapter的,然后再调用到DispatcherHandler中,通过handle方法来处理具体的请求. DispatcherHandler的注入在自动装配那一节已经说过了,忘记的同学可以重新会看一下. DispatcherHandler类声明 public class DispatcherHandler implements WebHand

  • SpringCloud使用Feign实现远程调用流程详细介绍

    目录 前言 1. 导入依赖坐标 2. 开启Feign自动装配 3. 声明远程调用 4. 替代RestTemplate 5. 测试 前言 本次示例代码的文件结构如下图所示. 1. 导入依赖坐标 在 order-service 的 pom.xml 文件中导入 Feign 的依赖坐标. <!-- Feign远程调用客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifa

随机推荐