473,763 Members | 7,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dealing with unsigned char* from C++ .lib

I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array of bytes.

For example, the .lib contains this function:

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

In the wrapper DLL I have this function:

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

I also tried to return the unsigned char* like this:

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

In C#, I've tried to access the functions like this:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale,[In, Out] IntPtr
image);

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
public static extern IntPtr create_wrapped_ returnimage(int id, int
scale,[In] IntPtr image);

Then I try to manage the array like this:

IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(un managedArray, newArray, 0, 500);

I then checked what I have and get an error that this returns null:

label1.Text = unmanagedArray. GetType().ToStr ing();

Eventually, I'll be converting the original byte array into an image, but
clearly I need to get this working first. I've tried various data types to
try and deal with the "unsigned char *image," but I haven't found a
solution.
Can someone suggest the best way to deal with this? Should I be converting
the unsigned char to a different type within the C++ wrapper function? If
so, some sample code would be helpful.

Thanks in advance.

May 31 '06 #1
5 7798
Stephen,

Does the function allocate the memory for the bytes? My guess is that
it doesn't. Otherwise, you would have a double reference to byte in the
unmanaged declaration.

However, it doesn't seem to have a mechanism to allow you to specify how
big the buffer is that you are passing to it. This is important, since you
don't want the API function to overwrite memory that does not belong to it.

Before I give an answer, can you show how you would call this (including
the code to set up any variables you are passing, as well as how you would
interpret or clean up those variables when done)? This way, we can indicate
what you have to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stephen Cawood" <ca****@canada. com> wrote in message
news:uwjfg.220$ I61.137@clgrps1 3...
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array of bytes.

For example, the .lib contains this function:

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

In the wrapper DLL I have this function:

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

I also tried to return the unsigned char* like this:

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

In C#, I've tried to access the functions like this:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale,[In, Out]
IntPtr image);

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
public static extern IntPtr create_wrapped_ returnimage(int id, int
scale,[In] IntPtr image);

Then I try to manage the array like this:

IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(un managedArray, newArray, 0, 500);

I then checked what I have and get an error that this returns null:

label1.Text = unmanagedArray. GetType().ToStr ing();

Eventually, I'll be converting the original byte array into an image, but
clearly I need to get this working first. I've tried various data types to
try and deal with the "unsigned char *image," but I haven't found a
solution.
Can someone suggest the best way to deal with this? Should I be
converting the unsigned char to a different type within the C++ wrapper
function? If so, some sample code would be helpful.

Thanks in advance.

May 31 '06 #2
thanks for the message, here are some more details.
this code is in my C++ wrapper, it includes a description of the function
and it writes the image out to a file. I was using it to test that the
wrapper was accessing the lib properly. I'd like to be able to do the same
from C#.

