Re: C# - getting binary data from .lib

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I am sorry that was a typo. Use (unsigned char*) rather than (char*).

What you are trying to accomplish is to let the wrapper internally call the
library to allocate memory for the image when a null pointer is passed as an
argument.

The library should return the pointer to this allocated memory. In "C",
this is accomplished by passing a pointer to a pointer on the stack.

In C# you are passing the address to a null pointer. You should get back an
unmanaged pointer with the allocated image.


"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:4nlgg.24072$JX1.915@xxxxxxxxxxx
BTW - the binary data is used to create .PGM image files.

"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:nhlgg.24033$JX1.23566@xxxxxxxxxxx
thanks again, I really appreciate the help. this problem has been a real
pain.
I tried your suggestion, but it didn't work. however, I feel that I might
be very close.


label1.Text = create_wrapped(0,5,ref ptrImage).ToString();
// returned -1 meaning that there was a problem

my original wrapper function was:

WIN32DLL_API int create_wrapped(int id, int scale, unsigned char *image)
{
return create(id, scale, image);
}

so I tried changing that to:

WIN32DLL_API int create_wrapped(int id, int scale, void** image)
{
return create(id, scale, (unsigned char*)*image);
}

your suggestion wouldn't compile:

WIN32DLL_API int create_wrapped(int id, int scale, void** image)
{
return create(id, scale, (char*)*image);
}

the error was: Cannot convert argument 3 from char* to unsigned char*


"Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx> wrote in message
news:%23EGjMdzhGHA.1508@xxxxxxxxxxxxxxxxxxxxxxx
Your wrapper function should have the argument typed as a pointer to a
pointer for this to work correctly.

[DllImport("Wrapper.dll", EntryPoint="create_wrapped")]
public static extern int create_wrapped(int id, int scale, [In][Out] ref
IntPtr image);

WIN32DLL_API int create_wrapped(int id, int scale, void** image)
{
return create(id, scale, (char*)*image);
}

You call it as follows:

IntPtr ptrImage = IntPtr.Zero;
label1.Text = create_wrapped(0,5,ref ptrImage);

and of course check for a null pointer return.




"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:o1jgg.23800$JX1.19291@xxxxxxxxxxx
I thought that you might be on to something, but sadly it didn't work.
thanks for the help though.

[DllImport("Wrapper.dll", EntryPoint="create_wrapped")]
public static extern int create_wrapped(int id, int scale, [In][Out]
ref IntPtr image);

IntPtr ptrImage = new IntPtr();
label1.Text = create_wrapped(0,5,ref ptrImage).ToString(); //this
crashes the debugger with no exception returned, I tried to catch it
but that didn't help


"Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx> wrote in message
news:uJyWUbyhGHA.4044@xxxxxxxxxxxxxxxxxxxxxxx
You could try the same argument semantics used by the "Gdi32"
function CreateDIBSection which returns an array of bytes as a pointer
argument.

The c# function uses [In][Out] ref IntPtr to represent the array of
bytes returned for the pointer argument passed to CreateDIBSection.
see example:

[DllImport("gdi32.dll")]
static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFO
pbmi,
uint iUsage, [In][Out] ref IntPtr ppvBits, IntPtr hSection, uint
dwOffset);


"Stephen Cawood" <cawood@xxxxxxxxxx> wrote in message
news:3y7gg.16349$JX1.2431@xxxxxxxxxxx
I posted this question a few days ago, but it hasn't been resolved so
I'm trying again (with better info).
I'm trying to access a C++ .lib from C#. I can get an int back fine,
the problem I'm having is returning an array of binary data. any
ideas?
thanks in advance...

.lib contains this function:

/-Call create() to create a bitmap of (10*scale) x (*10*scale) bytes
//create() will fill an unsigned char array with 100*scale*scale
bytes
//
//create ID=567, 5 pixels/bit, total image will be 50x50 pixels
//this function will malloc room if image is NULL
//returns -1 if problem, 0 otherwise

int create(int id, int scale, unsigned char *image);


wrapper DLL:

WIN32DLL_API int create_wrapped(int id, int scale, unsigned char
*image)
{
return create(id, scale, image);
}


[DllImport("Wrapper.dll", EntryPoint="create_wrapped")]
public static extern int create_wrapped(int id, int scale, byte[]
image);

byte[] bArray = new byte[500];
create_wrapped(0,5,bArray); //bArray = array of zeros



notes:
I've also tried...

public static extern int create_wrapped(int id, int scale,
[Out][MarshalAs(UnmanagedType.LPArray, SizeConst=500)]byte[] image);
//bArray = array of zeros

public static extern int create_wrapped(int id, int scale,
[MarshalAs(UnmanagedType.LPArray)]byte[] image); //bArray = array of
zeros


public static extern int create_wrapped(int id, int scale,[Out]
IntPtr image); //returns -1 failure















.



Relevant Pages

  • Re: C# - getting binary data from .lib
    ... Remember the wrapper is returning an unmanaged pointer to an array of bytes. ... WIN32DLL_API int create_wrapped(int id, int scale, unsigned char ... public static extern int create_wrapped(int id, int scale, byte ...
    (microsoft.public.dotnet.framework.interop)
  • Re: C# - getting binary data from .lib
    ... I suggest that you debug the wrapper first to see what you get if you pass a ... either way you get a pointer to allocate ... WIN32DLL_API int create_wrapped(int id, int scale, unsigned char ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Inconsistent results between char/short/int
    ... handling of Linux virtual mem or in weird CPU mem alignments (it's a PPC ... as a short or as an int (the only one that returns correct ... pointer to Address which gets switched to point to Nb. ... I'd make the pointers unsigned char * right form the start. ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... I'm reluctant to try and deal with the unmanaged pointer since ... WIN32DLL_API int create_wrapped(int id, int scale, unsigned char *image) ... public static extern int create_wrapped(int id, int scale, byte ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Learning pointers
    ... Please help me with my pointer understanding... ... int mystrcmp (const char * src, ... than (unsigned char *). ...
    (comp.lang.c)