Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Mixing managed/unmanaged in C++/CLI

Mixing managed/unmanaged in C++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpcsharpasp-netdesign
5 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    I have a legacy C app which uses FFmpeg APIs. I'm trying to update it, I want to write a UI in C# and keep the core in C. So, i'm dabbling in C++/CLI for the first time. It's my understanding that I can create a managed class wrapper, and have managed methods which take managed parameters, convert them as necessary to unmanaged equivalents (with System.Runtime.InteropServices.Marshal methods), do the work in native C, and then convert outputs from unmanaged to managed types for return. However, I'm stuck at the start. I've created a class like this:-

    ref class FFmpegCap
    {
    public:
    FFmpegCap(String^ inputVideo);

    protected:
    AVFormatContext *m_pFormatContext;

    };

    In my constructor, i need to call a C FFmpeg function to initialise the library, one of the parameters it requires is of type AVFormatContext ** . So, I call this function as follows:-

        if (av\_open\_input\_file(&m\_pFormatContext, pInputVideo ,NULL,0,NULL) != 0)
    {
    	
    }
    

    However, this fails to compile; error C2664: 'av_open_input_file' : cannot convert parameter 1 from 'cli::interior_ptr' to 'AVFormatContext **' It seems that the & operator is not doing what I expect of it! Any pointers? Thanks Jon

    using System.Beer;

    N T 2 Replies Last reply
    0
    • J Jon Hulatt

      I have a legacy C app which uses FFmpeg APIs. I'm trying to update it, I want to write a UI in C# and keep the core in C. So, i'm dabbling in C++/CLI for the first time. It's my understanding that I can create a managed class wrapper, and have managed methods which take managed parameters, convert them as necessary to unmanaged equivalents (with System.Runtime.InteropServices.Marshal methods), do the work in native C, and then convert outputs from unmanaged to managed types for return. However, I'm stuck at the start. I've created a class like this:-

      ref class FFmpegCap
      {
      public:
      FFmpegCap(String^ inputVideo);

      protected:
      AVFormatContext *m_pFormatContext;

      };

      In my constructor, i need to call a C FFmpeg function to initialise the library, one of the parameters it requires is of type AVFormatContext ** . So, I call this function as follows:-

          if (av\_open\_input\_file(&m\_pFormatContext, pInputVideo ,NULL,0,NULL) != 0)
      {
      	
      }
      

      However, this fails to compile; error C2664: 'av_open_input_file' : cannot convert parameter 1 from 'cli::interior_ptr' to 'AVFormatContext **' It seems that the & operator is not doing what I expect of it! Any pointers? Thanks Jon

      using System.Beer;

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Read about pin_ptr[^]

      Navaneeth How to use google | Ask smart questions

      J 1 Reply Last reply
      0
      • N N a v a n e e t h

        Read about pin_ptr[^]

        Navaneeth How to use google | Ask smart questions

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        The thing that confuses me is that &m_pFormatContext should be an unmanaged pointer, therefore AFAIU shouldn't need pinning?

        using System.Beer;

        N 1 Reply Last reply
        0
        • J Jon Hulatt

          The thing that confuses me is that &m_pFormatContext should be an unmanaged pointer, therefore AFAIU shouldn't need pinning?

          using System.Beer;

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Your unmanaged pointer is inside a managed object. You need pin_ptr to prevent the unmanaged pointer pointing to different location during the garbage collection. Have you tried changing to pin_ptr? I believe that should solve the issue.

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          • J Jon Hulatt

            I have a legacy C app which uses FFmpeg APIs. I'm trying to update it, I want to write a UI in C# and keep the core in C. So, i'm dabbling in C++/CLI for the first time. It's my understanding that I can create a managed class wrapper, and have managed methods which take managed parameters, convert them as necessary to unmanaged equivalents (with System.Runtime.InteropServices.Marshal methods), do the work in native C, and then convert outputs from unmanaged to managed types for return. However, I'm stuck at the start. I've created a class like this:-

            ref class FFmpegCap
            {
            public:
            FFmpegCap(String^ inputVideo);

            protected:
            AVFormatContext *m_pFormatContext;

            };

            In my constructor, i need to call a C FFmpeg function to initialise the library, one of the parameters it requires is of type AVFormatContext ** . So, I call this function as follows:-

                if (av\_open\_input\_file(&m\_pFormatContext, pInputVideo ,NULL,0,NULL) != 0)
            {
            	
            }
            

            However, this fails to compile; error C2664: 'av_open_input_file' : cannot convert parameter 1 from 'cli::interior_ptr' to 'AVFormatContext **' It seems that the & operator is not doing what I expect of it! Any pointers? Thanks Jon

            using System.Beer;

            T Offline
            T Offline
            teejayem
            wrote on last edited by
            #5

            I had a similar issue. I read that you have to create the pointer locally in the function then assign it to a member variable afterwards something like this:

            IDiscMaster *dscMaster;
            hResult = CoCreateInstance(CLSID_MSDiscMasterObj, 0, CLSCTX_ALL, IID_IDiscMaster, &dscMaster);
            if (SUCCEEDED(hResult))
            m_pDiscMaster = (IDiscMaster *)dscMaster;
            else
            return false;

            Don't be overcome by evil, but overcome evil with good

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups