473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to marshal C pointers to arrays in VB .NET

Hi
I have a C struct that is of the following

typedef struct{

DWORD num_conversions; ...

short *sample_values;

....

} DSCAIOINT;
In VB I'm doing the following
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _

Public Structure DSCAIOINT

....

Public num_conversions As Integer

....

Public sample_values() As Short

End Structure

in my code I'm passing my DSCAIOINT structure as

Public Declare Function dscADSampleInt Lib "a.dll" ( ByRef dscaioint As
DSCAIOINT) As Byte

However I'm not sure how to marshal the sample_values array so it will be a
short pointer. VB .NET keeps giving me errors of 'System.TypeLoadException'

"Additional information: Can not marshal field sample_values of type
ADSampleInt.DSCAIOINT: This type can not be marshaled as a structure field."

Can any one give me some help? thank you!
--
Kevin Liu
Nov 21 '05 #1
3 3232
Define sample_values as IntPtr and use Marshal.Copy to retrieve the data.

Public Structure DSCAIOINT
Public num_conversions As Integer

....

Public sample_values As IntPtr

End Structure

Public Declare Function dscADSampleInt Lib "a.dll" ( _
ByRef dscaioint As DSCAIOINT) As Byte

Dim myStruct As DSCAIOINT
Dim BUFF_SIZE = 2048

myStruct.sample_values = Marshal.Alloc(SizeOf(Short)*BUFF_SIZE)

Dim Result As Byte = dscADSampleInt(myStruct)

Dim samples(BUFF_SIZE) As Short

Marshal.Copy(myStruct.sample_values, samples, 0, BUFF_SIZE)
hope that helps..
Imran.

"kevin" <z2****@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi
I have a C struct that is of the following

typedef struct{

DWORD num_conversions; ...

short *sample_values;

...

} DSCAIOINT;
In VB I'm doing the following
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _

Public Structure DSCAIOINT

...

Public num_conversions As Integer

...

Public sample_values() As Short

End Structure

in my code I'm passing my DSCAIOINT structure as

Public Declare Function dscADSampleInt Lib "a.dll" ( ByRef dscaioint As
DSCAIOINT) As Byte

However I'm not sure how to marshal the sample_values array so it will be
a
short pointer. VB .NET keeps giving me errors of
'System.TypeLoadException'

"Additional information: Can not marshal field sample_values of type
ADSampleInt.DSCAIOINT: This type can not be marshaled as a structure
field."

Can any one give me some help? thank you!
--
Kevin Liu

Nov 21 '05 #2
Hi Imran
You are a genius! Thanks for the help. I had to use Marshal.AllocHGlobal
but everything works. Thanks for your help!

--
Kevin Liu
"Imran Koradia" <no****@microsoft.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
Define sample_values as IntPtr and use Marshal.Copy to retrieve the data.

Public Structure DSCAIOINT
Public num_conversions As Integer

....

Public sample_values As IntPtr

End Structure

Public Declare Function dscADSampleInt Lib "a.dll" ( _
ByRef dscaioint As DSCAIOINT) As Byte

Dim myStruct As DSCAIOINT
Dim BUFF_SIZE = 2048

myStruct.sample_values = Marshal.Alloc(SizeOf(Short)*BUFF_SIZE)

Dim Result As Byte = dscADSampleInt(myStruct)

Dim samples(BUFF_SIZE) As Short

Marshal.Copy(myStruct.sample_values, samples, 0, BUFF_SIZE)
hope that helps..
Imran.

"kevin" <z2****@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi
I have a C struct that is of the following

typedef struct{

DWORD num_conversions; ...

short *sample_values;

...

} DSCAIOINT;
In VB I'm doing the following
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _

Public Structure DSCAIOINT

...

Public num_conversions As Integer

...

Public sample_values() As Short

End Structure

in my code I'm passing my DSCAIOINT structure as

Public Declare Function dscADSampleInt Lib "a.dll" ( ByRef dscaioint As
DSCAIOINT) As Byte

However I'm not sure how to marshal the sample_values array so it will be a
short pointer. VB .NET keeps giving me errors of
'System.TypeLoadException'

"Additional information: Can not marshal field sample_values of type
ADSampleInt.DSCAIOINT: This type can not be marshaled as a structure
field."

Can any one give me some help? thank you!
--
Kevin Liu


Nov 21 '05 #3
> I had to use Marshal.AllocHGlobal but everything works.
Oops..sorry - thats what I meant. There's no Alloc method in the Marshal
class..
Thanks for your help!

glad to help :)
Imran.
Nov 21 '05 #4

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is...
2
by: Rory Plaire | last post by:
After searching and experimentation, I found the note in the remarks section of the MSDN doc on Marshal::GetDelegateForFunctionPointer...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
3
by: Charming12 | last post by:
Hi All, This is a problem which is eating my head from several days. I have a structure which contains an array of elements as public byte pinNumbers;
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.