public void GiveWheel(Wheel myWheel){
Sim.DLL_GiveWheelToCar(this.nativeCarObject, myWheel);
}
Here you are passing myWheel
which is of type Wheel
to a function that expects an IntPtr
. You need to pass the private field of the Wheel
class that is currently (mis)named nativeCarObject
.
Other points:
- As I hinted above, the
nativeCarObject
member ofWheel
should be renamed asnativeWheelObject
. - The DLL looks like it uses the
cdecl
calling convention. You should specify that in your p/invoke declarations. - It looks like you code leaks the objects that the DLL supplies. I suspect you need to supply some tidy up helpers.
The code might look like something this:
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);
}
This code is still leaking those objects. I'll let you sort that out.