2013-05-07 187 views
1

我在MATLAB中生成C++共享庫,並將其集成到C++中的Win32控制檯應用程序中。我必須從PHP調用這個控制檯應用程序。它有5個輸入,應該從PHP傳遞。當我運行應用程序給出它運行的輸入參數。其正常運行的代碼是如下:在C++ Win32控制檯應用程序中採用輸入參數

#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

您可以將命令行字符串參數使用atof()功能從stdlib.h翻番。當我看到你正在使用的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. 
+0

謝謝。我怎樣才能把它轉換成字符串?因爲我也必須發送img路徑作爲參數。 – user1583647 2013-05-08 01:33:37

+0

@ user1583647 - 它已經是一個字符串了,所以'std:string szt1 = argv [1]'對於ANSI或對於UNICODE是'std :: wstring szt1 = argv [1]'。 – 2013-05-08 04:38:07

相關問題