Search code examples
c#c++mfccomatl

How to Pass an ATL com object from c++ to c#


using ATl i have created a com object called "Comptes" ,that has properties like name,username. this is the idl file for the atl project

import "oaidl.idl";
import "ocidl.idl";

[
    object,
    uuid(15297371-6D05-4466-B80D-26207555CD28),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface Icompte : IDispatch{
    [propget, id(1)] HRESULT name([out, retval] BSTR* pVal);
    [propput, id(1)] HRESULT name([in] BSTR newVal);
    [propget, id(2)] HRESULT sername([out, retval] BSTR* pVal);
    [propput, id(2)] HRESULT sername([in] BSTR newVal);
    [propget, id(3)] HRESULT email([out, retval] BSTR* pVal);
    [propput, id(3)] HRESULT email([in] BSTR newVal);
    [propget, id(4)] HRESULT phone([out, retval] BSTR* pVal);
    [propput, id(4)] HRESULT phone([in] BSTR newVal);
};
[
    uuid(73A9DD4C-CE2D-4419-9F95-4A84C4E3B271),
    version(1.0),
]
library ATLOBJECTSLib
{
    importlib("stdole2.tlb");
    [
        uuid(1199A9AD-8AF7-4A25-A10B-DD06FB294B2E)      
    ]
    coclass compte
    {
        [default] interface Icompte;
    };
};

i have inculded the header and cpp file in an c++ mfc project and was able to estantiate the com object ,now in the mfc project i am sending data to c# project (just simple strings) this the mfc part :

void CmfcappDlg::OnBnClickedCreateacountbutt()
{

    CoInitializeEx(nullptr, COINIT::COINIT_MULTITHREADED);
    Icompte* pcompte;

    HRESULT hr = CoCreateInstance(CLSID_compte,nullptr,CLSCTX_INPROC_SERVER,IID_Icompte,(LPVOID*)&pcompte);
    if (SUCCEEDED(hr)) 
    {
        namectrl.GetWindowText(name);
        sernamectrl.GetWindowText(sername);
        emailctrl.GetWindowText(email);
        phonectrl.GetWindowText(phone);
        
        pcompte->put_name(name.AllocSysString());
        pcompte->put_sername(sername.AllocSysString());
        pcompte->put_phone(phone.AllocSysString());
        pcompte->put_phone(email.AllocSysString());
        //Managed::showcswindow(name, sername);

    }

    CoUninitialize();
}

now i can send the dat to the c# part with this class :

#include "stdafx.h"
#include "Managed.h"
#using <System.dll>
using namespace System;
using namespace ::gettingdatafromc;

void Managed::showcswindow(CString name,CString sername)
{
    services::showwindow(gcnew String(name), gcnew String(sername));
}

and receive it in c# project with this class :

namespace gettingdatafromc
{
    public static class services
    {
        public static void showwindow(string name, string sername)
        {

        }
    }
}

everything works fine exept that i want to send the com object by passing it as a parameter to the showwindow method rather than a simple string to the c# project is this even dowable ,


Solution

  • Try to pass raw IUnkown* pointer to C# and then use Marshal.GetObjectForIUnknown to convert IntPtr to object and then cast this object to Icompte. You will need to add reference to the corresponding TypeLib into C# project to be able to perform such a cast.