473,388 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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.NullReferenceException'
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 ViewFinderCallBackFun(UInt32 pBuf, UInt32 Size, UInt32 Format,
UInt32 Context)
{
UInt32 err = cdError.cdOK;

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

// Get the BITMAPHEADER from pBuf
BITMAPHEADER frameHeader = new BITMAPHEADER();
frameHeader = (BITMAPHEADER)Marshal.PtrToStructure(pBuffer,
frameHeader.GetType());

// Create byte array (BITMAPHEADER + image data) and copy
byte[] VideoData = new byte[frameHeader.bmfHeader.bfSize];
Marshal.Copy(pBuffer, VideoData, 0,
(int)frameHeader.bmfHeader.bfSize);

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

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

BitmapData bmpData = frame.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);

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

frame.UnlockBits(bmpData);

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

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct BITMAPINFOHEADER
{
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(LayoutKind.Sequential, Pack=1)]
public struct BITMAPFILEHEADER // 14 bytes
{
public Int16 bfType; //"magic cookie" - must be "BM"
public Int32 bfSize;
public Int16 bfReserved1;
public Int16 bfReserved2;
public Int32 bfOffBits;
}

[StructLayout(LayoutKind.Sequential)]
public struct BITMAPHEADER
{
[MarshalAs(UnmanagedType.Struct, SizeConst=14)]
public BITMAPFILEHEADER bmfHeader;
[MarshalAs(UnmanagedType.Struct, SizeConst=40)]
public BITMAPINFOHEADER bmiHeader;
}

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

If anybody can help me ... please.

Regards

Michael
Nov 16 '05 #1
3 4895
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.


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.
Nov 16 '05 #2
Thanks a lot! That did the trick.
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.

Nov 16 '05 #3
wu***********@web.de (Michael) wrote in message news:<2d**************************@posting.google. com>...
Thanks a lot! That did the trick.
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.

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
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
8
by: Bill Yellin | last post by:
Greetings! Trying to get the CANON Camera SDK to work with .Net - works well in VB6, and Canon does say that they're not supporting .Net, but I've seen messages out here from people who are...
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Pradeep | last post by:
Hi all, Can any one explain me what is callback function.... I have written some code after reading some tutorials from internet... But I am not sure is it a right way to write a call back...
6
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: robin | last post by:
PROBLEM i lost my canon mp160 drivers for the printer and scanner. how to i download from the drivers for the printerI lost my start up disk for my canon mp180 all in one printer.How can I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.