Writing a MATLAB file from a DLL called by LabVIEW

混江龙づ霸主 提交于 2019-12-10 20:14:19

问题


After testing my code alone, I decided to incorporate it into the project finally. The issue is, when LabVIEW 2010 (SP1, 64-bit) loads the custom DLL, it walks the dependencies and finds that it needs tbb.dll eventually. Well, as far as I can tell, LabVIEW uses its own version of tbb.dll. And its version is missing the entry point ?throw_exception_v4@internal@tbb@@YAXW4exception_id@12@@Z. I ran the function before separately, and it worked fine. It would appear this is not an unheard of LabVIEW error which hasn't been addressed.

Since this is a dependency of a third party library, I can either not use the MATLAB libraries and make everything from scratch, or I can force LabVIEW to load the working version of tbb.dll, which appears to mean copying the DLL into the Windows folder. Neither of these are usable solutions. Additionally, we do not have the license for Mathscript. Any ideas?

Just in case I did something wrong when I modified their example, a simplified version of my code is as follows:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "mat.h"
#include "createMatFile.h"

export "C" int createMatFile(const char *filename, int* dataArray, int count)
{
    MATFile *pmat;
    mxArray *data;
    mwSize dims[2] = {1,1};

    //Open the file to write to
    pmat = matOpen(filename, "w");
    if (pmat == NULL) {
        printf("Error creating file %s\n", filename);
        printf("(Do you have write permission in this directory?)\n");
        return(EXIT_FAILURE);
    }

    //Convert data to double
    double* dataDouble = new double[count];

    for(int i=0; i<count; i++)
    {
        dataDouble[i] = (double)data[i];
    }

    //Populate the mxArrays
    dataArray = mxCreateDoubleMatrix(1,count,mxREAL);
    memcpy((void *)(mxGetPr(dataArray)), (void *)dataDouble, sizeof(*dataDouble)*count);

    //Put the shape struct in the .mat file
    int status = matPutVariable(pmat, "data", dataArray);
    if (status != 0) {
        printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
        return(EXIT_FAILURE);
    } 

    //Clean up
    delete [] dataDouble;
    mxDestroyArray(dataArray);

    //Close the file to write to
    if (matClose(pmat) != 0) {
        printf("Error closing file %s\n",filename);
        return(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
}

The .h file is just the prototype of the function.


回答1:


Because LabVIEW installs its own version of tbb.dll into the windows path, and loads that, the only way to fix it is to copy the new tbb.dll in the system32 folder. This is not a good solution, so the only answer possible appears to be either:

  • Use this system32 hack which will probably cause other issues down the road.
  • Create some hack to dynamically load the proper DLL in a separate process with nothing loaded from LabVIEW and call the MATLAB DLL from that process then return the value computed to the LabVIEW process. Since my program only writes data to a file, it doesn't have to return anything back to LabVIEW, simplifying this choice.
  • Or just stop using MATLAB and LabVIEW in conjunction.


来源:https://stackoverflow.com/questions/25919667/writing-a-matlab-file-from-a-dll-called-by-labview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!