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

Thread: Import MFC DLL CLASS IN C#

  1. #1
    Join Date
    Oct 2003
    Location
    India , Faridabad
    Posts
    27

    Import MFC DLL CLASS IN C#

    Hi friends....

    I have a problem ,the code snippet is given below.
    I have a mfc shared memrory dll class.I want to instanciate an object of this class in C# .Can you provide some code sample to achieve it.

    #ifdef _FtpChannel

    #define _EXPORT_IMPORT_FtpChannel __declspec(dllexport)

    #else

    #define _EXPORT_IMPORT_FtpChannel __declspec(dllimport)

    #endif



    class _EXPORT_IMPORT_FtpChannel CFtpMessage : public IConnection

    {

    private:

    public:

    CFtpMessage();

    ~CFtpMessage();

    }
    Regards :

    Sudhir Mangla

    http://Programmerworld.net
    (Free books , articles and Source Code) :
    http://tips.DevelopersVoice.com
    (Technical Articles ) :
    http://http://Health.Programmerworld.net
    (Health tips )
    http://http://Faq.Programmerworld.net
    (Technical Faqs )
    thumb:

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Import MFC DLL CLASS IN C#

    You need to do the following two steps:
    1. Create a wrapper in C++ Unmanaged code from inside VS .NET environment.
    2. Create a wrapper in C++ managed code for the wrapper from step 1.

    When I say "wrapper" I mean a class that has the same interface of the original class, and implements all functions simply by calling the corresponding functions from the original class. It is obvious that you can change the interface if you really want to, but for the beginner of us we will keep the same interface.

    Lets say the original class looks like this:

    class __declspec(dllexport) MyUnmanagedVC6Class{
    public:
    MyUnmanagedVC6Class(int);
    void DoSomething(int,double);
    }

    and it is located in MyUnmanagedVC6Header.h file, which I have.

    Now we open a blank solution, and add a new C++ MFC Class Library Project. Lets call it MyUnmanagedDotNetVCWrapper.
    We will implemenet a wrapper for MyUnmanagedVC6Class:
    #include "MyUnmanagedVC6Header.h"

    class __declspec(dllexport) MyUnmanagedDotNetClass{
    private:
    MyUnmanagedVC6Class* _class;
    public:
    MyUnmanagedDotNetClass(int x)
    {
    _class = new MyUnmanagedVC6Class(x);
    }

    void DoSomething(int x,double d)
    {
    _class->DoSomething(x,d);
    }
    }

    This should be located in MyUnmanagedDotNetHeader.h.
    Notice: If you have all the sources for the original dll you can open it in VisualStudio .NET, and it will convert it for you into a dll you can use for the later step. This can save you the time for the first wrapper.

    Now, we add to the solution (it has to be the same solution) a new project: MFC C++ project, again unmanaged. Lets call it MyManagedDotNetVCWrapper. Now we right click on the name of the new project, and we change the property "Use Manage Extensions" to "Yes". This causes it to be like a managed environment (not exactly the same, but for our purposes this is what we need).

    Now we add a reference to the dll we created in step 1: RightClick on References, and then choose "Add References". On the Projects Tab, choose the MyUnmanagedDotNetVCWrapper.dll .

    Now we add a new header to the project, called MyManagedDotNetHeader.h, with a class:

    #include "MyUnmanagedDotNetHeader.h"

    namespace MyWrapperNamespace
    {
    public __gc class MyManagedDotNetClass
    {
    private:
    MyUnmanagedDotNetClass __nogc * _class;
    public:
    MyManagedDotNetClass(int x)
    {
    _class = new MyUnmanagedDotNetClass(x);
    }
    void DoSomething(int x, double d)
    {
    _class->DoSomething(x,d);
    }
    };
    }

    Notice the __gc (Garbage Collection) flag on the managed class, and the __nogc flag on the unmanaged pointer. Also notice the use of the namespace.

    After you build this, you can now use the MyManagedDotNetVCWrapper.dll in all your managed projects, like C# and VB.NET.

  3. #3
    Join Date
    Oct 2003
    Location
    India , Faridabad
    Posts
    27

    Re: Import MFC DLL CLASS IN C#

    Dear jhammer,

    I have already gone through such solution but there is one deflection :-

    #ifdef _FtpClient
    #define _EXPORT_IMPORT_CFtpClient __declspec(dllexport)
    #else
    #define _EXPORT_IMPORT_CFtpClient __declspec(dllimport)
    #endif

    class _EXPORT_IMPORT_CFtpClient CFtpClient
    {

    public:
    CFtpClient();
    ~CFtpClient();
    };
    Declared in "FtpClient.h"

    I have written a wrapper dll having managed extension set as true in property
    dailog i. e.

    #include "FtpClient.h"

    #ifdef _FtpChannel
    #define _EXPORT_IMPORT_FtpChannel __declspec(dllexport)
    #else
    #define _EXPORT_IMPORT_FtpChannel __declspec(dllimport)
    #endif
    class _EXPORT_IMPORT_FtpChannel CFtpMessage : public IConnection
    {
    private:
    CFtpClient* m_pFtpSever;

    public:
    CFtpMessage();
    ~CFtpMessage();
    };
    Declared in "FtpChannel.h"


    but deflection to write pure managed wrapper dll is that i have declared CFtpChannel in FtpChannel.h file
    but definition of all function is in "FtpChannel.cpp" file.

    Sp Please tell me how to write pure managed wrapper dll around CFtpChannel
    class.
    Regards :

    Sudhir Mangla

    http://Programmerworld.net
    (Free books , articles and Source Code) :
    http://tips.DevelopersVoice.com
    (Technical Articles ) :
    http://http://Health.Programmerworld.net
    (Health tips )
    http://http://Faq.Programmerworld.net
    (Technical Faqs )
    thumb:

  4. #4
    Join Date
    Apr 2005
    Location
    Juarez, México
    Posts
    30

    Smile Re: Import MFC DLL CLASS IN C#

    I also has a question, would the export dll thing work in the same way if otherwise, I mean from .NET to C++ ? Because this is exactly what I need, to create a copy of a class in another project, then from that another project create objects that can call the functions of the original class.
    Last edited by Hada; April 21st, 2005 at 05:12 PM.

  5. #5
    Join Date
    Nov 2005
    Posts
    162

    Thumbs up Re: Import MFC DLL CLASS IN C#

    Can you give me some samples on this?
    Many Thanks
    Cindy

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,102

    Re: Import MFC DLL CLASS IN C#

    IIRC, pinvoke only works with C-styled dlls and does not work to import C++ classes. So MFC classes are out.

  7. #7
    Join Date
    Jun 2019
    Posts
    1

    Re: Import MFC DLL CLASS IN C#

    Hi Sudhir,

    Did you solve the problem which you faced. If you solved could you please share the code here, it will be very much helpful for me

    Thanks in advance,
    Phani

    Quote Originally Posted by sudhirmangla View Post
    Hi friends....

    I have a problem ,the code snippet is given below.
    I have a mfc shared memrory dll class.I want to instanciate an object of this class in C# .Can you provide some code sample to achieve it.

    #ifdef _FtpChannel

    #define _EXPORT_IMPORT_FtpChannel __declspec(dllexport)

    #else

    #define _EXPORT_IMPORT_FtpChannel __declspec(dllimport)

    #endif



    class _EXPORT_IMPORT_FtpChannel CFtpMessage : public IConnection

    {

    private:

    public:

    CFtpMessage();

    ~CFtpMessage();

    }

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,102

    Re: Import MFC DLL CLASS IN C#

    See post #6

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)