在C ++ Win32控制台应用程序中获取输入参数

时间:2013-05-07 11:51:10

标签: visual-c++ console-application matlab-deployment

我在MATLAB中生成了C ++共享库,并将其集成到C ++的Win32控制台应用程序中。我必须从PHP调用这个控制台应用程序。它有5个输入,应该从PHP传递。当我运行应用程序时,它会运行输入参数。正确运行的代码如下:

#include "stdafx.h"
#include "shoes_sharedlibrary.h"
#include <iostream>
#include <string.h>
#include "mex.h"



using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    /* Call the MCR and library initialization functions */
if( !mclInitializeApplication(NULL,0) )
{

    exit(1);
}

if (!shoes_sharedlibraryInitialize())
{

    exit(1);
}



     mwArray img= "C:/Users/aadbi.a/Desktop/dressimages/T1k5aHXjNqXXc4MOI3_050416.jpg";
     double wt1 = 0;
     mwArray C(wt1);
     double wt2=0;
     mwArray F(wt2);
     double wt3=0;
     mwArray T(wt3);
     double wt4=1;
     mwArray S(wt4);



           test_shoes(img,C,F,T,S);
            shoes_sharedlibraryTerminate();
            mclTerminateApplication();
            return 0;
}

C,F,T,S是介于0和1之间的值。如何在_TCHAR *中传递输入参数?如何将_TCHAR *转换为十进制或双精度并再次将其转换为mwArray为传递给test_shoes。 test_shoes只接受mwArray作为输入。

test_shoes函数定义是:

void MW_CALL_CONV test_shoes(const mwArray& img_path, const mwArray& Wcoarse_colors, 
                             const mwArray& Wfine_colors, const mwArray& Wtexture, const 
                             mwArray& Wshape)
{
  mclcppMlfFeval(_mcr_inst, "test_shoes", 0, 0, 5, &img_path, &Wcoarse_colors, &Wfine_colors, &Wtexture, &Wshape);
}

1 个答案:

答案 0 :(得分:2)

您可以使用atof()中的stdlib.h函数将命令行字符串参数转换为double。正如我所看到你使用的是TCHAR等价物,有一个宏包含了对UNICODEANSI版本的正确调用,所以你可以这样做(假设你的命令行参数)是按正确的顺序)

#include "stdafx.h"
#include "shoes_sharedlibrary.h"
#include <iostream>
#include <string.h>
#include "mex.h"

#include <stdlib.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    // ... initial code

    // convert command line arguments to doubles ...
    double wt1 = _tstof(argv[1]);
    mwArray C(wt1);
    double wt2 = _tstof(argv[2]);
    mwArray F(wt2);
    double wt3 = _tstof(argv[3]);
    mwArray T(wt3);

    // ... and so on ....
}

请注意,argv[0]将包含命令行中指定的程序名称,因此参数从argv[1]开始。然后您的命令行可以是:

yourprog.exe 0.123 0.246 0.567 etc.
相关问题