简体   繁体   English

Matlab无法正确初始化C ++共享库

[英]Matlab could not initialize C++ shared library properly

I am trying to use C++ shared library generated using deploytool in MATLAB in Visual C++. 我正在尝试使用Visual C ++中的MATLAB中通过deploytool生成的C ++共享库。 I am getting an error: Could not initialize the library properly. 我收到一个错误:无法正确初始化库。

I followed: 我跟着:

http://www.mathworks.com/support/solutions/en/data/1-2QTWCE/ and http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and-dlls/ tutorial. http://www.mathworks.com/support/solutions/zh-CN/data/1-2QTWCE/http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and -dlls /教程。

My code is: the .cpp file is : 我的代码是:.cpp文件是:

#include "stdafx.h"
#include "try2.h"
#include <iostream>
#include <string.h>
#include "mex.h"
#include "mclmcr.h"
#include "mclmcrrt.h"
#ifdef _DEBUG
#pragma comment(lib, "try2.lib")
#pragma comment(lib, "mclmcrrt.lib")
#pragma comment(lib, "mclcommain.lib")
#pragma comment(lib, "mclbase.lib")
#pragma comment(lib, "mclmcr.lib")
#pragma comment(lib, "mclxlmain.lib")
#else
#pragma comment(lib, "try2.lib")
#pragma comment(lib, "mclmcrrt.lib")
#pragma comment(lib, "mclcommain.lib")
#pragma comment(lib, "mclbase.lib")
#pragma comment(lib, "mclmcr.lib")
#pragma comment(lib, "mclxlmain.lib")
#endif
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    /* Call the MCR and library initialization functions */

if( !mclInitializeApplication(NULL,0) )
{

      std::cerr << "Could not initialize the application properly."
                  << std::endl;
        return -1;
}
try2Initialize();
if (!try2Initialize())
{

     std::cerr << "Could not initialize the library properly."
                  << std::endl;
        return -1;
}


     mwArray img= "C:/Users/Desktop/dressimages/T1k5aHXjNqXXc4MOI3_050416.jpg";

            shoes(img);
            try2Terminate();
mclTerminateApplication();
            return 0;
}

and the try2.cpp wrapper file generated from MATLAB is 从MATLAB生成的try2.cpp包装文件是

//
  // MATLAB Compiler: 4.18 (R2012b)
  // Date: Tue Apr 23 13:16:46 2013
  // Arguments: "-B" "macro_default" "-W" "cpplib:try2" "-T" "link:lib" "-d"
  // "C:\UsersDocuments\MATLAB\try2\src" "-N" "-p" "vision" "-p"
  // "stats" "-w" "enable:specified_file_mismatch" "-w" "enable:repeated_file"
  // "-w" "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w"
  // "enable:demo_license" "-v"
  // "C:\Users\Desktop\trial\shoes.m" "-a"
  // "C:\Users\Desktop\trial\color_space_file.mat" 
  //
#include <stdio.h>
#define EXPORTING_try2 1
#include "try2.h"
static HMCRINSTANCE _mcr_inst = NULL;
#if defined( _MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__LCC__)
#ifdef __LCC__
#undef EXTERN_C
#endif
#include <windows.h>
static char path_to_dll[_MAX_PATH];
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *pv)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        if (GetModuleFileName(hInstance, path_to_dll, _MAX_PATH) == 0)
            return FALSE;
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
    }
    return TRUE;
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
static int mclDefaultPrintHandler(const char *s)
{
  return mclWrite(1 /* stdout */, s, sizeof(char)*strlen(s));
}
#ifdef __cplusplus
} /* End extern "C" block */
#endif
#ifdef __cplusplus
extern "C" {
#endif
static int mclDefaultErrorHandler(const char *s)
{
  int written = 0;
  size_t len = 0;
  len = strlen(s);
  written = mclWrite(2 /* stderr */, s, sizeof(char)*len);
  if (len > 0 && s[ len-1 ] != '\n')
    written += mclWrite(2 /* stderr */, "\n", sizeof(char));
  return written;
}
#ifdef __cplusplus
} /* End extern "C" block */
#endif
/* This symbol is defined in shared libraries. Define it here
 * (to nothing) in case this isn't a shared library. 
 */
