Canon SDK - callback function

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

    Canon SDK - callback function

    Hi developers,

    I downloaded the Canon SDK Version 7.1 and tried to migrate the
    "RELCTRL" sample to C# (with PInvoke). Almost everything is working
    except the following callback function that should receive the image
    data from the camera. I´m always getting the same error:

    An unhandled exception of type 'System.NullRef erenceException '
    occurred in Unknown Module.

    Additional information: Object reference not set to an instance of an
    object.

    This error shows up after the callback functions has been called 6
    times. Unfortunatley I don´t know why this happens and how I can solve
    the problem.

    =============== =============== =============== =============== =============== ====

    /* The function which receives the picture from a camera */
    UInt32 ViewFinderCallB ackFun(UInt32 pBuf, UInt32 Size, UInt32 Format,
    UInt32 Context)
    {
    UInt32 err = cdError.cdOK;

    if ( Format == cdType.FILEFORM AT_BMP )
    {
    IntPtr pBuffer = new IntPtr(pBuf);

    // Get the BITMAPHEADER from pBuf
    BITMAPHEADER frameHeader = new BITMAPHEADER();
    frameHeader = (BITMAPHEADER)M arshal.PtrToStr ucture(pBuffer,
    frameHeader.Get Type());

    // Create byte array (BITMAPHEADER + image data) and copy
    byte[] VideoData = new byte[frameHeader.bmf Header.bfSize];
    Marshal.Copy(pB uffer, VideoData, 0,
    (int)frameHeade r.bmfHeader.bfS ize);

    // Get image size
    int width = frameHeader.bmi Header.biWidth;
    int height = frameHeader.bmi Header.biHeight ;

    // Create new Bitmap
    Bitmap frame = new Bitmap(width, height);

    BitmapData bmpData = frame.LockBits(
    new Rectangle(0, 0, width, height),
    ImageLockMode.R eadWrite,
    PixelFormat.For mat24bppRgb);

    // Copy image data from memory to Scan0 of our Bitmap
    // Start at index 54 because of BITMAPHEADER
    for (int i = 54; i < VideoData.Lengt h; i++)
    {
    Marshal.WriteBy te(bmpData.Scan 0, i-54, VideoData);
    }

    frame.UnlockBit s(bmpData);

    // Create delegate
    if (this.RecievedF rame != null)
    {
    this.RecievedFr ame(this, new StreamEventArgs (frame));
    }
    }
    return err;
    }

    [StructLayout(La youtKind.Sequen tial, Pack=1)]
    public struct BITMAPINFOHEADE R
    {
    public UInt32 biSize;
    public Int32 biWidth;
    public Int32 biHeight;
    public Int16 biPlanes;
    public Int16 biBitCount;
    public UInt32 biCompression;
    public UInt32 biSizeImage;
    public Int32 biXPelsPerMeter ;
    public Int32 biYPelsPerMeter ;
    public UInt32 biClrUsed;
    public UInt32 biClrImportant;
    }

    [StructLayout(La youtKind.Sequen tial, Pack=1)]
    public struct BITMAPFILEHEADE R // 14 bytes
    {
    public Int16 bfType; //"magic cookie" - must be "BM"
    public Int32 bfSize;
    public Int16 bfReserved1;
    public Int16 bfReserved2;
    public Int32 bfOffBits;
    }

    [StructLayout(La youtKind.Sequen tial)]
    public struct BITMAPHEADER
    {
    [MarshalAs(Unman agedType.Struct , SizeConst=14)]
    public BITMAPFILEHEADE R bmfHeader;
    [MarshalAs(Unman agedType.Struct , SizeConst=40)]
    public BITMAPINFOHEADE R bmiHeader;
    }

    =============== =============== =============== =============== =============== ====

    If anybody can help me ... please.

    Regards

    Michael
  • Mattias Sjögren

    #2
    Re: Canon SDK - callback function

    [color=blue]
    >This error shows up after the callback functions has been called 6
    >times. Unfortunatley I don´t know why this happens and how I can solve
    >the problem.[/color]

    This type of error is often caused by garbage collection of the
    callback delegate you pass to native code. You must keep a reference
    to it as long as its needed to ensure it stays alive.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Michael

      #3
      Re: Canon SDK - callback function

      Thanks a lot! That did the trick.
      [color=blue]
      > This type of error is often caused by garbage collection of the
      > callback delegate you pass to native code. You must keep a reference
      > to it as long as its needed to ensure it stays alive.[/color]

      Comment

      • Bjoern M. Albrecht

        #4
        Re: Canon SDK - callback function

        wurst.michael@w eb.de (Michael) wrote in message news:<2ddb19ed. 0503030108.437c fc88@posting.go ogle.com>...[color=blue]
        > Thanks a lot! That did the trick.
        >[color=green]
        > > This type of error is often caused by garbage collection of the
        > > callback delegate you pass to native code. You must keep a reference
        > > to it as long as its needed to ensure it stays alive.[/color][/color]


        Hi there,

        Can you please explain your solution in some details - I think, i have
        similar problems with some other sdk .

        Thank you,
        Bjoern M. Albrecht

        Comment

        Working...