CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2016
    Posts
    4

    Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ library

    I started with example from older post:http://www.codeguru.com/cpp/cpp/cpp_...ET-Clients.htm.
    I need in my future development code something very similar with what I found in this posted code but to be able to compile in VS 2015. I think I was OK in bringing this code example up to day until the delegate definition. Noting helped from the Microsoft side when it was about delegates handled by this code example.
    Please help me bringing this code example in VS 2015. The problem I had was related to the Managed project part of the solution.
    Than you,
    Virginia

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    To make that 12 years old demo work under VS 2015, most of which is to be done is updating the old-syntax Managed C++ parts in the Managed.dll wrapper library project to the current C++/CLI syntax. Here are the four relevant source files in current C++/CLI:

    Code:
    // Managed.h
    
    #pragma once
    
    using namespace System;
    
    #include "..\Unmanaged\Unmanaged.h"
    
    class Feedback;
    
    namespace ManagedLib
    {
       public ref class ValueChangedEventArgs : public EventArgs
       {
       public:
         ValueChangedEventArgs(int i) : m_i(i)
         {}
    
       public:
         property int I { int get() { return m_i; } }
    
       private:
         int m_i;
       };
    
       public ref class Managed
       {
       public:
         Managed();
         ~Managed();
    
         void SetValue(int nValue);
    
         property int Value { int get(); }
    
         event EventHandler<ValueChangedEventArgs ^> ^ValueChanged;
    
       internal:
         void RaiseValueChanged(int i);
    
       private:
         CUnmanaged *m_pUnmanaged;
         Feedback *m_pFeedback;
       };
    }
    Code:
    // Managed.cpp
    
    #include "stdafx.h"
    
    #include "Managed.h"
    #include "Feedback.h"
    
    namespace ManagedLib
    {
       Managed::Managed()
       {
         m_pFeedback = new Feedback(this);
         m_pUnmanaged = new CUnmanaged(m_pFeedback);
       }
    
       Managed::~Managed()
       {
         delete m_pUnmanaged;
         delete m_pFeedback;
       }
    
       void Managed::SetValue(int nValue)
       {
         m_pUnmanaged->SetValue(nValue);
       }
    
       int Managed::Value::get()
       {
         return m_pUnmanaged->GetValue();
       }
    
       void Managed::RaiseValueChanged(int i)
       {
         ValueChanged(this, gcnew ValueChangedEventArgs(i));
       }
    }
    Code:
    // Feedback.h
    
    #pragma once
    
    #include <vcclr.h>
    
    #include "..\Unmanaged\Unmanaged.h"
    
    using namespace ManagedLib;
    
    class Feedback : public CUnmanaged::CFeedback
    {
    public:
         Feedback(Managed ^p);
    
         void OnValueChanged(int nValue);
    
    private:
         gcroot<Managed ^> m_pManaged;
    };
    Code:
    // Feedback.cpp
    
    #include "stdafx.h"
    
    #include "Managed.h"
    #include "Feedback.h"
    
    using namespace ManagedLib;
    
    Feedback::Feedback(Managed ^p)
    {
         m_pManaged = p;
    }
    
    void Feedback::OnValueChanged(int nValue)
    {
         m_pManaged->RaiseValueChanged(nValue);
    }
    Well, actually, the changes regarding event handling are a bit more than just syntactical translation, yet there's nothing unusual going on here. Any open questions that remain after studying the source code and the original article referred to, can easily be answered by MSDN documentation.

    I have attached an updated version of the demo project. It's not a port of the original project; instead it's a new one based on the updated original source code. (Setting up the project was non-trivial at a few points, so readers who try to recreate the project may encounter a few small difficulties. I'm not going to discuss this here, though, unless someone asks specific questions about that process, since it's rather off the topic of this thread.)
    Attached Files Attached Files
    Last edited by Eri523; April 13th, 2016 at 12:53 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Apr 2016
    Posts
    4

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    Thank you for this upgrade.
    As you mentioned it looks there is something special for setting up the project, it is not trivial and here I have the question: how to set the Managed project this time for VS 2015? I have a syntax error in line 14 of the Managed.h which also is signaled as C2243 error (typecast: conversion from Feedback to CUnmanaged:CFeedback exists, but is inaccessible)
    Best regards,
    Virginia

  4. #4
    Join Date
    Apr 2016
    Posts
    4

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    I am sorry about my earlier response, but I forgot to add in my Feedback.h a public CUnmanaged inheritance when I declared class Feedback.

    After this correction there are not syntax error, instead I have a good number of link errors type: unresolved token...., and unresolved external symbol.
    LNK2001,LNK2019,LNK2020,LNK2028,LNK1120,LNK4273.
    Any help is appreciated.
    Virginia

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    OK, first let's check if you used the correct project types for the DLL projects. For the Unmanaged project you need a Win32 Project (template path Templates/Visual C++/Win32/Win32 Project). In the Application Wizard that pops up while creating the project, you choose Application type DLL. For the Managed project you need a C++/CLI CLR Class Library project (Templates/Visual C++/CLR/Class Library).

    Also, set the third drop list ("Solution:") at the bottom of the New Project dialog to "Add to solution" for any project you create except the first one to get three projects contained within a single common solution. It would work with a separate solution for each project too, but that would be unnecessarily complicated, and you'd need to adjust the path to Unmanaged.h in Managed.h and Feedback.h and/or have two separate copies of Unmanaged.h in the Managed and Unmanaged projects. (The latter is not recommended if it can be avoided: Using more than one copy of the same .h file is a common source of annoying compilation errors/bugs in case you modify one of them and forget to adjust the other(s) accordingly.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    Oops! I just noticed that I simply forgot to upload the announced attachment containing my updated version of the demo project to post #2.

    Unfortunately, for obscure reasons, Codeguru currently is unreachable over my leased line ISP. I can reach it over 3G and a different ISP with my tablet (otherwise I couldn't write this post here... ), but it just turned out that CG's upload manager and my Android Opera refuse to cooperate, so I can't upload it right now either.

    For now, temporarily, here's a Dropbox link where you can download the zipped project rather than recreating it on your own: [EDIT: Link removed; file is now attached to post #2] Admins, I know links like this one aren't really appreciated here, but please tolerate it for a few days at most. I'll replace it with a real attachment upload as soon as I get hands on a Windows machine that's connected over a different ISP than the one of my leased line. Thanks.
    Last edited by Eri523; April 13th, 2016 at 12:55 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Apr 2016
    Posts
    4

    Resolved Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    Thank you very much for your support, I kindly appreciate it.
    Virginia

  8. #8

    Re: Delegate in C++ VS 2015 in managed class library for wrapping unmanaged C++ libra

    Thanks for this upgrade.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured