Jump to content
  • Advertisement
Sign in to follow this  
majek

[.net] PInvoke/COM interface wrapper question

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I don't know if my question belongs here but I thought I'd ask anyway since I've had no luck elsewhere. I'm trying to write a wrapper class in C# around a dll written in C. I've managed to load the library and access it's core functions, but I also need to create a wrapper around an interface inside the dll. This is where I run into problems. The CreateInterface() function returns a pointer to the COM interface, but I can't figure out how to get the interface associated with it. The Marshal class has methods like GetComInterfaceForObject and GetTypedObjectForIUnknown, but I have no idea how to use them and there's no good examples in the MSDN docs or online. I've tried everything but I can't figure it out. Please help!
public class COMWrapper
{
	// Delegates
	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	private delegate bool dTestFunction();
	
	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	private delegate IntPtr dCreateInterface();
	
	// Objects
	public dTestFunction TestFunction;
	public dCreateInterface CreateInterface;
	
	// Constructor
	public COMWrapper()
	{
		// Load library
		IntPtr ptrLibrary = NativeMethods.LoadLibrary("TestDLL.dll");
		
		// Load functions
		IntPtr ptrFunction = NativeMethods.GetProcAddress(ptrLibrary, "TestFunction");
		TestFunction = (dTestFunction)Marshal.GetDelegateForFunctionPointer(ptrFunction, typeof(dTestFunction));
		
		ptrFunction = NativeMethods.GetProcAddress(ptrLibrary, "CreateInterface");
		CreateInterface = (dCreateInterface)Marshal.GetDelegateForFunctionPointer(ptrFunction, typeof(dCreateInterface));
		
		// Test core function (works)
		bool testResult = TestFunction();
		
		// Create interface (second line causes System.ExecutionEngineException)
		IntPtr ptrTestInterface = CreateInterface();
		ITestInterface iTestInterface = (ITestInterface)Marshal.GetTypedObjectForIUnknown(ptrTestInterface, typeof(ITestInterface));
		
		// Test interface function (never gets this far)
		testResult = iTestInterface.TestFunction();		
		
		// Cleanup
		Marshal.ReleaseComObject(iTestInterface); // ???
		NativeMethods.FreeLibrary(ptrLibrary);
	}
}
	
[ComVisible(true)]
public interface ITestInterface
{
	bool TestFunction();
}

Share this post


Link to post
Share on other sites
Advertisement
I don't know much about creating a com wrapper manually, but if you have a COM dll you can use tlbimp and it will generate a com wrapper for you. Visual Studio will also do this automatically for you if you add the dll as a com reference.

Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!