none
P\Invoke this type argument const BYTE * const * to C# type argument RRS feed

  • Question

  • I have a C++ function api:

     

    int FpRecEngGeneralize(INT count, const BYTE * const * gen_features, BYTE * features, DWORD * size, HVFCONTEXT context);

     

    I have try to map using P\Invoke in C# like following:

     

    public unsafe int GeneralizeFeatures(byte[] featureSet, byte[] result , ref int resultCode)
            {
                result = new FingerCellFeature();

                if (featureSet == null || featureSet.Length == 0)
                    return Consts.VFE_ARGUMENT_NULL;

                if (!InitializeFingerCell())
                    return Consts.VFE_NOT_INITIALIZED;
               
                try
                {
                    //IntPtr size = new IntPtr(0);
                    int size = Consts.VF_MAX_FEATURES_SIZE;
                    fixed (byte* f = &(result)[0], fs = &featureSet[0])
                    {
                        return FpRecEngGeneralize(
                            3,
                            fs,
                            f,
                            ref size,
                            m_fcContext);
                    }
                }
                catch (Exception e)
                {
                    //m_lastError = e.Message;
                    resultCode = Consts.INTERNAL_EXCEPTION;
                    throw new Exception("Error " + e.Message);
                }
            }

     

    [DllImport(path, EntryPoint = "FpRecEngGeneralize", CallingConvention = CallingConvention.StdCall)]
            private static extern unsafe int FpRecEngGeneralize(
                int count,
                byte* gen_features,
                byte* features,
                ref int size,
                IntPtr context);

    But when I debug the dll: there is an exception <Bad Ptr> as following picture:

     

     

    So the problem here I think I do not map datatype const BYTE * const  in C# properly so could anyone guide me how to map it correctly in C# featureset is list of 3 extracted Finger Print Format as byte array.

     

    Thanks,

    Regards,

    Vic

     

     

     

    • Moved by Andrew.Wu Wednesday, October 12, 2011 8:49 AM (From:Visual C# General)
    Monday, October 10, 2011 1:55 PM

Answers

  • I was confused by the placement of C++ 'const' keyword. The arrays' sizes cannot change, so you can safely use arrays in your declaration:

    private static extern unsafe int FpRecEngGeneralize(
            int count,
            ref byte[] gen_features,
            byte[] features,
            ref int size,
            IntPtr context);
    

     

    • Proposed as answer by Paul Zhou Thursday, October 13, 2011 3:12 AM
    • Marked as answer by Paul Zhou Tuesday, October 18, 2011 5:48 AM
    Monday, October 10, 2011 3:11 PM

All replies

  • What is expected is a byte**.

    Depending on what is returned by the 'size' parameter, you should be able to have simpler code using the arrays directly.

    Monday, October 10, 2011 2:42 PM
  • I was confused by the placement of C++ 'const' keyword. The arrays' sizes cannot change, so you can safely use arrays in your declaration:

    private static extern unsafe int FpRecEngGeneralize(
            int count,
            ref byte[] gen_features,
            byte[] features,
            ref int size,
            IntPtr context);
    

     

    • Proposed as answer by Paul Zhou Thursday, October 13, 2011 3:12 AM
    • Marked as answer by Paul Zhou Tuesday, October 18, 2011 5:48 AM
    Monday, October 10, 2011 3:11 PM
  • Hi Vic,

    I'm going to help move your post to the Common Language Runtime forum for better support.

    Thanks for your understanding.

    Best Regards,


    Andrew Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 12, 2011 8:49 AM