Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Calling managed VC++ from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy - 09 Oct 2007 15:26 GMT
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
Ben Voigt [C++ MVP] - 09 Oct 2007 15:39 GMT
> Hi all, sorry for the cross group spam but my question does really
> fall into both groups. Here's my issue.
[quoted text clipped - 9 lines]
>
> 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);
};

> Thanks
> Andy
Andy - 11 Oct 2007 09:09 GMT
> > Hi all, sorry for the cross group spam but my question does really
> > fall into both groups. Here's my issue.
[quoted text clipped - 38 lines]
>
> - 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
Ben Voigt [C++ MVP] - 11 Oct 2007 17:20 GMT
>> > Hi all, sorry for the cross group spam but my question does really
>> > fall into both groups. Here's my issue.
[quoted text clipped - 53 lines]
>
> 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).

> Andy
Ben Voigt [C++ MVP] - 12 Oct 2007 17:40 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.