呼叫 C++ C# 的模板函数

发布于 2024-11-08 07:47:51 字数 326 浏览 3 评论 0原文

我对 C# 的了解非常有限。我的目标是为我的 C# 同事提供 C++ dll API。由于遗留原因,该 dll 必须采用 C++ 语言。

问题 - C++ 模板函数(如下所示,来自 VS)可以在 C# 中封送吗?

class  __declspec(dllexport) Foo
{
public: 
    template <typename T> T* getFoo(T* fooData){return fooData;};
};

如果没有,有什么建议吗?传递到模板函数的每个类型是否都应该有自己的函数,以便 C# 可以对其进行封送?

I have very limited knowledge of C#. My goal is to provide a C++ dll API to my C# co-worker. The dll has to be in C++ for legacy reasons.

Question - Can a C++ template function (shown below from VS) be marshaled in C#?

class  __declspec(dllexport) Foo
{
public: 
    template <typename T> T* getFoo(T* fooData){return fooData;};
};

If not, any suggestions? Should each type that is passed into the template function have their own function so C# can marshal it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你爱我像她 2024-11-15 07:47:53

所以几周后,我能够运行一些东西,我想我会与团队分享它。 (请原谅伪代码的出现)。我基本上自学了 C# 而不是 C++/CLI。

记住这个问题 - C++ 模板函数(如下所示,来自 VS)可以在 C# 中封送吗?

我的解决方法如下:对 C++ 函数进行封送非托管 C# 调用,这些函数可以将调用转换为模板方法。

这是代码:

//C++ code
//C++ Header
class  __declspec(dllexport) Foo
{
    public: 
        template <typename T> T* getFoo(T* fooData){return fooData;};
};

extern "C" __declspec(dllexport) void call_getFoo(Foo* pFoo, void* pfooData, int fooId)
{
    switch(fooId)
    {
        case(ENUM::1) : //Use an enum here for a better switch statement.       
        {   
            //Cast the void pointer to a specific type so the template knows how to use it.
            pFoo->getFoo((*TypeCast*)pfooData);
        break;
        }
    }
}

//C# Code
internal static class UnsafeNativeMethods
{
    const string _dllLocation = "Foo.dll";

    [DllImport(_dllLocation)]
    static public extern void call_getFoo(IntPtr pFoo, IntPtr pfooData, int fooId);           
}

//In a C# method
...
...
//Marshal Up a C# data type to a pointer for C++.
*YOUR TYPE HERE* myType;
int rawsize = Marshal.SizeOf(myType);
IntPtr pfooData = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(myType,pfooData,true);

//call the C++ dll
UnsafeNativeMethods.call_getFoo(pFoo, pfooData, fooId);

//Convert Ptr Back To myType
myType = (*YOUR TYPE HERE*) Marshal.PtrToStructure(pfooData, typeof(*YOUR TYPE HERE*));
...
...

希望有帮助!

So after a few weeks I was able to get something running and I thought I would share it with the group. (Pardon the Pseudo code Appearance). I basically taught myself C# instead of C++/CLI.

Remember the Question - Can a C++ template function (shown below from VS) be marshaled in C#?

My work around is as followed: Make marshaled unmanaged C# calls to C++ functions that can translate calls to the template method.

Here is the Code:

//C++ code
//C++ Header
class  __declspec(dllexport) Foo
{
    public: 
        template <typename T> T* getFoo(T* fooData){return fooData;};
};

extern "C" __declspec(dllexport) void call_getFoo(Foo* pFoo, void* pfooData, int fooId)
{
    switch(fooId)
    {
        case(ENUM::1) : //Use an enum here for a better switch statement.       
        {   
            //Cast the void pointer to a specific type so the template knows how to use it.
            pFoo->getFoo((*TypeCast*)pfooData);
        break;
        }
    }
}

//C# Code
internal static class UnsafeNativeMethods
{
    const string _dllLocation = "Foo.dll";

    [DllImport(_dllLocation)]
    static public extern void call_getFoo(IntPtr pFoo, IntPtr pfooData, int fooId);           
}

//In a C# method
...
...
//Marshal Up a C# data type to a pointer for C++.
*YOUR TYPE HERE* myType;
int rawsize = Marshal.SizeOf(myType);
IntPtr pfooData = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(myType,pfooData,true);

//call the C++ dll
UnsafeNativeMethods.call_getFoo(pFoo, pfooData, fooId);

//Convert Ptr Back To myType
myType = (*YOUR TYPE HERE*) Marshal.PtrToStructure(pfooData, typeof(*YOUR TYPE HERE*));
...
...

Hope that helps!

写下不归期 2024-11-15 07:47:52

问题 - C++ 模板函数(如下所示,来自 VS)可以在 C# 中封送吗?

不。没有从 C# 到 C++ 的兼容二进制接口。您只能从 C# 调用导出的 C 符号。

理论上,您可以在 C++ DLL 中显式实例化模板,这将使它们获得外部链接和导出符号表中的条目。但名称修改将使这些函数无法用于所有实际目的。因此,最好的方法是拥有一个调用底层 C++ 函数的中间 C 兼容层。

Question - Can a C++ template function (shown below from VS) be marshaled in C#?

No. There is no compatible binary interface from C# to C++. You can only call exported C symbols from C#.

Theoretically, you could explicitly instantiate the templates in the C++ DLL, which will cause them to get external linkage and entries in the export symbol table. But name mangling will make the functions unusable for all practical purposes. The best way is therefore to have an intermediate C-compatible layer which calls the underlying C++ functions.

时光礼记 2024-11-15 07:47:52

我认为最好的选择是用 C++/CLI 编写代码。您可以公开可由 C# 代码使用的托管 API,但在需要时仍使用本机 C++。

I think your best bet is to write your code in C++/CLI. You can expose a managed API that can be consumed by the C# code, but still use native C++ when required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文