HighTechTalks DotNet Forums  

P/Invoke - Abstract C++ Class

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss P/Invoke - Abstract C++ Class in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Armand du Plessis
 
Posts: n/a

Default P/Invoke - Abstract C++ Class - 11-08-2005 , 03:00 AM






Hi,

I've got an c++ class we need to use via interop. I wrote a little wrapper
around the methods required using the thiscall calling convention to pass the
pointer when invoking the class's members. (I'm getting an IntPtr to the
class back from another interop call)
The problem is the only exports I've got for the unmanaged type is for the
abstract base class, I'm guessing the ptr I'm getting back is for a concrete
type so some of the ordinal numbers I have is not valid. Ie. All the method
invocations works 100% but for the destructor which is defined as a virtual
method.
My problem is after I'm done with the class there's no way for me to get
this type to dispose properly and this poses a big problem for me at the
moment, hence this post. I'm not sure if it's possible to invoke the concrete
type's destructor from managed code as the ordinal number points to the base
class's method. Apologies if the description don't make all that much sense,
my knowledge of unmanaged code is a bit limited at the moment.

Any pointers (no pun intended) or advice/workarounds would be much
appreciated!

- Armand

I've included some mock code to illustrate the types below :

C++
------------------------------------------
class SomeNativeType
{
public:
virtual ~SomeNativeType();
int Count() const;
};


C#
------------------------------------------
public class SomeWrapper: IDisposable
{
#region Fields

private IntPtr _this;

#endregion

#region Constructor

public SomeWrapper(IntPtr nativeContext)
{
this._this = nativeContext;
}

public int Count
{
get
{
return count(_this);
}
}


private bool _disposed = false;
public void Dispose(bool disposing)
{
if(! _disposed)
{
Close(_this);
_this = IntPtr.Zero;
_disposed = true;
if(disposing)
{
// Free managed resources here
GC.SuppressFinalize(this);
}

}
}

public void Dispose()
{
Dispose(true);
}

~SomeWrapper()
{
Dispose(false);
}


// int Count() const;
[DllImport("native.dll", EntryPoint = "#xx", CallingConvention =
CallingConvention.ThisCall)]
private static extern int count(IntPtr _this);

// virtual ~SomeNativeType();
[DllImport("native.dll", EntryPoint = "#xx", CallingConvention =
CallingConvention.ThisCall)]
private static extern void Close(IntPtr _this);
}
}

Reply With Quote
  #2  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: P/Invoke - Abstract C++ Class - 11-12-2005 , 06:17 PM








"Armand du Plessis" <armand (AT) _nospam_dotnet (DOT) org.za> wrote

Quote:
Hi,

I've got an c++ class we need to use via interop. I wrote a little wrapper
around the methods required using the thiscall calling convention to pass
the
pointer when invoking the class's members. (I'm getting an IntPtr to the
class back from another interop call)
Just currious how you get the this pointer?

Quote:
The problem is the only exports I've got for the unmanaged type is for the
abstract base class, I'm guessing the ptr I'm getting back is for a
concrete
type so some of the ordinal numbers I have is not valid. Ie. All the
method
invocations works 100% but for the destructor which is defined as a
virtual
method.
My problem is after I'm done with the class there's no way for me to get
this type to dispose properly and this poses a big problem for me at the
moment, hence this post. I'm not sure if it's possible to invoke the
concrete
type's destructor from managed code as the ordinal number points to the
base
class's method. Apologies if the description don't make all that much
sense,
my knowledge of unmanaged code is a bit limited at the moment.

Any pointers (no pun intended) or advice/workarounds would be much
appreciated!

Don't try to interop with unmanaged C++ Classes, just write a simple wrapper
using Managed Extentions C++ or (better) C++/CLI (VS2005).


Quote:
- Armand

I've included some mock code to illustrate the types below :

C++
------------------------------------------
class SomeNativeType
{
public:
virtual ~SomeNativeType();
int Count() const;
};


C#
------------------------------------------
public class SomeWrapper: IDisposable
{
#region Fields

private IntPtr _this;

#endregion

#region Constructor

public SomeWrapper(IntPtr nativeContext)
{
this._this = nativeContext;
}

public int Count
{
get
{
return count(_this);
}
}


private bool _disposed = false;
public void Dispose(bool disposing)
{
if(! _disposed)
{
Close(_this);
_this = IntPtr.Zero;
_disposed = true;
if(disposing)
{
// Free managed resources here
GC.SuppressFinalize(this);
}

}
}

public void Dispose()
{
Dispose(true);
}

~SomeWrapper()
{
Dispose(false);
}


// int Count() const;
[DllImport("native.dll", EntryPoint = "#xx", CallingConvention =
CallingConvention.ThisCall)]
private static extern int count(IntPtr _this);

// virtual ~SomeNativeType();
[DllImport("native.dll", EntryPoint = "#xx", CallingConvention =
CallingConvention.ThisCall)]
private static extern void Close(IntPtr _this);
}
}



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.