0

i have a c++ dll (Cli) that is calling to methods in a c# dll.

the c# dll has the following method signature:

Class MyClass
{
   void DoSomeWork(ref ClassA a, ClassB b);
}

if there was no ref in the signature my code is something like this:

MyClass^ myClass = gcnew MyClass();
ClassA a = gcnew ClassA();
ClassB b = gcnew ClassB();
myClass->DoSomeWork(a, b);

how do i call it from the c++ code if there is a ref in the signature?

one more queastion i have - in c# i can call the Any() method on array but for some reason doing it the c++/cli is not working

if (reply->Any())

i get an error: error C2039: 'Any' : is not a member of 'System::Array'

any help would be appricated

thx

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Dardar
  • 624
  • 3
  • 13
  • 30
  • C++/CLI does use annotation at the call site, the code you'd use is identical. Any() is an extension method, not supported in C++/CLI. You have to call Enumerable::Any() explicitly. Ask only one question per post please. – Hans Passant Apr 02 '14 at 09:06

3 Answers3

0

If you compile your C++ DLL as a Managed Assembly then you should be able to access it just like you do with any C# assembly.

  • not really, since i can't just call it myClass->DoSomeWork(ref a, b); ref is not a valid syntax in c++/cli – Dardar Apr 02 '14 at 08:33
0

1a) You could use: myClass->DoSomeWork(*a, b);

1b) If ClassA is a class then ref is redundant anyway.

2) regarding if (reply->Any()) check whether you have in your source code: using namespace System::Linq;

rudolf_franek
  • 1,795
  • 3
  • 28
  • 41
0
  1. You do not need to do anything to pass ref parameters in C++/CLI. The method void DoSomeWork(ref ClassA a, ClassB b); in C# is seen as void DoSomeWork(ClassA^% a, ClassB^ b); in C++/CLI. Think of the % as equivalent to & in unmanaged C++: you don't need to add anything special to the method call to pass the parameter that way.
  2. Any() is an extension method, defined on Linq::Enumerable. See this answer for how to call extension methods in C++/CLI.
Community
  • 1
  • 1
David Yaw
  • 27,383
  • 4
  • 60
  • 93