blemos

I have funtion wich was written in C++ and has the signature like this :

"DWORD XPTO(BYTE **aux)"

I want to use it within C# code but the function has to be declared like: static extern xpto( aux)),

I dont know how to translate it!!!

Can Anyone help

Thanks a lot!

Barbara



Re: Visual C# General Unmanaged to managed code

Mark Benningfield

Hello All.

Barbara:

Try this:



  [DllImport("dllfilename")]
  public static extern Int32 XPTO(byte aux);


 

HTH.






Re: Visual C# General Unmanaged to managed code

ahmedilyas

Sorry Mark, had to change the color of the bright green/blue text as it was hurting the eyes as well as being made as non readable! It's changed to red now




Re: Visual C# General Unmanaged to managed code

Mark Benningfield

Hello All.

ahmedilyas:

Well, it just goes to show about "different strokes." For me, the brighter color was easier to read against the darker background. Are these colors easy to read


private void Form1_VisibleChanged(object sender, EventArgs e)
{
if (this.Visible)
MessageBox.Show("Visible");
}


For me, the standard color for types from the Code editor is too dark against the dark background of the code sample outline. That's what in the above snippet. If it is readable for everyone else, I'll just stick with it.






Re: Visual C# General Unmanaged to managed code

ahmedilyas

yes that sample there is fine Mark - I meant you had this color:



DllImport

 

 

for myself, and other people, its too bright, unreadable and does hurt the eyes. :-)






Re: Visual C# General Unmanaged to managed code

James Curran

Mark Benningfield wrote:

Try this:



[DllImport("dllfilename")]
public static extern Int32 XPTO(byte aux);


Not quite. It's "DWORD XPTO(BYTE **aux)" and those two stars are important.

With one star, I believe we could have used:



[DllImport("dllfilename")]
public static extern Int32 XPTO(ref byte aux);

But for two stars, I think we'll need:



[DllImport("dllfilename")]
public static extern Int32 XPTO(ref IntPtr aux);

with some extra manual code to get th ebyte out of the IntPtr.






Re: Visual C# General Unmanaged to managed code

Mark Benningfield

Hello All.

James:

Well, I'd be the first to admit that my C++ is pretty rusty, but since **aux is a parameter, and parameters are R-values, doesn't that mean that **aux is a double indirection, meaning the data value at the address pointed to by the addressValue at the address pointed to by the pointer In this case, a byte






Re: Visual C# General Unmanaged to managed code

blemos

 

First : Thanks for all the help.

 

What is returned by char** aux is a photo buffer pointer and I am not understanting how can construct the picture from this value.

I have applied several functions in order to get this info from IntPtr (and return the picture) but without any kind of sucess! Can someone please help again with this one.

 

Thanks for all the help given!

 

Barbara

 





Re: Visual C# General Unmanaged to managed code

Mark Benningfield

Hello All.

Barbara:

In the C++ signature of the function, does it say (BYTE **aux), as in your OP, or (BYTE aux**)   As James pointed out, the stars are important, and where they are is equally important.

BYTE **aux returns a byte value, whereas BYTE aux** is a pointer to a pointer that points to a byte array.  Also as James pointed out, getting the final address of the array is going to require a little fancy footwork, because I don't thing C# directly supports spanning pointer pointers.

Let us know, and also, we'll need to know if the C++ code has any way of determining the size of the buffer, if it is one.

HTH.






Re: Visual C# General Unmanaged to managed code

blemos

Hi Mark,

The signature is (BYTE **aux) and so id does return a byte value.

Regarding your second question : the function has a second value  to be returned "DWORD *imagelenght" that is giving me (correctly) the lenght of the photo data buffer.

 Function "DWORD XPTO(BYTE **aux,DWORD *imagelenght,char**type)"

 

The 3rd gives the type of picture

 

Thanks a lot!!

 

Barbara

 

 

 





Re: Visual C# General Unmanaged to managed code

James Curran

BYTE **aux returns a byte value, whereas BYTE aux** is a pointer to a pointer that points to a byte array.

um..No. "BYTE **aux" is a pointer to a pointer to a byte. The stars attach to the BYTE, not to the aux. Hence aux is a pointer.

Later, when I use the value aux, then the stars attach to aux:

BYTE** ppByte = aux;

BYTE* pByte = *aux; // aux points to a pointer to a byte.

BYTE Byte = **aux;

However, Barbara has mention that the refers to an array of bytes. This may allow us to make this work.

Now, it it were defined as :

DWORD XPTO(BYTE *aux)

Then the equavalent C# code would be:

Int32 XPTO([MarshalAs(UnmanagedType.LPArray, SizeConst= )] byte[] aux)

Now, for

DWORD XPTO(BYTE **aux)

Then the equavalent C# code would be:

Int32 XPTO([MarshalAs(UnmanagedType.LPArray, SizeConst= )] ref byte[] aux)

However, both of these these open the question of how to fill in the SizeConst parameter.






Re: Visual C# General Unmanaged to managed code

Mark Benningfield

Hello All.

James:

As I say, my C++ is pretty rusty, and getting worse the more I use C#, so I'll defer to you. As to the question of the length of the buffer:

Barbara:

This function returns a DWORD. Is it possible that a call to this function with zero or null specified for the buffer results in a return of the length required for that buffer, to be passed in the imagelength parameter See if you can find that out, and we'll try and make some more progress.






Re: Visual C# General Unmanaged to managed code

blemos

Hi Mark,

To have the buffer lenght I can just do something like "DWORD XPTO(null,DWORD *imagelenght,char**type)" And this is giving the lenght. DWORD has sucess/error codes. I have already tried with byte[] but nothing happens :( , it seems like is loosing information when passing from ** to [] ...

Thanks,

Barbara





Re: Visual C# General Unmanaged to managed code

James Curran

OK, let me give this a shot:

Int32 XPTO([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1) out byte[] aux, out Int32 imagelength, out string type);






Re: Visual C# General Unmanaged to managed code

blemos

Sorry James,

It doesn't compile Cannot marshal 'parameter #1': Cannot use SizeParamIndex for ByRef array parameters.

:S

Barbara