Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to call an external API function that takes a struct containing arrays as its parameter?

Thread Tools

How to call an external API function that takes a struct containing arrays as its parameter?

Robert Rossney
Posts: n/a
I'm working on a .NET application interoperates with a legacy application.
This application exposes an API through a single function call that takes a
great big struct full of string data as its argument.

In VB6, I can call the API by using these declarations:

Public Declare Sub MyApi Lib "MyApi.DLL" (buf As BUFFER_TYPE)

foo As String * 50
bar(49) As String * 100
status As int
End Type

I'm trying to write the equivalent C#.Net code, and I *think* it should look
something like this:

[StructLayout (LayoutKind.Sequential)]

internal struct barString
[MarshalAs( UnmanagedType.AnsiBStr, SizeConst=100)]
internal string data;

internal struct buffer
[MarshalAs( UnmanagedType.AnsiBStr, SizeConst=50 )]
internal string foo;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=49)]
internal barString bar[];
[MarshalAs (UnmanagedType.I2)]
internal int status;

private static extern void MyApi(buffer buf);

That is, in order for me to create a 49-element array of 100-byte strings
(bar), I first need to create a struct that's a 100-byte string, and then I
need to create an array of that struct that is dimensioned to 49 elements.

This seems awfully complicated. (Also, the real data structure is much more
complex than this, containing many arrays.) Is this really the right way to
do this?

In addition, this code:

buffer buf;
foreach (barString oneBar in
{ = "";

fails in the assignment statement with the message "The left-hand side of an
assignment must be a variable, property, or indexer." This leads me to
suspect that I'm not using structs properly, but for the life of me I can't
see why not.

Any ideas and pointers would be much appreciated. Thanks.

Bob Rossney Removed)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Access from function in struct (pointer to function) to other items in the struct (advanced) Ole C Programming 4 10-26-2004 09:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Issues with declaring struct arrays inside of a struct Richard Mathis ASP .Net Building Controls 0 10-10-2003 03:14 PM