473,405 Members | 2,344 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,405 software developers and data experts.

Convert a C struct to C# (an interop problem)

I'm triyng to use this function
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

Docs say that pSI is a struct (below)
[c++]
typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
CHAR szPname[MAXPNAMELEN]; // Here's the problem
WORD wTechnology;
WORD wVoices;
WORD wNotes;
WORD wChannelMask;
DWORD dwSupport;
} MIDIOUTCAPS;
This is my code in c#
[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
public char[] szPname; // Here's the problem
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

MIDIOUTCAPS pSI = new MIDIOUTCAPS();
pSI.szPname = new char[32];
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

This code, give me a nullreference error (the callback stop in the
Kernel32.dll)

If i change "public char[] szPname; X public char szPname;" works fine
(don't retrieve the szPname) (I rename too, of course, pSI.szPname = new
char[32];)

"CHAR szPname[MAXPNAMELEN];" is a null-terminated string

Some idea?
Nov 15 '05 #1
6 14903
A small question should be "How can i dimension a char 'in line' (in a
structure)"
"~toki" <pedorro77.hotmail.com> escribió en el mensaje
news:uJ**************@tk2msftngp13.phx.gbl...
I'm triyng to use this function
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

Docs say that pSI is a struct (below)
[c++]
typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
CHAR szPname[MAXPNAMELEN]; // Here's the problem
WORD wTechnology;
WORD wVoices;
WORD wNotes;
WORD wChannelMask;
DWORD dwSupport;
} MIDIOUTCAPS;
This is my code in c#
[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
public char[] szPname; // Here's the problem
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

MIDIOUTCAPS pSI = new MIDIOUTCAPS();
pSI.szPname = new char[32];
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

This code, give me a nullreference error (the callback stop in the
Kernel32.dll)

If i change "public char[] szPname; X public char szPname;" works fine
(don't retrieve the szPname) (I rename too, of course, pSI.szPname = new
char[32];)

"CHAR szPname[MAXPNAMELEN];" is a null-terminated string

Some idea?

Nov 15 '05 #2
Hi ~toki, it's simple

[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public char[] szPname;
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

Regards,
~toki
:>)

"~toki" <pedorro77.hotmail.com> escribió en el mensaje
news:%2***************@TK2MSFTNGP11.phx.gbl...
A small question should be "How can i dimension a char 'in line' (in a
structure)"
"~toki" <pedorro77.hotmail.com> escribió en el mensaje
news:uJ**************@tk2msftngp13.phx.gbl...
I'm triyng to use this function
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

Docs say that pSI is a struct (below)
[c++]
typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
CHAR szPname[MAXPNAMELEN]; // Here's the problem
WORD wTechnology;
WORD wVoices;
WORD wNotes;
WORD wChannelMask;
DWORD dwSupport;
} MIDIOUTCAPS;
This is my code in c#
[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
public char[] szPname; // Here's the problem
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

MIDIOUTCAPS pSI = new MIDIOUTCAPS();
pSI.szPname = new char[32];
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

This code, give me a nullreference error (the callback stop in the
Kernel32.dll)

If i change "public char[] szPname; X public char szPname;" works fine
(don't retrieve the szPname) (I rename too, of course, pSI.szPname = new
char[32];)

"CHAR szPname[MAXPNAMELEN];" is a null-terminated string

Some idea?


Nov 15 '05 #3
Hi, Toki

I met the problem before, but slightly different from yours.
Looks like the definition of character array inside of struct MIDIOUTCAPS is supposed to specify an array size. You just declared a char array. This will not work in C#.
Nov 15 '05 #4
Try this layout.
A WORD is 16 bit (short), not 32(int). A DWORD is 32 bit(ing), not 64
(long).
Make sure to check MMVERSION and use long for 64 bit or int for 32 bit.

[C#] [StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct MIDIOUTCAPS {
public short wMid;
public short wPid;
public long vDriverVersion; // is MMVERSION a 64 bit number?
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]
public string szPname;
public short wTechnology;
public short wVoices;
public short wNotes;
public short wChannelMask;
public int dwSupport;
}

Chris R.

"~toki" <pedorro77.hotmail.com> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl... I'm triyng to use this function
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