#ifndef LIB_try2_C_API
#define LIB_try2_C_API /* No special import/export declaration */
#endif
LIB_try2_C_API 
bool MW_CALL_CONV try2InitializeWithHandlers(
    mclOutputHandlerFcn error_handler,
    mclOutputHandlerFcn print_handler)
{
    int bResult = 0;
  if (_mcr_inst != NULL)
    return true;
  if (!mclmcrInitialize())
    return false;
  if (!GetModuleFileName(GetModuleHandle("try2"), path_to_dll, _MAX_PATH))
    return false;
    {
        mclCtfStream ctfStream = 
            mclGetEmbeddedCtfStream(path_to_dll);
        if (ctfStream) {
            bResult = mclInitializeComponentInstanceEmbedded(   &_mcr_inst,
                                                                error_handler, 
                                                                print_handler,
                                                                ctfStream);
            mclDestroyStream(ctfStream);
        } else {
            bResult = 0;
        }
    }  
    if (!bResult)
    return false;
  return true;
}
LIB_try2_C_API 
bool MW_CALL_CONV try2Initialize(void)
{
  return try2InitializeWithHandlers(mclDefaultErrorHandler, mclDefaultPrintHandler);
}
LIB_try2_C_API 
void MW_CALL_CONV try2Terminate(void)
{
  if (_mcr_inst != NULL)
    mclTerminateInstance(&_mcr_inst);
}
LIB_try2_C_API 
void MW_CALL_CONV try2PrintStackTrace(void) 
{
  char** stackTrace;
  int stackDepth = mclGetStackTrace(&stackTrace);
  int i;
  for(i=0; i<stackDepth; i++)
  {
    mclWrite(2 /* stderr */, stackTrace[i], sizeof(char)*strlen(stackTrace[i]));
    mclWrite(2 /* stderr */, "\n", sizeof(char)*strlen("\n"));
  }
  mclFreeStackTrace(&stackTrace, stackDepth);
}
LIB_try2_C_API 
bool MW_CALL_CONV mlxShoes(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "shoes", nlhs, plhs, nrhs, prhs);
}
LIB_try2_CPP_API 
void MW_CALL_CONV shoes(const mwArray& imagepath)
{
  mclcppMlfFeval(_mcr_inst, "shoes", 0, 0, 1, &imagepath);

  } 

When I try to run it, it cannot initialize the try2Initialize(). 当我尝试运行它时,它无法初始化try2Initialize()。 What could be the solution? 有什么解决方案? It is a win32 console application. 这是一个win32控制台应用程序。

I am using Visual Studio 2010, MATLAB R2012b-64bit and my os is Windows 7. P>S. 我正在使用Visual Studio 2010,MATLAB R2012b-64bit,我的操作系统是Windows 7。 Sorry for the excess code just want to give the details of the program. 很抱歉,多余的代码只想提供程序的详细信息。

I am not sure about R2012b and 64bit version. 我不确定R2012b和64位版本。 I am using r2012a. 我正在使用r2012a。 But I think I can share my stories. 但是我想我可以分享我的故事。 I use deploytool all the time, so the generated method is named as your method : try2Initialize(); 我一直都使用deploytool,所以生成的方法被命名为您的方法:try2Initialize(); Recently I changed to mcc command, I found the in c file, the method name is still the same, but if I check the method name in the dll file with some 3 party tool, like dll export viewer: http://www.nirsoft.net/utils/dll_export_viewer.html 最近我更改为mcc命令,我发现c文件中的方法名称仍然相同,但是如果我使用3种第三方工具(例如dll导出查看器)检查dll文件中的方法名称: http:// www。 nirsoft.net/utils/dll_export_viewer.html

The actual method name is _try2Initialize(); 实际的方法名称是_try2Initialize();。 there is a little _ in the front 前面有一点

So I suggest you check the method name if you are running out of option. 因此,如果您用尽了选项,建议您检查方法名称。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM