none
How to write a VC++ Wrapper class for C#

    Question

  • Hi, i have a VC++ application which has only one EXE, i wanted to write a C# application which has EXE & DLL's, now i wanted to call function's in VC++ from C#, since my VC++ application is an EXE, is it possible to write a C++ wrapper class and then call the functions appropriatly ? if so how to do that ?

    I have tried in another of option of creating an Invoker function and passing the function pointer of a VC++ application,but i am failing in that, can anyone guide me in this approach ?

    Does it make sense to create another C++ DLL and then make it connection bridge b/w both VC++ exe and C# dll ?

     Is there any way that i can pass some messages to VC++ exe and do the process ?

    Thanks

    Ravi


    Working as a software engineer
    Monday, April 26, 2010 5:59 AM

Answers

  • Hello rsodimba,

     

    1. I suggest that you define a COM interface and implement the interface in your VC++ EXE source codes.

    1.1 This COM interface provides methods that are to be called by the C# program.

    1.2 The implementation of this interface (in your VC++ EXE) simply forwards calls to the original VC++ functions that you want exposed to the C# application. You can look upon the implementation codes as wrappers for the low-level functions that actually perform the required tasks.

    1.3 The C# application accesses these methods by first instantiating the COM interface and this is achieved by COM interop.

     

    2. There are many articles on COM interop available on the web and some good ones can be found in CodeProject, e.g. :

    2.1 Understanding Classic COM Interoperability With .NET Applications

    http://www.codeproject.com/KB/COM/cominterop.aspx

    2.2 .NET - COM Interoperability

    http://www.codeproject.com/KB/COM/COM_DOTNET_INTEROP.aspx

     

    Best of luck, rsodimba,

    - Bio.

    Monday, April 26, 2010 7:33 AM

All replies

  • Hello rsodimba,

     

    1. I suggest that you define a COM interface and implement the interface in your VC++ EXE source codes.

    1.1 This COM interface provides methods that are to be called by the C# program.

    1.2 The implementation of this interface (in your VC++ EXE) simply forwards calls to the original VC++ functions that you want exposed to the C# application. You can look upon the implementation codes as wrappers for the low-level functions that actually perform the required tasks.

    1.3 The C# application accesses these methods by first instantiating the COM interface and this is achieved by COM interop.

     

    2. There are many articles on COM interop available on the web and some good ones can be found in CodeProject, e.g. :

    2.1 Understanding Classic COM Interoperability With .NET Applications

    http://www.codeproject.com/KB/COM/cominterop.aspx

    2.2 .NET - COM Interoperability

    http://www.codeproject.com/KB/COM/COM_DOTNET_INTEROP.aspx

     

    Best of luck, rsodimba,

    - Bio.

    Monday, April 26, 2010 7:33 AM
  • Hello Ravi

    Here is my suggestion use pInvoke option, and tell us where you are failing in this option?

    I used this approach in my C++ and C# application and its working fine for me

     

    //C# code
    
    
        [DllImport("MyDll.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U1)] 
        public static extern bool MyFunction(string strpass, StringBuilder strHash);
    //MyFunction is exist in dll
     

     

     


    मनोज कुमार जांगिड
    • Proposed as answer by Jangid Monday, April 26, 2010 10:42 AM
    Monday, April 26, 2010 10:42 AM