I am using c# with wpf to create a class library, I am exporting this as COM interface. I am trying to call this com object from plain C. I have managed to get it working in C++ but am struggling to get it working in plain C.
so I am using the follwing code in c# to create the com interface.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Linq;
using System.Text;
namespace WPF_TEST
{
[ComVisible(true)]
[Guid("92ce2a56-c61f-486c-8854-4d8d88626ba9")]
public interface ITestWPFInterface
{
[DispId(1)]
void TestWPF();
}
[ComVisible(true)]
[Guid("62e2ed3c-5c2a-48c5-851b-e0b1f6daa9cd"),
ClassInterface(ClassInterfaceType.None)]
public class TestWPFInterface : ITestWPFInterface
{
public void TestWPF()
{
WPF_TEST.MainWindow window = new WPF_TEST.MainWindow();
window.ShowDialog();
}
}
}
this code is then packaged into a class library and "register for com interop" is ticked.
I am following the jeff glatt tutorial using the following code in plain c to try and pick up this COM object
Header file
#ifndef _ITESTWPFINTERFACE_H_
#define _ITESTWPFINTERFACE_H_
#include <initguid.h>
DEFINE_GUID(CLSID_ITestWPFInterface, 0x62e2ed3c, 0x5c2a, 0x48c5, 0x85, 0x1b, 0xe0, 0xb1, 0xf6, 0xda, 0xa9, 0xcd);
DEFINE_GUID(IID_ITestWPFInterface, 0x92ce2a56, 0xc61f, 0x486c, 0x88, 0x54, 0x4d, 0x8d, 0x88, 0x62, 0x6b, 0xa9);
#undef INTERFACE
#define INTERFACE ITestWPFInterface
DECLARE_INTERFACE_ (INTERFACE, IUnknown)
{
STDMETHOD (QueryInterface) (THIS_ REFIID, void **) PURE;
STDMETHOD_ (ULONG, AddRef) (THIS) PURE;
STDMETHOD_ (ULONG, Release) (THIS) PURE;
STDMETHOD (TestWPF) (THIS) PURE;
};
#endif
C file
i initialise and get as far as creating the instance and releasing the class factory
#include <stdio.h>
#include <windows.h>
#include <objbase.h>
#include "ITestWPFInterface.h"
int main( int argc, const char* argv[] )
{
ITestWPFInterface *exampleObj;
IClassFactory *classFactory;
HRESULT hr;
HRESULT hres;
int result;
if (!CoInitialize(0))
{
result = GetLastError();
if ((hr = CoGetClassObject(&CLSID_ITestWPFInterface,CLSCTX_INPROC_SERVER,0,&IID_IClassFactory,&classFactory)))
MessageBox(0, "Can't get IClassFactory", "CoGetClassObject error", MB_OK|MB_ICONEXCLAMATION);
else
{
if ((hr = classFactory->lpVtbl->CreateInstance(classFactory,0,&IID_ITestWPFInterface,&exampleObj)))
{
classFactory->lpVtbl->Release(classFactory);
MessageBox(0, "Can't create ITestWPFInterface object", "CreateInstance error", MB_OK|MB_ICONEXCLAMATION);
}
else
{
result = GetLastError();
hr =classFactory->lpVtbl->Release(classFactory);
hres = exampleObj->lpVtbl->TestWPF(exampleObj);
exampleObj->lpVtbl->Release(exampleObj);
}
}
CoUninitialize();
}
else
MessageBox(0, "Can't initialize COM", "CoInitialize error", MB_OK|MB_ICONEXCLAMATION);
return(0);
}
// this builds ok but this returns an E_POINTER error and I get an error that says that 'The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.'
ANY IDEAS?