2012-02-29 55 views
1

我有一個非託管庫,我想從託管類中使用。該函數的接口是:C++/CLI管理VS. unmanaged short

GetProgress(short* value); 

所以我在管理類中寫道:

short val = 0; 
GetProgress(&val); 

我得到了以下錯誤:

Error C2664: 'GetProgress' : cannot convert parameter 1 from 'cli::interior_ptr' in 'short *' with [ Type=short ]

我讀this topic,所以我改變了我代碼分成:

short val = 0; 
pin_ptr<short*> pVal = &val; 
GetProgress(pVal); 

除了前面的錯誤,我得到

Error C2440: 'initialisation' : cannot convert from 'short *' to 'cli::pin_ptr' with [ Type=short * ]

我該如何解決這個問題?

回答

1

這是一個有趣的。

下面的代碼產生C2664因爲val可以是管理類型:

using namespace System; 

void GetProgress(short* value) 
{ 
    // unmanaged goodness 
} 

ref class XYZ : System::Object 
{ 
    short val; 

    void foo() 
    { 
     GetProgress(&val); 
    } 
}; 

但如果你首先聲明一個本地變量,這一切工作正常...

using namespace System; 

void GetProgress(short* value) 
{ 
    // unmanaged goodness 
} 

ref class XYZ : System::Object 
{ 
    short val; 

    void foo() 
    { 
     short x; 
     GetProgress(&x); 
     val = x; 
    } 
}; 

不是你正在尋找的答案,但我想我會包括它,因爲這是一個簡單的修復。

+0

好的就是這樣。謝謝! :) – gregseth 2012-02-29 10:48:50