Marshalling Byte Array

D

Denis C

Hi,

Taking Chris's suggestion I am trying to take byte
array's as returns from C library functions that return
pointers to structs. On the regular framework I am
getting a marshalling error. Would I get this error on
the compact?

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As Byte()
End Function

Then:

Dim ptr As Byte() = ClassName.GetLongVal()

and I get:

An unhandled exception of
type 'System.Runtime.InteropServices.MarshalDirectiveExcep
tion' occurred in ConsoleApplication1.exe

Additional information: Can not marshal return value.


What do you think?

Denis
 
A

Alex Yakhnin, eMVP

Would I get this error on
the compact?

Yes you would. Marshaller can not return the value types greater than 32
bits in CF.
If you need to pass some bytes from unmananged to managed program you can do
it by passing a pointer to a byte array. Just don't forget to "Pin" it to
prevent GC from moving.
 
D

Denis C

Thanks Alex. I can't say I know anything about Pinning.
Both CF and VB are completely new to me. Maybe you could
read some of my previous thread "Developing For the
Compact Framework" and give me a little insight.

I had the functions returning IntPtr but I cannot then
assign those to a byte() in VB. Is VB the problem here?
Can I do this in VB oe because of the pointer is it much
better to use C#?


Thanks,

Denis
 
A

Alex Yakhnin, eMVP

I had the functions returning IntPtr but I cannot then
assign those to a byte() in VB. Is VB the problem here?
Can I do this in VB oe because of the pointer is it much
better to use C#?

Unless you're planing to use "unsafe" pointers, VB should be able to handle
with P/Invoke just fine. Language in this case becomes a matter of
preference (I prefer to use C#).
Anyway, if you could post the original eVC++ structure and function
declarations again, myself or somebody else should be able to help you.
 
D

Denis C

Sorry, I've posted a few times without luck. One more
time:

Here's the structure. There are about 6 or 7 similar to
this.

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

this function returns GetLongRTS, a pointer to the
GetLongRT struct.

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As ***
End Function

I need to know what the return type should be in place
of "***" and how to get at the data inside once I have it.

Thanks,

Denis
 
A

Alex Yakhnin, eMVP

Denis,

If the function returns a pointer then the return type should be IntPtr.

When you get this pointer, then first you can copy it to a byte array and
strip the data from it.
Something like that:

Dim hStruct as IntPtr = GetLongVal()
Dim sval as String
Dim lval as Long

Dim size as Integer = 30 + 16

Dim bData as Byte() = New Byte(size)

Marshal.Copy(hStruct, bData, 0, size)

sval = System.Text.ASCIIEncoding.ASCII.GetString(bData, 0, 30)
lval = BitConverter.ToInt64(bData, 31, 16)


Sorry if I did something wrong from the sintax point of view - I am a little
bit rusty with VB.NET.

HTH... Alex
--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com

Denis C said:
Sorry, I've posted a few times without luck. One more
time:

Here's the structure. There are about 6 or 7 similar to
this.

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

this function returns GetLongRTS, a pointer to the
GetLongRT struct.

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As ***
End Function

I need to know what the return type should be in place
of "***" and how to get at the data inside once I have it.

Thanks,

Denis
-----Original Message-----

Unless you're planing to use "unsafe" pointers, VB should be able to handle
with P/Invoke just fine. Language in this case becomes a matter of
preference (I prefer to use C#).
Anyway, if you could post the original eVC++ structure and function
declarations again, myself or somebody else should be able to help you.

--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com




.
 
D

Denis C

Thanks so much. The code is very straightforward and
close to what I was trying. I was trying Array.copy!!
I'll be sure to let you know how it goes!

Denis
-----Original Message-----
Denis,

If the function returns a pointer then the return type should be IntPtr.

When you get this pointer, then first you can copy it to a byte array and
strip the data from it.
Something like that:

Dim hStruct as IntPtr = GetLongVal()
Dim sval as String
Dim lval as Long

Dim size as Integer = 30 + 16

Dim bData as Byte() = New Byte(size)

Marshal.Copy(hStruct, bData, 0, size)

sval = System.Text.ASCIIEncoding.ASCII.GetString(bData, 0, 30)
lval = BitConverter.ToInt64(bData, 31, 16)


Sorry if I did something wrong from the sintax point of view - I am a little
bit rusty with VB.NET.

HTH... Alex
--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com

Sorry, I've posted a few times without luck. One more
time:

Here's the structure. There are about 6 or 7 similar to
this.

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

this function returns GetLongRTS, a pointer to the
GetLongRT struct.

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As ***
End Function

I need to know what the return type should be in place
of "***" and how to get at the data inside once I have it.

Thanks,

Denis
-----Original Message-----
I had the functions returning IntPtr but I cannot then
assign those to a byte() in VB. Is VB the problem here?
Can I do this in VB oe because of the pointer is it much
better to use C#?

Unless you're planing to use "unsafe" pointers, VB should be able to handle
with P/Invoke just fine. Language in this case
becomes a
matter of
preference (I prefer to use C#).
Anyway, if you could post the original eVC++ structure and function
declarations again, myself or somebody else should be able to help you.

--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com

"Denis C" <[email protected]> wrote
in
message
Thanks Alex. I can't say I know anything about Pinning.
Both CF and VB are completely new to me. Maybe you could
read some of my previous thread "Developing For the
Compact Framework" and give me a little insight.

I had the functions returning IntPtr but I cannot then
assign those to a byte() in VB. Is VB the problem here?
Can I do this in VB oe because of the pointer is it much
better to use C#?


Thanks,

Denis
-----Original Message-----
Would I get this error on
the compact?

Yes you would. Marshaller can not return the value types
greater than 32
bits in CF.
If you need to pass some bytes from unmananged to
managed program you can do
it by passing a pointer to a byte array. Just don't
forget to "Pin" it to
prevent GC from moving.

--
Alex Yakhnin, .NET CF MVP
IntelliProg, Inc.
http://www.intelliprog.com

"Denis C" <[email protected]>
wrote
in
message
Hi,

Taking Chris's suggestion I am trying to take byte
array's as returns from C library functions that return
pointers to structs. On the regular framework I am
getting a marshalling error. Would I get this error on
the compact?

<DllImport("DBAccess.dll", SetLastError:=True)> _
Public Shared Function GetLongVal() As Byte()
End Function

Then:

Dim ptr As Byte() = ClassName.GetLongVal()

and I get:

An unhandled exception of
type 'System.Runtime.InteropServices.MarshalDirectiveExcep
tion' occurred in ConsoleApplication1.exe

Additional information: Can not marshal return value.


What do you think?

Denis


.



.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top