2017-04-25 4 views
1

VBAから入力として取得し、新しいものを作成してVBAに戻すDLL用に、C++でコードを記述しました。C++ DLLから返されたポインタからVBAの行列全体を取得するにはどうすればよいですか?

次のように書かれている.H DLL:

// FieldTrans.h 
#ifdef FIELDTRANS_EXPORTS 
#define FIELDTRANS_API __declspec(dllexport) 
#else 
#define FIELDTRANS_API __declspec(dllimport) 
#endif 

extern "C" 
{ 
    // Returns the normalized cardinal sinus coefficient 
    FIELDTRANS_API double _stdcall SinC(double x); 
    // Returns the interpolated field matrix: 180° x 360° with Res step 
    FIELDTRANS_API double _stdcall ShannonInterp(double** Matrix[], int MatrixHeight, int MatrixWidth, double Res, double DeltaTheta, double DeltaPhi); 
}; 

と.cppファイル:.defファイル内の呼び出し名を定義した後

// FieldTrans.cpp 
#include "stdafx.h" 
#include "FieldTrans.h" 
#include <math.h> 
#include <vector> 

double _stdcall SinC(double x) 
{ 
    double y; 
    y = 1; 
    if (x != 0) 
    { 
     y = sin(3.14159265358979323846 * x)/(3.14159265358979323846 * x); 
    } 
    return y; 
} 

double** _stdcall ShannonInterp(double** Matrix, int MatrixHeight, int MatrixWidth, double Res, double DeltaTheta, double DeltaPhi) 
{ 
    int k, l, m, n; 
    double iRes, jRes; 
    double ThetaSinC, PhiSinC; 
    int Height = static_cast<int> (180/Res); 
    int Width = static_cast<int> (360/Res); 
    double** Interpolated = new double*[Height]; 
    k = 0; 
    m = 1; 

    for (int i = 0; i < Height; i++) 
    { 
     Interpolated[i] = new double[Width]; 
     iRes = i * Res; 
     do 
     { 
      k = k + 1; 
      m = k + 1; 
     } while (iRes >(k + 1) * DeltaTheta); 
     if (m > MatrixHeight) 
     { 
      m = MatrixHeight - 1; 
     } 
     ThetaSinC = SinC((iRes - k * DeltaTheta)/DeltaTheta); 
     l = 0; 
     n = 1; 
     for (int j = 0; j < Width; j++) 
     { 
      jRes = j * Res; 
      do 
      { 
       l = l + 1; 
       n = l + 1; 
      } while (jRes >(l + 1) * DeltaPhi); 
      if (n > MatrixWidth) 
      { 
       n = 0; 
      } 
      PhiSinC = SinC((jRes - l * DeltaPhi)/DeltaPhi); 
      Interpolated[i][j] = 1 * ThetaSinC * PhiSinC + 2 * ThetaSinC * (1 - PhiSinC) + 3 * (1 - ThetaSinC) * PhiSinC + 4 * (1 - ThetaSinC) * (1 - PhiSinC); 
     } 
    } 
    return Interpolated; 
} 

を、私が書きました私のVBAのコードは、関数の1つを呼び出すには、次の宣言:

Private Declare Function ShannonInterp Lib "C:\NSI2000\Script\FieldTrans.dll" (ByRef Matrix() As Double, ByVal MatrixHeight As Integer, ByVal MatrixWidth As Integer, ByVal Res As Double, ByVal DeltaTheta As Double, ByVal DeltaPhi As Double) As Double 

私はこのような機能を使用することができます任意のクラッシュやエラーをユタ:

PointerCo = ShannonInterp(AmplCo, PointsTheta, PointsPhi, Resolution, 181/(PointsTheta+1), 361/(PointsPhi+1)) 

私の質問は:私はPointerCoを推測しているポインタですが、私は変数にそれが参照行列全体を格納する必要があります。出来ますか?それ、どうやったら出来るの?

答えて

2

いいえあなたはVBAとC++の間で "相互作用"できません。

これを実行する最も簡単な方法は、VBAの古典的なVariant配列にマップする、C++側でSAFEARRAY型のVT_VARIANTを使用することです。インターネット上に散在するこのチュートリアルはたくさんあります。

Microsoft COMおよびそれに関連するサポートクラスを使用していない場合は、スピードアップするために良い月間の設定をお勧めします。

+0

私のケースに合う「ダミーのための」チュートリアルを教えていただけますか? – Noldor130884

+0

Googleの「CComVariant」が良い出発点です。 – Bathsheba

関連する問題