Docs say that pSI is a struct (below)
[c++]
typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
CHAR szPname[MAXPNAMELEN]; // Here's the problem
WORD wTechnology;
WORD wVoices;
WORD wNotes;
WORD wChannelMask;
DWORD dwSupport;
} MIDIOUTCAPS;
This is my code in c#
[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
public char[] szPname; // Here's the problem
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

MIDIOUTCAPS pSI = new MIDIOUTCAPS();
pSI.szPname = new char[32];
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

This code, give me a nullreference error (the callback stop in the
Kernel32.dll)

If i change "public char[] szPname; X public char szPname;" works fine
(don't retrieve the szPname) (I rename too, of course, pSI.szPname = new
char[32];)

"CHAR szPname[MAXPNAMELEN];" is a null-terminated string

Some idea?

Nov 15 '05 #5
I found the same solution that you post.
It works now.
But i go to check the long, int & shorts.
Thanks,,
"Chris R" <so*****@hotmail.com> escribió en el mensaje
news:ut**************@TK2MSFTNGP09.phx.gbl...
Try this layout.
A WORD is 16 bit (short), not 32(int). A DWORD is 32 bit(ing), not 64
(long).
Make sure to check MMVERSION and use long for 64 bit or int for 32 bit.

[C#]

[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct MIDIOUTCAPS {
public short wMid;
public short wPid;
public long vDriverVersion; // is MMVERSION a 64 bit number?
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]
public string szPname;
public short wTechnology;
public short wVoices;
public short wNotes;
public short wChannelMask;
public int dwSupport;
}

Chris R.

"~toki" <pedorro77.hotmail.com> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
I'm triyng to use this function
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

Docs say that pSI is a struct (below)
[c++]
typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
CHAR szPname[MAXPNAMELEN]; // Here's the problem
WORD wTechnology;
WORD wVoices;
WORD wNotes;
WORD wChannelMask;
DWORD dwSupport;
} MIDIOUTCAPS;
This is my code in c#
[C#]
public struct MIDIOUTCAPS {
public int wMid;
public int wPid;
public long vDriverVersion;
public char[] szPname; // Here's the problem
public int wTechnology;
public int wVoices;
public int wNotes;
public int wChannelMask;
public long dwSupport;
}

MIDIOUTCAPS pSI = new MIDIOUTCAPS();
pSI.szPname = new char[32];
int result = NativeMethods.midiOutGetDevCaps( -1, ref pSI,
(uint)Marshal.SizeOf(pSI) );

This code, give me a nullreference error (the callback stop in the
Kernel32.dll)

If i change "public char[] szPname; X public char szPname;" works fine
(don't retrieve the szPname) (I rename too, of course, pSI.szPname = new
char[32];)

"CHAR szPname[MAXPNAMELEN];" is a null-terminated string

Some idea?


Nov 15 '05 #6
Hi,

here is my code:

public short wMid;
public short wPid;
public uint vDriverVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public char[] szPname;
public int dwFormats;
public short wChannels;
public short wReserved1;

It works fine!
Good luck with your project

Bye

Guillaume
http://www.ece.fr/~gbonnet
May 15 '06 #7

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

Similar topics

4
by: quek | last post by:
Im having a problem passing a struct ptr. Maybe someone could help me. Heres what Visual C gives me as error: error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct main::$S1 *'...
2
by: TJO | last post by:
Hello, What is the easiest way to convert a struct containing string members to a byte? THanks
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
22
by: glenn | last post by:
I have a COM Server that I've written based on information from the book ..NET and COM / the complete Interop Guide. I have gotten the project to compile and I've located the regasm.exe program...
8
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be...
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
4
by: Yoavo | last post by:
Hi, I want to convert a string to double. I use the function: "System.Convert.ToDouble". The problem is that if the string contains the character "." the program aborts. What might be the...
1
by: PLS | last post by:
I'm trying to write the definition of a struct that must exactly match the binary layout of a struct created by a native c++ program. The C++ struct contains several wchar_t and char fields...
1
by: Paul Jarvis | last post by:
I have a large structure, below is a simplistic version of my structure: public struct MeanMinMaxSd { public double mean; public double min; public double max; public double sd; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.