Calling Manged code from Unmanged code.

This is a discussion on Calling Manged code from Unmanged code. within the C++ Programming forums, part of the General Programming Boards category; Hello all, I have been trying to call a function written in managed code, but it needs to be called ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Calling Manged code from Unmanged code.

    Hello all,

    I have been trying to call a function written in managed code, but it needs to be called from unmanaged code. Basically, I have a master thread within C++ code, that needs to update a .NET interface. The only way to do this is by calling a .NET function from within the master thread.

    I have wrote a very basic program to try and get this working, but with no success. I have a std c++ class and a .net manged class. I create a .net delegate to that manged function, and pass the delegate to my standard class. It is then called from the main program. However, I keep getting an error which I have not been able to resolve.

    If anyone could give me some suggestions as to where I am going wrong, that would be great. I have attached a zip file which demonstrates this, but with C# and a c++ dll. However, my interface needs to be in .net c++. You will have to rename the extension to .zip.

    Code:
    #include <iostream>
    #include <tchar.h>
    
    using namespace System;
    
    class StdClass
    { 
      public:
        typedef int (__stdcall *Callback)(int x, int y);
        
        StdClass(Callback ptr)
        {
          m_CallBack = ptr;
        }
        
        int Call(int x, int y)
        {
          return (*m_CallBack)(x, y);
        }
        
      private:
        Callback m_CallBack;
    };
    
    __gc class GcClass
    {
        public:
            int Call(int x, int y)
            {
          return x * y;
            }
    };
        
    __delegate int CallbackDelegate(int x, int y);
    
    int _tmain()
    {
      GcClass *gcClass = new GcClass();
      
      CallbackDelegate *callBack = new CallbackDelegate(gcClass, &GcClass::Call);
      
      StdClass *stdClass = new StdClass((StdClass::Callback)callBack);
      
      int iResult = stdClass->Call(3, 3);
    
      delete stdClass;
        return 0;
    }
    Attached Files Attached Files
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 03:04 AM

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