473,378 Members | 1,436 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,378 software developers and data experts.

Problems using IntPtr and Interop services to get image from unmanaged code

Hi,
I was hoping someone could help me with the following: I'm writing
code to try and get a image from a USB camera mfg. dll.

Here's the dll function:
SDK_API MTUSB_GetCurrentFrame( DEV_HANDLE DevHandle, int FrameType,
unsigned char *Buffer );

Here's my C# wrapper:
public IntPtr GetCurrentFrame(FRAME_TYPE FrameType)
{
int FrameTyp = (int)FrameType;

if (MtxGetCurrentFrame(_devHandle, FrameTyp, _pImage) < 0)
{
MessageBox.Show("Error trying to get current frame
buffer. Frame grabber running?", _camError, MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
return _pImage;
}

which calls into
[DllImport("c:/Windows/system32/MT_USBCamera_SDK.dll",
CallingConvention = CallingConvention.Cdecl, EntryPoint =
"MTUSB_GetCurrentFrame")]
//private static extern int MtxGetCurrentFrame(uint DevHandle,
int FrameType, ref byte Buffer);
private static extern int MtxGetCurrentFrame(uint DevHandle,
int FrameType, IntPtr Buff);

Before calling I allocate memory for the IntPtr like this:
_pImage = Marshal.AllocHGlobal(pixelDepthBits * _maxX * _maxY);

After the dialog box closes I free the memory on the heap like this:
Marshal.FreeHGlobal(_pImage);

It's weird because if I do individual clicks to "snap" an image
everything works fine. An image is grabbed from the USB camera and
displayed in a PictureBox.
But if I do the same thing using a timer, the application will run
from 2 to 15 seconds before throwing the following error:
An unhandled exception of type 'System.AccessViolationException'
occurred in USBCam.exe

Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.

I'm using Visual Studio 2005, with the .NET 2.0 framework on Windows
XP SP2. Any idea on what I may be doing wrong. BTW, I get the same
error if I do an unsafe call using a byte array and the fixed keyword
to a byte pointer.

Please advise.
-Chucker

Sep 20 '07 #1
0 1405

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

Similar topics

3
by: Charles Law | last post by:
I have defined LVITEM as follows: <StructLayout(LayoutKind.Sequential)> Private Structure LVITEM Dim mask As Int16 Dim iItem As Int16 Dim iSubItem As Int16 Dim state As Int16 Dim stateMask As...
8
by: Johann Blake | last post by:
I need to call a C DLL function. The first parameter expects a pointer to a long. It returns a value at the address of the pointer. The second parameter expects a pointer to a pointer. It creates...
17
by: Sharon | last post by:
I Have a PC with dual XEON CPU’s and 4 Giga Byte RAM win Windows XP Pro. I have 2 problems with it: (1) Windows is showing only 3.25 Giga byte on the System Properties ? General tab. While the...
0
by: Andre Viens | last post by:
Hello, I am using the following variation of code from <http://support.microsoft.com/default.aspx?scid=kb;EN-US;319340> to add icons to an imagelist for use in a listview: Private Structure...
10
by: Christopher Kurtis Koeber | last post by:
Dear all, I would like to convert a system.drawing.image or bitmap to a FreeImage bitmap. I am using the code below. NOTE: the problem I am having doesn't seem to be with the FreeImage Library...
2
by: Bart Simpson | last post by:
I have written a library in ('native') C++, and I have made a lot of use of function pointers e.g.: /* Example C++ callbacks: typedef void (*CBFUNC1)(unsigned int, const char*, object&);...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
7
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I'm trying to use the DirectorySearcher.FindOne() method to get the display name of the current user. The code runs on a server behind a web service. Everything works fine when I run it on my...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.