Re: P/Invoke BYTE *



Take a look at the OpenNETCF.Net namespace in the SDF 1.4 source (still a
free download). It's got loads of examples of this.


--
Chris Tacke
OpenNETCF Consulting
www.opennetcf.com
--




"Mario" <alphatommy@xxxxxxxxxxx> wrote in message
news:1154474033.674652.181680@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

Is there somewhere where I can find an example of what you are
suggesting? Thanks.

Cheers,

Mario


Paul G. Tobey [eMVP] wrote:
Since this is a special case, knowing what's to be passed to it, I'd be
inclined to use the scheme found in many of the OpenNETCF classes: you
have
a managed class with the fields that you want exposed via get/set
methods.
There is also a method to get the structure as a byte[]. Declare the
function to take a byte[], not an IntPtr. This provides the flexibility
to
easily call the function with a variety of different structures, since
they
just have to be convertable to byte[].

Paul T.

"Mario" <alphatommy@xxxxxxxxxxx> wrote in message
news:1154390414.492187.272730@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for your replies. I think I got it semi working. I can write,
but can not get any return values. Again, I might be reading it wrong.
Another question that I have is that on another function it has a
parameter of type LPCVOID. According to some docs that I have found,
type LPCVOID should be translated into type IntPtr when calling from
C#. I also know that LPCVOID is pointed to a structure that has the
following members:

typedef struct {

UINT32 val1;
UINT32 val2;
UNIT32 val3;
} Sample_t, *pSample_t

What would I need to do to use this function from C#? Thanks for your
help.

Regards,

Mario



Paul G. Tobey [eMVP] wrote:
A call back or some data copied into the buffer passed as a parameter?
A
callback is going to require a rather different scheme (look up
delegates
in
the help or in the archives of this group). If you just want to copy
some
data into a pre-allocated managed array in your unmanaged code, go
ahead.

Paul T.

"Mario" <alphatommy@xxxxxxxxxxx> wrote in message
news:1154041731.172840.235320@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Paul,

Your suggestion worked great. Thanks.

I have another question. How would the declaration different if the
function needs a call back. I want the driver (dll function) to
return
value to pDataBuffer. Thanks.


Paul G. Tobey [eMVP] wrote:
I think that you want that to be a byte array.

public static extern void myfunction(IntPtr myhandle, Byte
byAddress,
byte[] pDataBuffer, int iSize);

Paul T.

"Mario" <alphatommy@xxxxxxxxxxx> wrote in message
news:1153946653.546113.166310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

I have this function in a dll

void myfunction (HANDLE myhandle, BYTE byAddress, BYTE*
pDataBuffer,
int iSize)

Invoking using Platform invoke by using the following:

[DllImport("mydll.dll")]
public static extern void myfunction(IntPtr myhandle, Byte
byAddress,
ref Byte pDataBuffer, int iSize);

Somehow I don't seem to be getting data from pDataBuffer. What
is
the
right declaration for BYTE * in platform invoke?

Thanks.






.



Relevant Pages

  • Re: P/Invoke BYTE *
    ... Paul G. Tobey wrote: ... value to pDataBuffer. ... bytepDataBuffer, int iSize); ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: P/Invoke BYTE *
    ... inclined to use the scheme found in many of the OpenNETCF classes: ... Paul G. Tobey wrote: ... value to pDataBuffer. ... bytepDataBuffer, int iSize); ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: P/Invoke BYTE *
    ... value to pDataBuffer. ... public static extern void myfunction(IntPtr myhandle, Byte byAddress, ... bytepDataBuffer, int iSize); ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: P/Invoke BYTE *
    ... public static extern void myfunction(IntPtr myhandle, Byte byAddress, ... bytepDataBuffer, int iSize); ... void myfunction (HANDLE myhandle, BYTE byAddress, BYTE* pDataBuffer, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: P/Invoke BYTE *
    ... I did find passing arrays, ... but did not find any reference for passing an array of structures. ... Paul G. Tobey wrote: ... value to pDataBuffer. ...
    (microsoft.public.dotnet.framework.compactframework)