首页 > 解决方案 > 用于 LabView 的原生 c++ 的 c++/cli dll 包装器

问题描述

我正在尝试为 IO Industries Core2 DVR 编写一个 c++/cli 包装器,然后 LabView 将使用它。该公司提供了一个包含所有头文件(用 c++ 编写)和 boost 库的 SDK。我已经设法构建了一个构建器,LabView 能够通过 .net 托盘看到该功能。

// ManagedProject.h

#pragma once
#include "core_api_helper.h"
#include "core_api.h"


using namespace System;
using namespace CoreApi;

namespace ManagedProject {

//Setup class
public ref class Setup
{
private:

public:
    unsigned int initializeTest();

};
}

// 这是 DLL 包装器。

#include "stdafx.h"

#include "ManagedProject.h"
#include "core_api_helper.h"
#include "core_api.h"
#include "resource.h"

using namespace CoreApi;
using namespace Common;
using namespace ManagedProject;

//Global handles

//A handle to the Core Api
InstanceHandle g_hApi;

//A handle to the Core Api Device Collection
DeviceCollectionHandle g_hCoreDeviceCollection;


unsigned int Setup::initializeTest()
{

try
{
    //Initialize the Core API (must be called before any other Core API functions)
    //Returns a handle to the Core Api
    g_hApi = Instance::initialize();

    // get a collection of Core devices
    g_hCoreDeviceCollection = g_hApi->deviceCollection();

    unsigned int deviceCount = g_hCoreDeviceCollection->deviceCount();

    return deviceCount;
}
catch (GeneralException& e)
{
    e.what();
    return 3;

}

}

但是,当我在调试模式下通过 Visual Studio 2015 运行 LabView 时,我遇到了以下问题,返回给 LabView 的是来自 catch 块的 3。

调试模式下的第一次中断(NULL ptr) 注意: InstanceHandle 是一个 shared_ptr

可以看出该变量是一个 NULL 指针,同样的事情也发生在 g_hCoreDeviceCollectoin 上。我想我需要用新命令来实例化它,但我有点不确定,因为 InstanceHandle 是一个 shared_ptr。

任何帮助将非常感激

标签: c++c++-cliwrappershared-ptrlabview

解决方案


C++/CLI 有一个很棒的特性,叫做混合模式。您可以在同一代码中(在同一 C++/CLI 类中)同时使用托管数据类型和本机数据类型。尝试在包装器中直接使用用 C++ 编写的 SDK 中的对象。


推荐阅读