Click here to Skip to main content
15,887,350 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: charset encoding going from std::string to System::String and viceversa Pin
N a v a n e e t h15-Oct-09 22:27
N a v a n e e t h15-Oct-09 22:27 
QuestionManaging native struct returned from function called in managed wrapper Pin
alleyes14-Oct-09 8:37
professionalalleyes14-Oct-09 8:37 
AnswerRe: Managing native struct returned from function called in managed wrapper Pin
N a v a n e e t h15-Oct-09 4:40
N a v a n e e t h15-Oct-09 4:40 
GeneralRe: Managing native struct returned from function called in managed wrapper Pin
alleyes15-Oct-09 4:47
professionalalleyes15-Oct-09 4:47 
GeneralRe: Managing native struct returned from function called in managed wrapper Pin
N a v a n e e t h15-Oct-09 16:11
N a v a n e e t h15-Oct-09 16:11 
GeneralRe: Managing native struct returned from function called in managed wrapper Pin
alleyes16-Oct-09 1:21
professionalalleyes16-Oct-09 1:21 
QuestionHow to use C++ ATL-Template class in c# Pin
Chiman19-Oct-09 10:59
Chiman19-Oct-09 10:59 
AnswerRe: How to use C++ ATL-Template class in c# Pin
N a v a n e e t h9-Oct-09 18:42
N a v a n e e t h9-Oct-09 18:42 
There is no straightforward method to use template classes in other CLI languages like C#. This is because of the difference in how C++ templates and CLI generics works. C++ templates will be instantiated at compile time and CLI generics will be on runtime. C++/CLI also supports managed templates, but unfortunately it is not portable to other CLI languages.

If you want to use inheritance, you may need to create managed classes that calls the template class for each known type. Like MyBaseClassInt which use MyBaseClass<int> etc. This will not be the correct method as you will have class explosion when you have several types to be used.

Prefer composition over inheritance. In such case, this can be easily solved by providing a factory class which checks the type and instantiates respective template instance. Any new types can be easily added to the factory method without recompiling the whole application. We are taking the capability of C++/CLI to mix generic and template type parameters. To see how this can be done, consider the following code:
C++
// Your existing class - Keep it intact

template< class T >
class MyBaseClass:
     public IConnectionPointContainerImpl,
     public IPersistStorageImpl,
     public IPersistPropertyBagImpl,

{
public:
     void DoSomething();
};

// Define a managed generic interface that all CLI languages can use
// This interface should have all the methods that other languages need access
// Note that this uses generics which is supported by CLI

generic<typename T>
public interface class IManagedBase
{
public:
     void DoSomething();
};


// This is implementation for the above interface
// This uses managed template class and work with mixing generic type and template type

template<typename T>
ref class MyBaseClassWrapper : IManagedBase<T>
{
public:
     virtual void DoSomething()
     {
         // Using native class
         MyBaseClass<T> native;
         native.DoSomething();
     }
};

// Provide a simple builder class which takes care about instantiations and returns interface
// This will be used by other CLI languages

public ref class NativeObjectBuilder
{
public:
    // Add all the types you know here
    generic<typename T> virtual IManagedBase<T> GetBaseObject()
    {
         // typeid gives the compile time type
         if(T::typeid == int::typeid)
             return (IManagedBase<T>^) gcnew MyBaseClassWrapper<int>();
         else if(T::typeid == double::typeid)
             return (IManagedBase<T>^) gcnew MyBaseClassWrapper<double>();
    }
};
From C#, you can use it like
NativeObjectBuilder objectBuilder = new NativeObjectBuilder();
IManagedBase<int> ib = objectBuilder.GetBaseObject<int>();
ib.DoSomething(); // call goes to native ATL class!
This trick works well for primitive types. But for other complex types, you may need to write wrappers and use it.

Hope that helps Smile | :)

PS: I wrote this code directly on CP editor and you may get some minor syntax error.


modified on Saturday, October 10, 2009 1:03 AM

QuestionConvert System::String* str[] to const char* chars[] array! Pin
Cracked-Down9-Oct-09 0:34
Cracked-Down9-Oct-09 0:34 
AnswerRe: Convert System::String* str[] to const char* chars[] array! Pin
N a v a n e e t h9-Oct-09 18:50
N a v a n e e t h9-Oct-09 18:50 
GeneralRe: Convert System::String* str[] to const char* chars[] array! Pin
Cracked-Down10-Oct-09 2:06
Cracked-Down10-Oct-09 2:06 
GeneralRe: Convert System::String* str[] to const char* chars[] array! Pin
N a v a n e e t h10-Oct-09 6:10
N a v a n e e t h10-Oct-09 6:10 
GeneralRe: Convert System::String* str[] to const char* chars[] array! Pin
Cracked-Down10-Oct-09 8:23
Cracked-Down10-Oct-09 8:23 
GeneralRe: Convert System::String* str[] to const char* chars[] array! Pin
N a v a n e e t h10-Oct-09 17:58
N a v a n e e t h10-Oct-09 17:58 
GeneralRe: Convert System::String* str[] to const char* chars[] array! Pin
Cracked-Down10-Oct-09 20:40
Cracked-Down10-Oct-09 20:40 
QuestionFile handling with "fstream" Pin
Fenix27-Oct-09 23:27
Fenix27-Oct-09 23:27 
AnswerEither that or... Pin
Fenix27-Oct-09 23:57
Fenix27-Oct-09 23:57 
GeneralRe: Either that or... Pin
Richard MacCutchan8-Oct-09 2:15
mveRichard MacCutchan8-Oct-09 2:15 
GeneralRe: Either that or... Pin
Fenix28-Oct-09 4:01
Fenix28-Oct-09 4:01 
GeneralRe: Either that or... Pin
Richard MacCutchan8-Oct-09 4:13
mveRichard MacCutchan8-Oct-09 4:13 
GeneralRe: Either that or... Pin
Fenix28-Oct-09 16:24
Fenix28-Oct-09 16:24 
GeneralRe: Either that or... Pin
Fenix28-Oct-09 16:32
Fenix28-Oct-09 16:32 
GeneralRe: Either that or... Pin
Richard MacCutchan8-Oct-09 22:11
mveRichard MacCutchan8-Oct-09 22:11 
GeneralRe: Either that or... Pin
N a v a n e e t h8-Oct-09 16:54
N a v a n e e t h8-Oct-09 16:54 
GeneralRe: Either that or... Pin
Fenix29-Oct-09 14:17
Fenix29-Oct-09 14:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.