简体   繁体   中英

DLLImport using C++ code in C# with struct & prototype

This is my first post in SO

Before I post my question in SO. I haved tried to using DLLImport to call an C++ method ( it's in an in DLL file named SDK.dll ) in C# . But i encountered a problem:

I have tried to use DLLImport to use this C++ code in C#.

RegisterVideoPreviewCB(HANDLE hChannel, PVOID pContext, VideoCaptureCB_Ptr pCB);

I can convert the HANDLE and PVOID except the VideoCaptureCB_Ptr. I know I don't have the definition of VideoCaptureCB_Ptr in C#.

I look back at the SDK.h file. I see this code

typedef void (CALLBACK * VideoCaptureCB_Ptr)(PVOID pContext, BYTE * apData[3],  VideoSampleInfo_T * pVSI);
typedef struct _VideoSampleInfo_T
{
    ULONG   idFormat; // 
    ULONG   lSignalState;
    int     nLen; // not used for raw video data(e.g. YUV420)
    int     nWidth;
    int     nHeight;
    int     anPitchs[3]; // only used for raw video data(e.g. YUV420)
    ULONG   dwMicrosecsPerFrame; // 1000*1000/FPS
    ULONG   field;
    int     iSerial;

} VideoSampleInfo_T;

I think I have to use DllImport to declare the _VideoSampleInfo_T and the VideoCaptureCB_Ptr before declare the RegisterVideoPreviewCB in C#.

So could anyone help me to use the DllImport or someway (such as re-declare VideoCaptureCB_Ptr ) to make an VideoCaptureCB_Ptr definition in C#. So I can pass it to parametter of RegisterVideoPreviewCB definition.

Thanks in advance.

--------------- Update Oh I realized I only need to convert below code to C#

 typedef void (CALLBACK * VideoCaptureCB_Ptr)(PVOID pContext, BYTE * apData[3], VideoSampleInfo_T * pVSI);
    typedef struct _VideoSampleInfo_T
    {
        ULONG   idFormat; // 
        ULONG   lSignalState;
        int     nLen; // not used for raw video data(e.g. YUV420)
        int     nWidth;
        int     nHeight;
        int     anPitchs[3]; // only used for raw video data(e.g. YUV420)
        ULONG   dwMicrosecsPerFrame; // 1000*1000/FPS
        ULONG   field;
        int     iSerial;

    } VideoSampleInfo_T;

Please help me to convert it to C#

We can achieve it by two ways depend on other concerns (such as time, effort, maintanace..):

  1. Port unmanaged code into managed code: As you saw, we can have similar type C# in stead of C++. We have delegation take place function pointer, struct/enum/class present typedef struct..However, this method take a lot of time for sure and seems that we made a double work and can not re-use unmanaged code as we want.

  2. Use tool to convert unmanaged code into managed: For instance, we have tool shipped with Windows SDK name tlpimp.exe ( http://clrinterop.codeplex.com/releases/view/17579 ). You can refer it and try to update some rule to adapt your situation. In general, we may need create some middle c# classes for complex type of unmanaged code type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM