Re: C# - Interop causing AccessViolationException

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



What happens if you make a change to:

C++ Wrapper
-----------------------------------------------
WRAPPER_API void find_objects_wrapped(void** rgb_cam_image, char
rgb_greybar)
{
// dereference the address of the rgb_cam_image ptr to obtain access to the
image bits array
unsigned char* ptrImage = (unsigned char*)*rgb_cam_image;
return find_objects(ptrImage, rgb_greybar);
}

C# code
-----------------------------------------------
[DllImport("Wrapper.dll", EntryPoint="find_objects_wrapped")]

public static extern void find_objects_wrapped([In,Out] ref IntPtr
rgb_cam_image, char rgb_greybar);

unsafe
{
// Get pointer to frame - using SharperCV wrapped for OpenCV
IntPtr cam_image = ImageFrame.getPixelAddr(0, 0, 0);

char rgb_greybar = (char)1;
// pass the address of the cam_image ptr to c++ wrapper
find_objects_wrapped(ref cam_image, rgb_greybar);
}

"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:AiXig.33923$I61.9343@xxxxxxxxxxx
thanks, but the result was the same.

in the function that works, one of the parameters is an empty byte array
that is passed by reference and populated with binary data in the C
function. that binary data is then used in the C# code to produce an
image.

in this new function, a pointer to an existing byte array is passed to the
function the byte array is then used within the function - presumably
reading this byte array is causing the error. but I don't know why.


"Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx> wrote in message
news:u7RX2WWjGHA.1276@xxxxxxxxxxxxxxxxxxxxxxx
Try this:

C++ Wrapper
-----------------------------------------------
WRAPPER_API void find_objects_wrapped(void* rgb_cam_image, char
rgb_greybar)
{
unsigned char* ptrImage = (unsigned char*)rgb_cam_image;
return find_objects(ptrImage, rgb_greybar);
}

C# code
-----------------------------------------------

[DllImport("Wrapper.dll", EntryPoint="find_objects_wrapped")]

public static extern void find_objects_wrapped([In] IntPtr rgb_cam_image,
char rgb_greybar);

unsafe
{
// Get pointer to frame - using SharperCV wrapped for OpenCV
IntPtr cam_image = ImageFrame.getPixelAddr(0, 0, 0);

char rgb_greybar = (char)1;

find_objects_wrapped(cam_image, rgb_greybar);
}


"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:wZKig.31805$I61.25270@xxxxxxxxxxx
thanks in advance...
while trying to pass an IntPtr into a wrapper DLL, I'm getting an
AccessViolationException: "Attemped to read or write from protected
memory"

I'm using another function in a similar way (except that one passes in
the pointer by ref) and that one is working fine (thanks to this group,
of course).
any ideas?


from the C header file:
-----------------------------------------------
void find_objects(unsigned char *rgb_cam_image, char rgb_greybar);


C++ Wrapper
-----------------------------------------------
WRAPPER_API void find_objects_wrapped(unsigned char* rgb_cam_image, char
rgb_greybar)
{
unsigned char* ptrImage = NULL;
ptrImage = (unsigned char*)GlobalLock(rgb_cam_image);
return find_objects(ptrImage, rgb_greybar);
}

C# code
-----------------------------------------------

[DllImport("Wrapper.dll", EntryPoint="find_objects_wrapped")]

public static extern void find_objects_wrapped([In] IntPtr
rgb_cam_image, char rgb_greybar);


unsafe
{
// Get pointer to frame - using SharperCV wrapped for OpenCV
IntPtr cam_image = ImageFrame.getPixelAddr(0, 0, 0);

char rgb_greybar = (char)1;

find_objects_wrapped(cam_image, rgb_greybar);
}







.



Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: C# - Interop causing AccessViolationException
    ... C++ Wrapper ... // Get pointer to frame - using SharperCV wrapped for OpenCV ... char rgb_greybar = 1; ... one of the parameters is an empty byte array ...
    (microsoft.public.dotnet.framework.interop)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)