//-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
//create() will fill an unsigned char array with 100*scale*scale bytes
WIN32DLL_API int create_wrapped( int id, int scale, unsigned char *image)
{
// This code writes the image out to a .PGM file
create(id, scale, image);
char *file_name = "test.pgm";
char *comment = "createimag e";
int width = 10*scale;
int height = 10*scale;
FILE *out;
int i,j;
out=(FILE*)fope n(file_name,"wb ");
if(out==NULL)
{printf("PGM_FU NCTIONS.C error: Couldn't open %s for
writing\n",file _name);exit(1); }
fprintf(out,"P5 \n#%s\n",commen t);
fprintf(out,"%d %d\n255\n",widt h,height);

for(i=0;i<width *height;i++)
{
j=(int)(*(image +i));
fputc(j,out);
}
fclose(out);
return create(id, scale, image);
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:ul******** ******@TK2MSFTN GP03.phx.gbl...
Stephen,

Does the function allocate the memory for the bytes? My guess is that
it doesn't. Otherwise, you would have a double reference to byte in the
unmanaged declaration.

However, it doesn't seem to have a mechanism to allow you to specify
how big the buffer is that you are passing to it. This is important,
since you don't want the API function to overwrite memory that does not
belong to it.

Before I give an answer, can you show how you would call this
(including the code to set up any variables you are passing, as well as
how you would interpret or clean up those variables when done)? This way,
we can indicate what you have to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stephen Cawood" <ca****@canada. com> wrote in message
news:uwjfg.220$ I61.137@clgrps1 3...
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int), but
I'm having issues dealing with an array of bytes.

For example, the .lib contains this function:

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

In the wrapper DLL I have this function:

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

I also tried to return the unsigned char* like this:

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

In C#, I've tried to access the functions like this:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale,[In, Out]
IntPtr image);

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
public static extern IntPtr create_wrapped_ returnimage(int id, int
scale,[In] IntPtr image);

Then I try to manage the array like this:

IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(un managedArray, newArray, 0, 500);

I then checked what I have and get an error that this returns null:

label1.Text = unmanagedArray. GetType().ToStr ing();

Eventually, I'll be converting the original byte array into an image, but
clearly I need to get this working first. I've tried various data types
to try and deal with the "unsigned char *image," but I haven't found a
solution.
Can someone suggest the best way to deal with this? Should I be
converting the unsigned char to a different type within the C++ wrapper
function? If so, some sample code would be helpful.

Thanks in advance.


May 31 '06 #3
I got some advice from the interop group, but I am still working on the
problem. here's the current status...

I currently get the error "Can not marshall return type"
this is what I'm trying...
//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

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
[return : MarshalAs(Unman agedType.LPArra y)]
public static extern byte[] create_wrapped_ returnimage(int id, int scale,
[Out] byte[] image);

then to use it...

//Test with returnImage
byte[] bImage = new byte[2560];
bImage = create_wrapped_ returnimage(0,5 ,bImage);
"Stephen Cawood" <ca****@canada. com> wrote in message
news:d8kfg.227$ I61.119@clgrps1 3...
thanks for the message, here are some more details.
this code is in my C++ wrapper, it includes a description of the function
and it writes the image out to a file. I was using it to test that the
wrapper was accessing the lib properly. I'd like to be able to do the same
from C#.

//-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
//create() will fill an unsigned char array with 100*scale*scale bytes
WIN32DLL_API int create_wrapped( int id, int scale, unsigned char *image)
{
// This code writes the image out to a .PGM file
create(id, scale, image);
char *file_name = "test.pgm";
char *comment = "createimag e";
int width = 10*scale;
int height = 10*scale;
FILE *out;
int i,j;
out=(FILE*)fope n(file_name,"wb ");
if(out==NULL)
{printf("PGM_FU NCTIONS.C error: Couldn't open %s for
writing\n",file _name);exit(1); }
fprintf(out,"P5 \n#%s\n",commen t);
fprintf(out,"%d %d\n255\n",widt h,height);

for(i=0;i<width *height;i++)
{
j=(int)(*(image +i));
fputc(j,out);
}
fclose(out);
return create(id, scale, image);
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:ul******** ******@TK2MSFTN GP03.phx.gbl...
Stephen,

Does the function allocate the memory for the bytes? My guess is that
it doesn't. Otherwise, you would have a double reference to byte in the
unmanaged declaration.

However, it doesn't seem to have a mechanism to allow you to specify
how big the buffer is that you are passing to it. This is important,
since you don't want the API function to overwrite memory that does not
belong to it.

Before I give an answer, can you show how you would call this
(including the code to set up any variables you are passing, as well as
how you would interpret or clean up those variables when done)? This
way, we can indicate what you have to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stephen Cawood" <ca****@canada. com> wrote in message
news:uwjfg.220$ I61.137@clgrps1 3...
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int),
but I'm having issues dealing with an array of bytes.

For example, the .lib contains this function:

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

In the wrapper DLL I have this function:

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

I also tried to return the unsigned char* like this:

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

In C#, I've tried to access the functions like this:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale,[In, Out]
IntPtr image);

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
public static extern IntPtr create_wrapped_ returnimage(int id, int
scale,[In] IntPtr image);

Then I try to manage the array like this:

IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(un managedArray, newArray, 0, 500);

I then checked what I have and get an error that this returns null:

label1.Text = unmanagedArray. GetType().ToStr ing();

Eventually, I'll be converting the original byte array into an image,
but clearly I need to get this working first. I've tried various data
types to try and deal with the "unsigned char *image," but I haven't
found a solution.
Can someone suggest the best way to deal with this? Should I be
converting the unsigned char to a different type within the C++ wrapper
function? If so, some sample code would be helpful.

Thanks in advance.



Jun 1 '06 #4
after some more advice, I'm trying this code, but the result is an array of
zeros. I don't seem to be getting the bytes.

wrapper DLL:

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

test C# form:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale, [Out] byte[]
image);

byte[] bArray = new byte[2560];
create_wrapped( 0,5,bArray);

label1.Text = bArray[0].ToString();

the result is: 0
also, when I look in the debugger, the array is all zeros

BTW - when I check the return value of the function like this...

label1.Text = create_wrapped( 0,5,bArray).ToS tring();

the result is: 0
(which is success)
notes:

/-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

"Stephen Cawood" <ca****@canada. com> wrote in message
news:96Efg.490$ I61.347@clgrps1 3...
I got some advice from the interop group, but I am still working on the
problem. here's the current status...

I currently get the error "Can not marshall return type"
this is what I'm trying...
//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

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
[return : MarshalAs(Unman agedType.LPArra y)]
public static extern byte[] create_wrapped_ returnimage(int id, int scale,
[Out] byte[] image);

then to use it...

//Test with returnImage
byte[] bImage = new byte[2560];
bImage = create_wrapped_ returnimage(0,5 ,bImage);
"Stephen Cawood" <ca****@canada. com> wrote in message
news:d8kfg.227$ I61.119@clgrps1 3...
thanks for the message, here are some more details.
this code is in my C++ wrapper, it includes a description of the function
and it writes the image out to a file. I was using it to test that the
wrapper was accessing the lib properly. I'd like to be able to do the
same from C#.

//-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
//create() will fill an unsigned char array with 100*scale*scale bytes
WIN32DLL_API int create_wrapped( int id, int scale, unsigned char *image)
{
// This code writes the image out to a .PGM file
create(id, scale, image);
char *file_name = "test.pgm";
char *comment = "createimag e";
int width = 10*scale;
int height = 10*scale;
FILE *out;
int i,j;
out=(FILE*)fope n(file_name,"wb ");
if(out==NULL)
{printf("PGM_FU NCTIONS.C error: Couldn't open %s for
writing\n",file _name);exit(1); }
fprintf(out,"P5 \n#%s\n",commen t);
fprintf(out,"%d %d\n255\n",widt h,height);

for(i=0;i<width *height;i++)
{
j=(int)(*(image +i));
fputc(j,out);
}
fclose(out);
return create(id, scale, image);
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:ul******** ******@TK2MSFTN GP03.phx.gbl...
Stephen,

Does the function allocate the memory for the bytes? My guess is
that it doesn't. Otherwise, you would have a double reference to byte
in the unmanaged declaration.

However, it doesn't seem to have a mechanism to allow you to specify
how big the buffer is that you are passing to it. This is important,
since you don't want the API function to overwrite memory that does not
belong to it.

Before I give an answer, can you show how you would call this
(including the code to set up any variables you are passing, as well as
how you would interpret or clean up those variables when done)? This
way, we can indicate what you have to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stephen Cawood" <ca****@canada. com> wrote in message
news:uwjfg.220$ I61.137@clgrps1 3...
I'm trying to use a C++ .lib from C# (I tried the Interop group will no
results).
I have a working wrapper DLL (I can get back simple things like int),
but I'm having issues dealing with an array of bytes.

For example, the .lib contains this function:

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

In the wrapper DLL I have this function:

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

I also tried to return the unsigned char* like this:

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

In C#, I've tried to access the functions like this:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale,[In, Out]
IntPtr image);

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
public static extern IntPtr create_wrapped_ returnimage(int id, int
scale,[In] IntPtr image);

Then I try to manage the array like this:

IntPtr ptrArg = new IntPtr();
IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
byte [] newArray = new byte[500];
Marshal.Copy(un managedArray, newArray, 0, 500);

I then checked what I have and get an error that this returns null:

label1.Text = unmanagedArray. GetType().ToStr ing();

Eventually, I'll be converting the original byte array into an image,
but clearly I need to get this working first. I've tried various data
types to try and deal with the "unsigned char *image," but I haven't
found a solution.
Can someone suggest the best way to deal with this? Should I be
converting the unsigned char to a different type within the C++ wrapper
function? If so, some sample code would be helpful.

Thanks in advance.




Jun 2 '06 #5

it works! wow, this took some time. anyway, here's the winning combination.
thanks again.
C .lib
-------------------
/-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);
C++ Wrapper
-------------------

WRAPPER_API int create_wrapped( int id, int scale, void** image)
{
int retCode = -1;
unsigned char* ptrImage = NULL;

if ( NULL != *image )
{
ptrImage = (unsigned char*)GlobalLoc k(*image);
retCode = create(id, scale, ptrImage);
GlobalUnlock(*i mage);
}
else
{
retCode = create(id, scale, ptrImage);
*image = (void*)ptrImage ;
}

return retCode;

}
C# Accessing the wrapper function
-------------------
// create
[DllImport("Wrap per.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale, [In][Out] ref
IntPtr image);
C# Using the data
-------------------

// Allocate unmanaged memory - must be freed with
Marshal.FreeHGl obal(ptrImage)
IntPtr ptrImage = Marshal.AllocHG lobal(2500);
// Try to copy the unmanaged bytes to a managed array
try
{
// Get a pointer to an unmanaged byte array which contains the image
label1.Text = create_wrapped( 0, 5, ref ptrImage).ToStr ing(); //return 0 for
success
byte[] bManagedArray = new byte[2500];

// Read through entire pointer byte by byte and put into managed array
for (int i = 0; i < bManagedArray.L ength; i++)
{
bManagedArray[i] = Marshal.ReadByt e(ptrImage, i);
}
// custom function to Write the PGM image out to a file
writeImageToFil e(bManagedArray );

}
catch (ArgumentNullEx ception ex)
{
MessageBox.Show (ex.Message, "Marshal Copy Error", MessageBoxButto ns.OK,
MessageBoxIcon. Exclamation);
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGl obal(ptrImage);
}

"Stephen Cawood" <ca****@canada. com> wrote in message
news:IpWfg.1729 $I61.1099@clgrp s13...
after some more advice, I'm trying this code, but the result is an array
of zeros. I don't seem to be getting the bytes.

wrapper DLL:

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

test C# form:

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
public static extern int create_wrapped( int id, int scale, [Out] byte[]
image);

byte[] bArray = new byte[2560];
create_wrapped( 0,5,bArray);

label1.Text = bArray[0].ToString();

the result is: 0
also, when I look in the debugger, the array is all zeros

BTW - when I check the return value of the function like this...

label1.Text = create_wrapped( 0,5,bArray).ToS tring();

the result is: 0
(which is success)
notes:

/-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

"Stephen Cawood" <ca****@canada. com> wrote in message
news:96Efg.490$ I61.347@clgrps1 3...
I got some advice from the interop group, but I am still working on the
problem. here's the current status...

I currently get the error "Can not marshall return type"
this is what I'm trying...
//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

[DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
[return : MarshalAs(Unman agedType.LPArra y)]
public static extern byte[] create_wrapped_ returnimage(int id, int scale,
[Out] byte[] image);

then to use it...

//Test with returnImage
byte[] bImage = new byte[2560];
bImage = create_wrapped_ returnimage(0,5 ,bImage);
"Stephen Cawood" <ca****@canada. com> wrote in message
news:d8kfg.227$ I61.119@clgrps1 3...
thanks for the message, here are some more details.
this code is in my C++ wrapper, it includes a description of the
function and it writes the image out to a file. I was using it to test
that the wrapper was accessing the lib properly. I'd like to be able to
do the same from C#.

//-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
//create() will fill an unsigned char array with 100*scale*scale bytes
WIN32DLL_API int create_wrapped( int id, int scale, unsigned char *image)
{
// This code writes the image out to a .PGM file
create(id, scale, image);
char *file_name = "test.pgm";
char *comment = "createimag e";
int width = 10*scale;
int height = 10*scale;
FILE *out;
int i,j;
out=(FILE*)fope n(file_name,"wb ");
if(out==NULL)
{printf("PGM_FU NCTIONS.C error: Couldn't open %s for
writing\n",file _name);exit(1); }
fprintf(out,"P5 \n#%s\n",commen t);
fprintf(out,"%d %d\n255\n",widt h,height);

for(i=0;i<width *height;i++)
{
j=(int)(*(image +i));
fputc(j,out);
}
fclose(out);
return create(id, scale, image);
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:ul******** ******@TK2MSFTN GP03.phx.gbl...
Stephen,

Does the function allocate the memory for the bytes? My guess is
that it doesn't. Otherwise, you would have a double reference to byte
in the unmanaged declaration.

However, it doesn't seem to have a mechanism to allow you to specify
how big the buffer is that you are passing to it. This is important,
since you don't want the API function to overwrite memory that does not
belong to it.

Before I give an answer, can you show how you would call this
(including the code to set up any variables you are passing, as well as
how you would interpret or clean up those variables when done)? This
way, we can indicate what you have to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stephen Cawood" <ca****@canada. com> wrote in message
news:uwjfg.220$ I61.137@clgrps1 3...
> I'm trying to use a C++ .lib from C# (I tried the Interop group will
> no results).
> I have a working wrapper DLL (I can get back simple things like int),
> but I'm having issues dealing with an array of bytes.
>
> For example, the .lib contains this function:
>
> int create(int id, int scale, unsigned char *image);
>
> In the wrapper DLL I have this function:
>
> WIN32DLL_API int create_wrapped( int id, int scale, unsigned char
> *image)
> {
> return create(id, scale, image);
> }
>
> I also tried to return the unsigned char* like this:
>
> WIN32DLL_API int create_wrapped_ returnimage(int id, int scale,
> unsigned char *image)
> {
> create(id, scale, image);
> return image;
> }
>
> In C#, I've tried to access the functions like this:
>
> [DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped")]
> public static extern int create_wrapped( int id, int scale,[In, Out]
> IntPtr image);
>
> [DllImport("Win3 2DLL.dll", EntryPoint="cre ate_wrapped_ret urnimage")]
> public static extern IntPtr create_wrapped_ returnimage(int id, int
> scale,[In] IntPtr image);
>
> Then I try to manage the array like this:
>
> IntPtr ptrArg = new IntPtr();
> IntPtr unmanagedArray = create_wrapped_ returnimage(0,5 ,ptrArg);
> byte [] newArray = new byte[500];
> Marshal.Copy(un managedArray, newArray, 0, 500);
>
> I then checked what I have and get an error that this returns null:
>
> label1.Text = unmanagedArray. GetType().ToStr ing();
>
> Eventually, I'll be converting the original byte array into an image,
> but clearly I need to get this working first. I've tried various data
> types to try and deal with the "unsigned char *image," but I haven't
> found a solution.
> Can someone suggest the best way to deal with this? Should I be
> converting the unsigned char to a different type within the C++
> wrapper function? If so, some sample code would be helpful.
>
> Thanks in advance.
>
>
>



Jun 4 '06 #6

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

Similar topics

19
6481
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
3
31511
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
13
1858
by: Eric Lilja | last post by:
Hello, consider the following complete program: #include <assert.h> #include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> static int has_char(const char *, const char);
42
3373
by: yong | last post by:
Hi all I have an large integer in this format x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6 now I must convert it to this format y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5 x1-x5 is given.I must get y1-y4 from it. How can I do this on my 32bit PC?
3
41944
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned 0x%x\n",(unsigned char)b); }
5
2850
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
11751
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
8
2339
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while (*s1 == *s2) {
29
9990
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.