passing from unmanaged to managed code 
Author Message
 passing from unmanaged to managed code

I had an application developed in vc6 that I passed to vc++.net.
Now, I have unmanaged code, but I need to use managed code to interop with
other applications.

Does anybody know an easy way to pass from unmanaged to managed code? Is it
possible?

Thanks



Sun, 24 Jul 2005 19:40:55 GMT  
 passing from unmanaged to managed code
Quote:
> Does anybody know an easy way to pass from managed to managed code?
yes
> Is it possible?

yes


Sun, 24 Jul 2005 22:44:20 GMT  
 passing from unmanaged to managed code
Sorry for the bad joke.
Here is a way using a static interface.

#include <gcroot.h>
#include <string>
using namespace std;

#using <mscorlib.dll>
using namespace System;

#pragma managed

public __gc __interface ICallBacks {
    void Method1( System::String* );

Quote:
};

class MngInterface {
    static gcroot<ICallBacks*> callBacksRoot;
public:
    static void set_CallBacks( ICallBacks* callBacks ) {
        callBacksRoot = callBacks;
    }

    //add method wrappers here
    static void Method1( string* str ) {
        ICallBacks* callBacks = callBacksRoot;
        if( callBacks )
            callBacks->Method1( new String( str->c_str() ) );
    }

Quote:
};

gcroot<ICallBacks*> MngInterface::callBacksRoot;

public __gc class Interface {  //interface exposed to CLR
public:
    __property static void set_CallBacks( ICallBacks* callBacks ) {
        MngInterface::set_CallBacks( callBacks );
    }

Quote:
};

//test classes
#pragma unmanaged
class UmgTest {
public:
    static void Fn() {
        string str( "Hello World." );
        MngInterface::Method1( &str );
    }
Quote:
};

#pragma managed
public __gc class Test {
public:
    static void Fn() {
        UmgTest::Fn();
    }

Quote:
};

//C# test prog
using System;

namespace CsTest
{
    class App
    {
        class CallBacks :ICallBacks {
            public void Method1( String str ) {
                Console.WriteLine( "Method1 = {0}", str );
            }
        }

        [STAThread]
        static void Main(string[] args)
        {
            CallBacks callBacks = new CallBacks();
            Interface.CallBacks = callBacks;

            Test.Fn();

            Console.WriteLine( "Press Enter to stop" );
            Console.ReadLine();
        }

    }

Quote:
}



Sun, 24 Jul 2005 22:46:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing arrays from managed to unmanaged code

2. Referencing data from unmanaged code to managed code in C++ Wrapper class

3. calling managed code from unmanaged code

4. Calling managed code from unmanaged Code

5. How to pass a function pointer from Managed C++ to unmanaged c++

6. problem passing unmanaged pointer from one managed member function to another

7. passing array from managed to unmanaged ????

8. How to Pass Array from managed to unmanaged !!??!!

9. Debugging into managed c++ DLL that is being used by unmanaged code

10. Creating a .NET managed library to talk to existing unmanaged code

11. Managed c++ with unmanaged code Linking problem...

12. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

 

 
Powered by phpBB® Forum Software