Interop between C++ and C#

This is a discussion on Interop between C++ and C# within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been looking high and low for information about this and haven't had any luck yet. I am ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Interop between C++ and C#

    Hello,

    I have been looking high and low for information about this and haven't had any luck yet. I am creating an application in C++ that uses a C# DLL to connect to a database and return information back to the C++. Incidentally, I will need to send an instance of one of the classes i created in C++ to the C# as well.
    Is this possible, and if so, can someone give me some tips please?? it would be greatly appreciated!

    thanks in advance

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's definately possibly in .NET (in fact that was one of the primary goals of .NET), and since you're using C#, I assume you are in a .NET environment. All I can tell you is that it IS possible - I tried learning myself a while back but I gave up. Keep looking though.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    I have checked with everyone at my office and i've still had no luck! Of course, going the other way (Managed C++ wrapper for use in C# ) is easy...but the other way arnd...!?!?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I search on MSDN and found a wealth of information - including some demos between C# and VB - that should get you there.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Well, it's pretty much the same as using C++ classes in C#. I.e. you write Managed C++ code that calls the C# stuff, but in an unmanaged C++ class. You write an unmanaged wrapper class which contains managed code.

    The question I ask myself, though, is, why would you want to do that in the first place?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    ahhh...good question -- unfortunately those are just the requirements of the system and there's just no way around it! (believe me, i tried suggesting what you said -- i agree, it makes sense)

    an unmanaged wrapper? i'll read into it. I found this article:
    http://www.csharpfriends.com/Article...x?articleID=42
    and in it, it mentions the use of "tlbexp.exe" to create a type library file for the C# portion. I have found tlbexp.exe, but my question is very basic...does anyone know what exactly i'm supposed to do with it? how do i use it??

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Ah, yes. Visual C# has the option to create a COM wrapper DLL automatically. This allows you to use the C# classes like COM objects. I'm not sure, I think it also creates C/C++ header files for you to include.
    tlbexp.exe goes beyond this and creates a type library, which allows the C# classes to be used as an Automation object, that is, from the Windows Scripting Host.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Really!! how do i create the COM wrapper automatically? i can't seem to find any good advice about it on the web...

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    From what I see, it creates it automatically. The ClassID is the same as the class name. However, there's a project option you have to activate if you want the thing to be registered automatically, and believe me, you don't want to do that by hand.

    You need the C# sources for that, of course.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    I got it! I had to use the template provided by .Net (ComClass) which provides the interface outline for me and then tweak it a little to fit my functions. whew....now onto converting "_bstr_t" string into std::string ....

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21