3

私はいくつかの C++ コードをリンクし、それを COM オブジェクト内にラップして C# 経由でアクセスしようとしてきました。atl プロジェクトを作成し、Add(double a, double b) などの単純なメソッドを追加しました。以下は、私の atl.h ファイルのコードです。

// 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)

以下は、atl.cpp ファイルからのものです。

// 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;
}

私のC#ファイル内で、dllを呼び出しています...参照した後... dllは表示されますが、割り当てられたメソッドは表示されません。これは私の問題です。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);
    }
}
}

Add(2, 3); でデバッグが中断します。「DLL 'com.dll' に 'Add' という名前のエントリ ポイントが見つかりません」と表示されます。何か案は?

4

1 に答える 1

2

DllImport は PInvoke 用です (ネイティブ Win32 dll へ)。
COM 相互運用が必要です。

ATL com オブジェクトを登録してから、.Net または COM コンポーネントと同様に、そのオブジェクトへの参照を追加します。

ATL の代わりに、C++/CLI を介して C++ 機能を公開できます。

お役に立てれば、

于 2010-07-23T15:34:05.397 に答える