gpt4 book ai didi

c# - 从 C# 中的 ATL dll 文件访问方法

转载 作者:行者123 更新时间:2023-11-30 17:21:33 25 4
gpt4 key购买 nike

我一直在尝试链接一些 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' 的入口点。”有什么想法吗?

最佳答案

DllImport 用于 PInvoke(到 native Win32 dll)。
您需要 COM Interop。

注册您的 ATL com 对象,然后添加对它的引用,就像您对任何 .Net 或 COM 组件所做的那样。

作为 ATL 的替代方案,您可以通过 C++/CLI 公开您的 C++ 功能。

希望对您有所帮助,

关于c# - 从 C# 中的 ATL dll 文件访问方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3319662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com