[英]What's the equivalent to Marshal.ReadIntPtr(IntPtr) (C#) in Java?
[英]Marshal 2 IntPtr's in the same function?
我想知道一種方法來封送我用C#語言在C ++中創建的2個類。 (一個教程鏈接將是完美的,我嘗試自己為Google進行搜索,但是我的英語知識並不是那么先進,可以搜索正確的東西。我是從C ++到C#的新手。)
好吧,基本上我擁有的是這樣的:
C ++
EXPORT_API CarClass* DLL_AddCar();
EXPORT_API CarWheel* DLL_AddWheel();
EXPORT_API void DLL_GiveWheelToCar(CarClass* car,CarWheel* wheel);
C#
public class Car
{
#region Members
private IntPtr nativeCarObject;
#endregion Members
public Car()
{
this.nativeCarObject = Sim.DLL_AddCar();
}
// ---> this part is not working
//#region Wrapper methods
//public void GiveWheel(Wheel myWheel){Sim.DLL_GiveWheelToCar(this.nativeCarObject,myWheel);}
//#endregion Wrapper methods
}
public class Wheel
{
#region Members
private IntPtr nativeCarObject;
#endregion Members
public Wheel()
{
this.nativeCarObject = Sim.DLL_AddWheel();
}
}
internal class Sim
{
public const string pluginName = "MyDLL";
#region PInvokes
[DllImport(pluginName)] public static extern IntPtr DLL_AddCar();
[DllImport(pluginName)] public static extern IntPtr DLL_AddWheel();
[DllImport(pluginName)] public static extern void DLL_GiveWheelToCar(IntPtr car,IntPtr wheel);
#endregion PInvokes
}
現在我的問題是在哪里使用“ DLL_GiveWheelToCar”? 我嘗試的方法被注釋掉了,因為它沒有用,是否是邏輯錯誤,還是應該更改編組方式?
謝謝你的建議。
public void GiveWheel(Wheel myWheel){
Sim.DLL_GiveWheelToCar(this.nativeCarObject, myWheel);
}
在這里,您將類型為Wheel
myWheel
傳遞給需要IntPtr
的函數。 您需要傳遞Wheel
類的私有字段,該私有字段當前(錯誤)命名為nativeCarObject
。
其他要點:
Wheel
的nativeCarObject
成員應重命名為nativeWheelObject
。 cdecl
調用約定。 您應該在p / invoke聲明中指定。 該代碼可能看起來像這樣:
public class Car
{
public readonly IntPtr nativeCarObject = Sim.DLL_AddCar();
public void GiveWheel(Wheel myWheel)
{
Sim.DLL_GiveWheelToCar(this.nativeCarObject, myWheel.nativeWheelObject);
}
}
public class Wheel
{
public readonly IntPtr nativeWheelObject = Sim.DLL_AddWheel();
}
internal class Sim
{
public const string pluginName = "MyDLL";
[DllImport(pluginName, CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr DLL_AddCar();
[DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr DLL_AddWheel();
[DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
public static extern void DLL_GiveWheelToCar(IntPtr car, IntPtr wheel);
}
此代碼仍在泄漏那些對象。 我讓你解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.