3

I am wondering if it is possible to pass my invoked C# object between c++ functions? I can already call my C# dlls from my native code, but now I need to pass a object between c++ functions which also means that I need to declare it in the header file...

When defining it in my header file I get the following error:

BOOL Exists(Api ^api);

Error   60  error C3395: 'ApiBase' : __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

Does anyone know how I should handle this in my header?

6
  • 2
    Just stop using __declspec(dllexport). Commented Nov 26, 2013 at 9:12
  • Why? What to do instead? Commented Nov 26, 2013 at 9:45
  • BOOL Exists(Api ^api); is intended to be a part of native dll's api? Commented Nov 26, 2013 at 9:58
  • @johny Yes it is. The problem is that I cant remove the dll export because I am also wrapping the c++ code in my tests which is written in C#. Sorry if you missunderstand me somehow. I'm not a c++ master Commented Nov 26, 2013 at 10:01
  • 2
    You can export a C++/CLI function but it can never be one that uses a managed type as its argument. Like Api^. That doesn't make sense because exported functions are always used by unmanaged code, such code would never now how to pass a managed object. Nor is it necessary at all, you can already use a public ref class from C#, just by adding a reference to the DLL. Commented Nov 26, 2013 at 10:57

1 Answer 1

3

You use __declspec(dllexport) to export a native C++ class. But it sounds like your class is a managed .net ref class. If you wish to export the functionality for both managed clients and unmanaged clients you need to declare two classes. One a managed ref class, and one a native class.

Or perhaps the issue is that you are trying to export a function that has managed parameters with __declspec(dllexport). Again that is not possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm ok. Maybe it's just easier and cleaner if I change this class that I need to pass between functions to a Singleton? That way I will have a global access point to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.