如何从C ++ DLL返回双指针到C#?

时间:2015-01-09 15:01:54

标签: c# c++ dll pinvoke marshalling

我正在尝试将指针从我的C ++ dll返回到C#。我尝试了所有的东西,但是我的double类型的指针没有值。

下面是我对C ++ dll的导入:

[DllImport("/Resources/libfli.dll", EntryPoint = "FLIGetTemperature")]
public static extern unsafe int FLIGetTemperature(long dev, double* temperature);
// Get the temperature of a given camera. This function places the temperature of the CCD camera 
// cold finger of device dev in the location pointed to by temperature.
// Return Value: Zero on success. Non-zero on failure.
// Parameters: dev Camera device to get the temperature of.
// temperature Pointer to where the temperature will be placed.
// See Also: FLISetTemperature

从FLI_SDK_Documentation

定义FliGetTemperatre
LIBFLIAPI FLIGetTemperature (flidev_t dev, double* temperature)
Get the temperature of a given camera.

以下是我如何在C#中声明对.Dll的调用:

unsafe public void GetTheTemperatureOfTheCamera()
{
    int success=0;
    long ldev = 0;
    long* dev = &ldev;
    double lTemperature = 0;
    double* temperature = &lTemperature;
    success = FLIGetTemperature(ldev, temperature);
}

当我运行我的代码时,我收到以下错误:

  

未处理的类型' System.AccessViolationException'发生在myApplication.exe

中      

附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

我也尝试过编组和IntPtr,但这也不起作用。

我尝试过Fliopen这个功能。它成功了。以下是FliOpen的代码。

    [DllImport("/Resources/libfli.dll", EntryPoint = "FLIOpen")]
    public static extern unsafe int FLIOpen(long* dev, string name, long domain);

    unsafe public int InitDevice()
    {
        long ldev = 0;
        long* dev = &ldev;
        int success;
        string deviceName = "flipro0";// this is default name for device
        long domainName = 258; //this is default domain name

        success = FLIOpen(dev, deviceName, domainName);
        return success;
    }

FLIOpen方法虽然成功但

1 个答案:

答案 0 :(得分:0)

阅读文档,我会写这样的东西:

unsafe public void GetTheTemperatureOfTheCamera()
{
     int success=0;
     long dev = 0;
     long* ldev  = &dev;
     success = FLIOpen(ldev, deviceName, domainName); //Get the device handle.

     if (success != 0)
        throw new Exception("Cannot open device");

     double lTemperature = 0;
     double* temperature = &lTemperature;
     success = FLIGetTemperature(dev, temperature);
}