P/Invoke BYTE *

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

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

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 said:
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.
 
It won't change - a byte array is passed by ref, so your driver can write to
it and the caller will see it (and that's not a callback - a callback is
passing a function pointer). Of course you'll want to pin the managed
source.

-Chris



Mario said:
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.

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 said:
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.
 
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 said:
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.

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 said:
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.
 
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


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 said:
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.

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.

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.
 
Couple options. My choice: declare the equivalent managed struct and pass
it by ref. Other option, declare in as a managed class and pass it by value
(which is by ref for ref types).


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




Mario said:
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


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 said:
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.

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.
 
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 said:
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


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 said:
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.

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.
 
Hi,

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

Cheers,

Mario

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 said:
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


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.

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.

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.
 
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 said:
Hi,

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

Cheers,

Mario

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 said:
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.

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.

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.
 
Hi Chris,

I did download the source code for SDF 1.4. I did find passing arrays,
but did not find any reference for passing an array of structures. The
members of the structure that I need to pass contains the parameters to
drive PMW (sample, period,duration) and I need to pass a bunch of these
to turn on the buzzer. Thanks for your help.

Regards,

Mario

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 said:
Hi,

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

Cheers,

Mario

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.

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.

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.

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.
 
An array of structures could be represented as an array of bytes, just like
a single structure. There will be some alignment (dummy) bytes between
array entries, in most cases, but otherwise it's just the same the single
structure case.

Paul T.

Mario said:
Hi Chris,

I did download the source code for SDF 1.4. I did find passing arrays,
but did not find any reference for passing an array of structures. The
members of the structure that I need to pass contains the parameters to
drive PMW (sample, period,duration) and I need to pass a bunch of these
to turn on the buzzer. Thanks for your help.

Regards,

Mario

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 said:
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.

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.

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.

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.
 
Hi Chris,

Sorry it is a noob question, but how do you pin the managed source
code? Do I use the unsafe block? Thanks.

mario

It won't change - a byte array is passed by ref, so your driver can write to
it and the caller will see it (and that's not a callback - a callback is
passing a function pointer). Of course you'll want to pin the managed
source.

-Chris



Mario said:
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.

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.

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.
 
Thanks all, I finally got it working.

Hi Chris,

Sorry it is a noob question, but how do you pin the managed source
code? Do I use the unsafe block? Thanks.

mario

It won't change - a byte array is passed by ref, so your driver can write to
it and the caller will see it (and that's not a callback - a callback is
passing a function pointer). Of course you'll want to pin the managed
source.

-Chris



Mario said:
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.

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.
 
Back
Top