473,385 Members | 1,464 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,385 software developers and data experts.

Interfacing with an API - structs with arrays of structs.

The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct that contains an array of other structs.

typedef struct
{
int nWidth;
int nHeight;
DWORD dwFlags;
} HCA_MODE;

typedef struct
{
DWORD dwSize;
int nCount;
HCA_MODE Mode[16];
} HCA_MODE_INFO;

HRESULT CamPreviewGetModeInfo(HANDLE hCamera,HCA_MODE_INFO *pModeInfo);

This is my attempt...

[StructLayout(LayoutKind.Sequential)]
public struct HCA_MODE
{
int nWidth;
int nHeight;
long dwFlags;
};

[StructLayout(LayoutKind.Sequential)]
public class HCA_MODE_INFO
{
long dwSize; // Size of the structure
int nCount; // Number of modes
HCA_MODE[] Mode = new HCA_MODE[16];
} ;

[DllImport("mobilecamapi.dll",EntryPoint="CamPrevie wGetModeInfo")]
public static extern int CamPreviewGetModeInfo(int hCamera, ref HCA_MODE_INFO pModeInfo);

It returns with a 'not supported exception' which I think is a euphemism for you got it the API declaration wrong. I know this API is supported.

Because the struct HCA_MODE_INFO contains a 16 element array I had to declare it as a class in C# since structs don't seem to like having arrays in them. I doubt whether passing a class around is the same as passing a struct.

The documentation seems a little vague about this area. Does anyone know what I am supposed to do?

Also, is a C DWORD a long in C# and a WORD an int?

Regards
Dave A



Nov 22 '05 #1
1 1550
Even if they are manipulated as pointers, arrays in C are not pointers: they are arrays and are stored as arrays. You have three solutions: go into unsafe mode and use pointers to allocate the necessary memory, use C++/Managed or try something like this:

typedef struct
{
uint dwSize;
int nCount;

int nWidth0;
int nHeight0;
uint dwFlags0;

int nWidth1;
int nHeight1;
uint dwFlags1;

........

int nWidth15;
int nHeight15;
uint dwFlags15;

} HCA_MODE_INFO;

You will probably find a better way to express this.

DWORD are 32 bits unsigned integer; this is the same thing as « unsigned int » or « unsigned long » in C because both int and long are 32 bits in C but in C#, this is an « uint » or System.UInt32; not a long. Long in C# are 64 bits. WORD are 16 bits or « ushort » or System.UInt16.

S. L.
"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:ON**************@TK2MSFTNGP11.phx.gbl...
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct that contains an array of other structs.

typedef struct
{
int nWidth;
int nHeight;
DWORD dwFlags;
} HCA_MODE;

typedef struct
{
DWORD dwSize;
int nCount;
HCA_MODE Mode[16];
} HCA_MODE_INFO;

HRESULT CamPreviewGetModeInfo(HANDLE hCamera,HCA_MODE_INFO *pModeInfo);

This is my attempt...

[StructLayout(LayoutKind.Sequential)]
public struct HCA_MODE
{
int nWidth;
int nHeight;
long dwFlags;
};

[StructLayout(LayoutKind.Sequential)]
public class HCA_MODE_INFO
{
long dwSize; // Size of the structure
int nCount; // Number of modes
HCA_MODE[] Mode = new HCA_MODE[16];
} ;

[DllImport("mobilecamapi.dll",EntryPoint="CamPrevie wGetModeInfo")]
public static extern int CamPreviewGetModeInfo(int hCamera, ref HCA_MODE_INFO pModeInfo);

It returns with a 'not supported exception' which I think is a euphemism for you got it the API declaration wrong. I know this API is supported.

Because the struct HCA_MODE_INFO contains a 16 element array I had to declare it as a class in C# since structs don't seem to like having arrays in them. I doubt whether passing a class around is the same as passing a struct.

The documentation seems a little vague about this area. Does anyone know what I am supposed to do?

Also, is a C DWORD a long in C# and a WORD an int?

Regards
Dave A



Nov 22 '05 #2

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

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
2
by: Michiel Rapati-Kekkonen | last post by:
recently I was put on the right trail in the matter of searching in arrays of structs. I got it working, a bit. Unfortunately, as soon as I want more, I'm stuck again: it is basically a...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
3
by: Nyx18 | last post by:
what im trying to do is read in a data from a file into 4 different arrays then also read in the same data using array of structs. the assignment is to show that we know how to use both methods of...
2
by: =?Utf-8?B?U2V0aEluTUk=?= | last post by:
I am a total newb at .net, and I have not been able to search out a best practice answer to what must be a common problem. My app must process binary data from a UDP socket, a MSMQ queue and a...
4
by: KioKrofov | last post by:
Currently I have (a much bigger version of) the code below in my main cpp file: struct myData { int dataCodeNum; char* dataName; } myData arrayOfData1={ { 1, ...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.