Calling managed VC++ from C#

A

Andy

Hi all, sorry for the cross group spam but my question does really
fall into both groups. Here's my issue.

I have a Visual C++, CLR Class Library with a public class and a
public method in a solution. Also in the solution is a c# console app.
I have referenced the c++ project from the c# project and can
instanciate the vc++ class, however I cannot see my vc++ method from
my c# project.

As I understood it, as this was all managed code, that was all I
needed to do....

Anyone got any hints as to what's gone wrong?

Thanks
Andy
 
B

Ben Voigt [C++ MVP]

Andy said:
Hi all, sorry for the cross group spam but my question does really
fall into both groups. Here's my issue.

I have a Visual C++, CLR Class Library with a public class and a
public method in a solution. Also in the solution is a c# console app.
I have referenced the c++ project from the c# project and can
instanciate the vc++ class, however I cannot see my vc++ method from
my c# project.

As I understood it, as this was all managed code, that was all I
needed to do....

Anyone got any hints as to what's gone wrong?

Which version of C++? 2002, 2003, or 2005? 2005's support for .NET is
called C++/CLI, not managed VC++. The older Managed Extensions for C++ are
buggy (but not causing your problem).

I suspect you forgot to declare the access for your member function. Try
this:

public ref class MyClass
{
public: // <- this is important
void CallMe(void);
};

or

public ref struct MyClass // still a C# "class", not "struct". For C#
"struct" you'd use "value class" or "value struct"
{
// in a C++ struct, things are public by default
void CallMe(void);
};
 
A

Andy

Which version of C++? 2002, 2003, or 2005? 2005's support for .NET is
called C++/CLI, not managed VC++. The older Managed Extensions for C++ are
buggy (but not causing your problem).

I suspect you forgot to declare the access for your member function. Try
this:

public ref class MyClass
{
public: // <- this is important
void CallMe(void);

};

or

public ref struct MyClass // still a C# "class", not "struct". For C#
"struct" you'd use "value class" or "value struct"
{
// in a C++ struct, things are public by default
void CallMe(void);



};


- Show quoted text -

Hi Ben and thanks for replying.

Sorry, you are right I am using VS 2005 and C++/CLI and after much
messing about I managed to get it to work. Basically I removed the
header file for my cpp class definition and bingo, it all worked. Not
sure why and the old school c/c++ guy that sits behind be said that
removing the .h was a bad idea and it shouldn't work...

To my next problem..... My C++/CLI code is trying to return a type
defined in C# but I get a complilation exception
error C2440 'return' : cannot convert from '<type>' to '<type>'

where type is the same thing! :-(

Andy
 
B

Ben Voigt [C++ MVP]

Andy said:
Hi Ben and thanks for replying.

Sorry, you are right I am using VS 2005 and C++/CLI and after much
messing about I managed to get it to work. Basically I removed the
header file for my cpp class definition and bingo, it all worked. Not
sure why and the old school c/c++ guy that sits behind be said that
removing the .h was a bad idea and it shouldn't work...

To my next problem..... My C++/CLI code is trying to return a type
defined in C# but I get a complilation exception
error C2440 'return' : cannot convert from '<type>' to '<type>'

where type is the same thing! :-(

Is it a C# struct or class?

If a class (reference type), make sure you haven't forgotten the ^ (meaning
tracking handle).
 
B

Ben Voigt [C++ MVP]

Sorry, you are right I am using VS 2005 and C++/CLI and after much
messing about I managed to get it to work. Basically I removed the
header file for my cpp class definition and bingo, it all worked. Not
sure why and the old school c/c++ guy that sits behind be said that
removing the .h was a bad idea and it shouldn't work...

And just to mention, I've got a lot of code with ref classes declared in
headers and member functions defined in implementation files, so that should
work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top