CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: using c++ to instantiate a c# object?

  1. #1
    Join Date
    Oct 2008
    Posts
    6

    using c++ to instantiate a c# object?

    Hi Everyone

    I'm wondering how to instantiate a c# object and make a simple method call on the object using c++. I don't need any complex interactions or anything. All I want to be able to do is the following (written in pseudo c++):

    Code:
      C#Object obj;  //instantiate the object
      obj.getStringFromCPlusPlus("heheheheh");  /* pass a string from my c++ to the instantiated c# object, and the c# object will do the work from there */
    I have googled around for some of this and it all seems really complicated, but then again the examples they give are more complex than this one. I really don't know where to start with this. Thanks for any help you can give me.

    Thanks

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019

    Re: using c++ to instantiate a c# object?

    Why do you need to do this? Why not either write everything in C# or everything in C++?

    I'd imagine you can only do this in managed C++. Are you trying it in managed or unmanaged C++?
    Succinct is verbose for terse

  3. #3
    Join Date
    Oct 2008
    Posts
    6

    Red face Re: using c++ to instantiate a c# object?

    Ok, here's the situation. I have some code in c++ that does what I need it to, and I don't want to rewrite it in another language as it is complicated (it does dll injection to subclass a non-standard windows control so that it can intercept messages to that window). I'm writing a program to track my statistics and the statistics of those that I'm playing with in online poker (similar to other programs like PokerHUD) in real time. I have almost no experience in c++, and a fair amount of Java/c# experience, so it would be a lot easier for me to pass the messages that are captured in c++ to c# or java for parsing. Basically I just want to pass string messages between two programs that were written in different languages (so there's probably better ways to do it than instantiating objects? - sorry, I'm not a great programmer :|) I just really don't want to do everything in c++ because it will make the project less enjoyable as I will have to constantly look things up online that I don't know about .

    As I have almost no c++ experience, I'm not sure about managed vs. unmanaged. I'm working in visual studio 2008, and there are no __gc declarations in the code (this means no managed types?). Is there a way to check if a project is managed/unmanaged?

    edit: I suppose in the worst case I could just write the messages out to a file and then read them in from my program in another language.. How much slower do you think that would be comparatively?

    edit2: If it might be easier to instantiate a c++ object in c#, I could probably do it that way as well. Also, if it is easier to work between c++ and Java (I'm not sure), I could do that as well.

    Thanks
    Last edited by nightmares; October 14th, 2008 at 10:41 AM.

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019

    Re: using c++ to instantiate a c# object?

    Have a look at http://www.codeproject.com/KB/cs/unm...tomanaged.aspx for starters.

    Normally you need a whole load of interface wrappers which are just one liners and quite tedious to write but this article provides an alternative.
    Succinct is verbose for terse

  5. #5
    Join Date
    Oct 2008
    Posts
    6

    Re: using c++ to instantiate a c# object?

    Thanks for your help. I'm sure this way would work, however I found an alternate method, using WM_COPYDATA to send the strings between the c++ and c#. http://www.codeproject.com/KB/cs/wm_copydata_use.aspx While this doesn't address my problem as I had initially intended to (see OP), it works.

    Thanks again

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: using c++ to instantiate a c# object?

    Quote Originally Posted by cup
    Why do you need to do this? Why not either write everything in C# or everything in C++?

    I'd imagine you can only do this in managed C++. Are you trying it in managed or unmanaged C++?
    "Managed C++" is completely obsolete [it existed only with V1 of .NET] the proper technology is C++/CLI. They are two totally different things.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: using c++ to instantiate a c# object?


  8. #8
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019

    Re: using c++ to instantiate a c# object?

    Guess I'm about 5 years behind - comes from maintaining legacy code where things still have to be done the old fashioned, clunky way.
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)