Mixing managed/unmanaged in C++/CLI
-
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;
-
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;
Navaneeth How to use google | Ask smart questions
-
Navaneeth How to use google | Ask smart questions
The thing that confuses me is that &m_pFormatContext should be an unmanaged pointer, therefore AFAIU shouldn't need pinning?
using System.Beer;
-
The thing that confuses me is that &m_pFormatContext should be an unmanaged pointer, therefore AFAIU shouldn't need pinning?
using System.Beer;
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
-
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;
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