我正在使用SWIG來製作與緊湊框架(WinCE)兼容的C#綁定。我已經解決了大部分的直接問題,但我的下一個攔截器是一些函數返回一個double。生成封裝器,但它們在運行時失敗,因爲CF框架不會封送非整數數據類型(http://msdn.microsoft.com/en-us/library/aa446536.aspx)更改SWIG封裝函數返回值
我的示例失敗是試圖包裝這個功能:
double getMaxMagnification() const
{
return m_maxMag;
}
SWIG生成此包裝:
SWIGEXPORT double SWIGSTDCALL CSharp_LTIImageFilter_getMaxMagnification(void * jarg1) {
double jresult ;
LizardTech::LTIImageFilter *arg1 = (LizardTech::LTIImageFilter *) 0 ;
double result;
arg1 = (LizardTech::LTIImageFilter *)jarg1;
result = (double)((LizardTech::LTIImageFilter const *)arg1)->getMaxMagnification();
jresult = result;
return jresult;
}
這是NG,因爲它需要編組雙返回值。
我手動更改這通過傳入的指針返回雙:
SWIGEXPORT void SWIGSTDCALL CSharp_LTIImageFilter_getMaxMagnification(void * jarg1, void *jarg2) {
fprintf(stderr, "CSharp_LTIImageFilter_getMaxMagnification\n");
//double jresult ;
LizardTech::LTIImageFilter *arg1 = (LizardTech::LTIImageFilter *) 0 ;
double result;
arg1 = (LizardTech::LTIImageFilter *)jarg1;
result = (double)((LizardTech::LTIImageFilter const *)arg1)->getMaxMagnification();
*((double*)jarg2) = result;
//jresult = result ;
//return jresult;
}
使得在C#聲明文件和實現類的相應變化後,該按預期工作。
也就是說,
互操作宣言
NG:
[DllImport("LizardTech_SdkInterop.dll", EntryPoint="CSharp_LTIImageFilter_getMaxMagnification")]
public static extern double LTIImageFilter_getMaxMagnification(IntPtr jarg1);
OK:
[DllImport("LizardTech_SdkInterop.dll", EntryPoint="CSharp_LTIImageFilter_getMaxMagnification")]
public static extern void LTIImageFilter_getMaxMagnification(IntPtr jarg1, ref double jarg2);
實現類
NG:
public override double getMaxMagnification() {
double ret = RasterSDKPINVOKE.LTIImageFilter_getMaxMagnification(swigCPtr);
return ret;
}
OK:
public override double getMaxMagnification() {
double ret = 0;
RasterSDKPINVOKE.LTIImageFilter_getMaxMagnification(swigCPtr, ref ret);
return ret;
}
我怎樣才能SWIG爲我做到這一點?我認爲這些任務是:
(a)將包裝器函數的返回類型(僅)從double更改爲void (b)將參數(double指針)添加到參數列表中,以便包裝器可以發送(c)使互操作聲明反映上述兩個變化 (d)使C#包裝器調用新的包裝函數。
一如既往的大圖片重新定位是讚賞。