tags:

views:

79

answers:

1

Hi, I've been working with trying to link up some c++ code and wrap it inside a COM object to access via C#. I created an atl project and added a simple method such as Add(double a, double b). The following is the code from my atl.h file:

// atl.h : Declaration of the Catl
#pragma once
#include "resource.h"       // main symbols

#include "com_i.h"


#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif



// Catl

class ATL_NO_VTABLE Catl :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<Catl, &CLSID_atl>,
public Iatl
{
public:
Catl()
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_ATL)

DECLARE_NOT_AGGREGATABLE(Catl)

BEGIN_COM_MAP(Catl)
COM_INTERFACE_ENTRY(Iatl)
END_COM_MAP()



DECLARE_PROTECT_FINAL_CONSTRUCT()

HRESULT FinalConstruct()
{
    return S_OK;
}

void FinalRelease()
{
}

public:

STDMETHOD(Add)(DOUBLE a, DOUBLE b);
};

OBJECT_ENTRY_AUTO(__uuidof(atl), Catl)

The following is from the atl.cpp file

// atl.cpp : Implementation of Catl

#include "stdafx.h"
#include "atl.h"

STDMETHODIMP Catl::Add(DOUBLE a, DOUBLE b)
{
// TODO: Add your implementation code here

return a + b;
}

Inside my c# file I'm calling the dll... after i referenced it... it sees the dll but not the methods assigned. which is my problem. Heres the code from program.cs

sing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace sharpdll
{
class Program
{
    [DllImport("com.dll")]
    public static extern double Add(double a, double b);

    static void Main(string[] args)
    {
        Add(2, 3);
    }
}
}

Debugging breaks at Add(2, 3); Says "Unable to find an entry point named 'Add' in DLL 'com.dll'." Any ideas?

+1  A: 

DllImport is for PInvoke (to native Win32 dlls).
You want COM Interop.

Register your ATL com object, then Add a reference to it, as you would to any .Net or COM component.

An alternative to ATL, you can expose your C++ functionality through C++/CLI.

Hope this helps,

Binary Worrier
Thanks binary worrier, that led me in the right direction. But after registering the object, then referencing the dll as usual inside the bin folder of the project, builds fine but when I run the project... I have the following error:Unable to load DLL 'com.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)I don't understand why its getting this error... the reference worked fine, any ideas? Almost there!!
I would be willing to show my screen to make sure everything is in place, via skype, crossloop, etc.
At a guess I would say you're not building the COM object correctly. It's not my area of expertise. You should ask another question with this specific problem. Sorry I can't be of more help :)
Binary Worrier