Calling C++ function pointer from C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sasha

    #1

    Calling C++ function pointer from C#

    I have a c++ code that callls csharp. Now I want to be able to pass
    a function pointer from C++ to Csharp code and have c# callback to it.
    Is it possible and how?

    Here is what I have so far :
    #include "windows.h"
    #include <stdio.h>
    #import "CSDll.tlb" named_guids

    int main(int argc, char* argv[])
    {
    HRESULT hRes = S_OK;
    CoInitialize(NU LL);
    CSDll::IMyManag edInterface *pManagedInterf ace = NULL;

    hRes = CoCreateInstanc e(CSDll::CLSID_ Class1, NULL,
    CLSCTX_INPROC_S ERVER,
    CSDll::IID_IMyM anagedInterface , reinterpret_cas t<void**>
    (&pManagedInter face));

    if (S_OK == hRes)
    {
    hRes = pManagedInterfa ce->raw_Run();
    pManagedInterfa ce->Release();
    }

    CoUninitialize( );
    return 0;
    }
  • Ben Voigt [C++ MVP]

    #2
    Re: Calling C++ function pointer from C#

    sasha wrote:
    I have a c++ code that callls csharp. Now I want to be able to pass
    a function pointer from C++ to Csharp code and have c# callback to it.
    Is it possible and how?
    Do you have a special reason for using COM interop instead of C++ interop?
    With C++ interop, you can create a delegate in C++/CLI and pass it to the C#
    code.
    >
    Here is what I have so far :
    #include "windows.h"
    #include <stdio.h>
    #import "CSDll.tlb" named_guids
    >
    int main(int argc, char* argv[])
    {
    HRESULT hRes = S_OK;
    CoInitialize(NU LL);
    CSDll::IMyManag edInterface *pManagedInterf ace = NULL;
    >
    hRes = CoCreateInstanc e(CSDll::CLSID_ Class1, NULL,
    CLSCTX_INPROC_S ERVER,
    CSDll::IID_IMyM anagedInterface , reinterpret_cas t<void**>
    (&pManagedInter face));
    >
    if (S_OK == hRes)
    {
    hRes = pManagedInterfa ce->raw_Run();
    pManagedInterfa ce->Release();
    }
    >
    CoUninitialize( );
    return 0;
    }

    Comment

    • sasha

      #3
      Re: Calling C++ function pointer from C#

      On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
      sasha wrote:
      I have a c++ code that  callls csharp. Now I want to be able to pass
      a function pointer from C++ to Csharp code and have c# callback to it.
      Is it possible and how?
      >
      Do you have a special reason for using COM interop instead of C++ interop?
      With C++ interop, you can create a delegate in C++/CLI and pass it to theC#
      code.
      >
      >
      >
      Here is what I have so far :
      #include "windows.h"
      #include <stdio.h>
      #import "CSDll.tlb" named_guids
      >
      int main(int argc, char* argv[])
      {
         HRESULT hRes = S_OK;
         CoInitialize(NU LL);
         CSDll::IMyManag edInterface *pManagedInterf ace = NULL;
      >
         hRes = CoCreateInstanc e(CSDll::CLSID_ Class1, NULL,
      CLSCTX_INPROC_S ERVER,
          CSDll::IID_IMyM anagedInterface , reinterpret_cas t<void**>
      (&pManagedInter face));
      >
         if (S_OK == hRes)
         {
             hRes = pManagedInterfa ce->raw_Run();
             pManagedInterfa ce->Release();
         }
      >
          CoUninitialize( );
      return 0;
        }
      I have no reason, frankly speaking. How would I do that. please
      provide the reference to it. I have lots of legacy code, as a back-
      end, that heavily relies on function pointers. And I want to call
      those from my C# code. Any suggestion or examples of that?

      Thanks

      Comment

      • Shekhar

        #4
        Re: Calling C++ function pointer from C#

        You have 3 options:

        Using Explicit PInvoke in C++ (DllImport Attribute)

        Using C++ Interop (Implicit PInvoke)

        A Closer Look at Platform Invoke

        Read more here: http://msdn.microsoft.com/en-us/libr...82(VS.80).aspx


        "sasha" <aborovinsky@gm ail.comwrote in message
        news:cea35498-b502-4808-b5be-10492e572ba9@v1 6g2000prc.googl egroups.com...
        On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
        sasha wrote:
        I have a c++ code that callls csharp. Now I want to be able to pass
        a function pointer from C++ to Csharp code and have c# callback to it.
        Is it possible and how?
        >
        Do you have a special reason for using COM interop instead of C++ interop?
        With C++ interop, you can create a delegate in C++/CLI and pass it to the
        C#
        code.
        >
        >
        >
        Here is what I have so far :
        #include "windows.h"
        #include <stdio.h>
        #import "CSDll.tlb" named_guids
        >
        int main(int argc, char* argv[])
        {
        HRESULT hRes = S_OK;
        CoInitialize(NU LL);
        CSDll::IMyManag edInterface *pManagedInterf ace = NULL;
        >
        hRes = CoCreateInstanc e(CSDll::CLSID_ Class1, NULL,
        CLSCTX_INPROC_S ERVER,
        CSDll::IID_IMyM anagedInterface , reinterpret_cas t<void**>
        (&pManagedInter face));
        >
        if (S_OK == hRes)
        {
        hRes = pManagedInterfa ce->raw_Run();
        pManagedInterfa ce->Release();
        }
        >
        CoUninitialize( );
        return 0;
        }
        I have no reason, frankly speaking. How would I do that. please
        provide the reference to it. I have lots of legacy code, as a back-
        end, that heavily relies on function pointers. And I want to call
        those from my C# code. Any suggestion or examples of that?

        Thanks

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: Calling C++ function pointer from C#

          sasha wrote:
          On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
          >sasha wrote:
          >>I have a c++ code that callls csharp. Now I want to be able to pass
          >>a function pointer from C++ to Csharp code and have c# callback to
          >>it. Is it possible and how?
          >>
          >Do you have a special reason for using COM interop instead of C++
          >interop? With C++ interop, you can create a delegate in C++/CLI and
          >pass it to the C# code.
          >>
          [snip]
          I have no reason, frankly speaking. How would I do that. please
          provide the reference to it. I have lots of legacy code, as a back-
          end, that heavily relies on function pointers. And I want to call
          those from my C# code. Any suggestion or examples of that?
          >
          Thanks
          Well that's actually a slightly different problem. There's
          System::Runtime ::InteropServic es::Marshal::Ge tFunctionPointe rForDelegate
          that helps with that in p/invoke. And it seems there's
          GetDelegateForF unctionPointer for the case you originally mentioned.

          For C++ interop, you don't have to do anything special at all. You just
          compile your code with C++/CLI and call your legacy code normally. If you
          want a C++/CLI function to be usable by C#, put it into a "ref class".

          trivial example:

          #include <cmath>
          ref class Wrapper // ref class, C# can use it
          {
          public:
          static double ComputeIt()
          {
          return System::Math::S in(0.2) + std::sin(0.3); //
          see, it can both call C# and legacy C++ code in the same function
          }
          };


          Comment

          • sasha

            #6
            Re: Calling C++ function pointer from C#

            On Nov 19, 4:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
            sasha wrote:
            On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
            sasha wrote:
            >I have a c++ code that callls csharp. Now I want to be able to pass
            >a function pointer from C++ to Csharp code and have c# callback to
            >it. Is it possible and how?
            >
            Do you have a special reason for using COM interop instead of C++
            interop? With C++ interop, you can create a delegate in C++/CLI and
            pass it to the C# code.
            >
            [snip]
            I have no reason, frankly speaking. How would I do that. please
            provide the reference to it. I have lots of legacy code, as a back-
            end, that heavily relies on function pointers. And I want to call
            those from my C# code. Any suggestion or examples of that?
            >
            Thanks
            >
            Well that's actually a slightly different problem. There's
            System::Runtime ::InteropServic es::Marshal::Ge tFunctionPointe rForDelegate
            that helps with that in p/invoke. And it seems there's
            GetDelegateForF unctionPointer for the case you originally mentioned.
            >
            For C++ interop, you don't have to do anything special at all. You just
            compile your code with C++/CLI and call your legacy code normally. If you
            want a C++/CLI function to be usable by C#, put it into a "ref class".
            >
            trivial example:
            >
            #include <cmath>
            ref class Wrapper // ref class, C# can use it
            {
            public:
            static double ComputeIt()
            {
            return System::Math::S in(0.2) + std::sin(0.3); //
            see, it can both call C# and legacy C++ code in the same function
            }
            >
            };

            Wait, so from legacy code, I call managed c++ DLL, from the manage
            DLL I call Csharp DLL.

            is there an example of that as a starting point? thanks

            Comment

            • Ben Voigt [C++ MVP]

              #7
              Re: Calling C++ function pointer from C#

              sasha wrote:
              On Nov 19, 4:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
              >sasha wrote:
              >>On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spam>
              >>wrote:
              >>>sasha wrote:
              >>>>I have a c++ code that callls csharp. Now I want to be able to
              >>>>pass a function pointer from C++ to Csharp code and have c#
              >>>>callback to it. Is it possible and how?
              >>
              >>>Do you have a special reason for using COM interop instead of C++
              >>>interop? With C++ interop, you can create a delegate in C++/CLI and
              >>>pass it to the C# code.
              >>
              >[snip]
              >>I have no reason, frankly speaking. How would I do that. please
              >>provide the reference to it. I have lots of legacy code, as a back-
              >>end, that heavily relies on function pointers. And I want to call
              >>those from my C# code. Any suggestion or examples of that?
              >>
              >>Thanks
              >>
              >Well that's actually a slightly different problem. There's
              >System::Runtim e::InteropServi ces::Marshal::G etFunctionPoint erForDelegate
              >that helps with that in p/invoke. And it seems there's
              >GetDelegateFor FunctionPointer for the case you originally mentioned.
              >>
              >For C++ interop, you don't have to do anything special at all. You
              >just compile your code with C++/CLI and call your legacy code
              >normally. If you want a C++/CLI function to be usable by C#, put it
              >into a "ref class".
              >>
              >trivial example:
              >>
              >#include <cmath>
              >ref class Wrapper // ref class, C# can use it
              >{
              > public:
              > static double ComputeIt()
              > {
              > return System::Math::S in(0.2) + std::sin(0.3);
              >// see, it can both call C# and legacy C++ code in the same function
              > }
              >>
              >};
              >
              >
              Wait, so from legacy code, I call managed c++ DLL, from the manage
              DLL I call Csharp DLL.
              >
              is there an example of that as a starting point? thanks
              I just gave you an example. You need to make sure any managed libraries you
              need to call are added as references, native libraries you use header files
              and static libraries or import libraries as usual. There's nothing
              special... throw a call to qsort into that example and the C++/CLI compiler
              will automatically make the callback function pointer compatible with native
              qsort. C++ interop is also called "It Just Works" :)

              Unless I'm misunderstandin g your requirement. I think you want C# to call
              C++/CLI, C++/CLI passes callback function pointers to legacy C++ libraries,
              legacy C++ libraries calls through the function pointer back to C++/CLI
              which can call other C++/CLI, legacy C++, C#, etc.


              Comment

              Working...