c++ CLI Export void return __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

我怕爱的太早我们不能终老 提交于 2020-07-10 08:18:11

问题


I'm trying to export some void/functions from a C++ Cli that wraps C# .Net Functions.

In this moment I can properly export methods that return integer value, but when I try to export a Void, I get the error:

Error C3395 'Test2': __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention ClassLibrary1

This is the full code:

#pragma once

using namespace System;
using namespace System::Reflection;
using namespace RobinHoodLibrary;

namespace ClassLibrary1 {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
        RobinHood^ robin = gcnew RobinHood();

        public: int Add(int Number1, int Number2)
        {
            return robin->Add(Number1, Number2);
        }

        public: System::Void Test()
        {
            robin->Test();
        }

        public: int Test1(int i)
        {
            return robin->Test1(i);
        }

        public: System::Void Test2(String^ txt)
        {
            robin->Test2(txt);
        }

    };
}

extern __declspec(dllexport) int Add(int Number1, int Number2) {
    ClassLibrary1::Class1 c;
    return c.Add(Number1, Number2);
}

extern __declspec(dllexport) void Test() {
    ClassLibrary1::Class1 c;
    c.Test();
    return;
}

extern __declspec(dllexport) int Test1(int i) {
    ClassLibrary1::Class1 c;
    return c.Test1(i);
}

extern __declspec(dllexport) System::Void Test2(String^ txt) {
    ClassLibrary1::Class1 c;
    c.Test2(txt);   
}

I can easy export Add, Test and Test1 method but not Test2.

How can I to fix it ?

Thanks to support


回答1:


AFAIK You can't export methods which have C++/CLI types in arguments or in return value. So you have to use const wchar_t* or std::wstring as parameter instead of String^.




回答2:


try with gcroot<> in function Test2 like this:

System::Void Test2(gcroot<String^> txt);


来源:https://stackoverflow.com/questions/54708505/c-cli-export-void-return-declspecdllexport-cannot-be-applied-to-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!