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

WAVEOUTCAPS problem

Hi,

I trying to get something that seems to be simple to work: I’m using VS
2005/C#. I want to get the information of my WaveOut devices. The WAVEOUTCAPS
structure is defined in C++ as:

typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
TCHAR szPname[MAXPNAMELEN];
DWORD dwFormats;
WORD wChannels;
WORD wReserved1;
DWORD dwSupport;
} WAVEOUTCAPS;

In C# I declare it as:

public struct WAVEOUTCAPS
{
public ushort wMid;
public ushort wPid;
public ulong vDriverVersion;
public char[] szPname;
public ulong dwFormats;
public ushort wChannels;
public ushort wReserved1;
public ulong dwSupport;
}

And later in the code:

WAVEOUTCAPS waveinfo = new WAVEOUTCAPS();
waveinfo.szPname = new char[32];

the waveOutGetDevCaps function is declared as

[DllImport("winmm.dll")]
public static extern int waveOutGetDevCaps(int uDeviceID, ref WAVEOUTCAPS
lpCaps, int uSize);

everything up to here compiles and runs fine. As soon as I call

waveOutGetDevCaps(0, ref waveinfo, Marshal.SizeOf(waveinfo));

my program crashes with the error:

System.Runtime.InteropServices.SafeArrayTypeMismat chException was unhandled
Message="Specified array was not of the expected type."

Any ideas what I’m doing wrong?

Jul 8 '07 #1
9 3616
On Sun, 08 Jul 2007 14:00:01 -0700, cnickl
<cn****@discussions.microsoft.comwrote:
[...]
my program crashes with the error:

System.Runtime.InteropServices.SafeArrayTypeMismat chException was
unhandled
Message="Specified array was not of the expected type."

Any ideas what I’m doing wrong?
Maybe you need to explicitly call the Unicode version? The error sure
seems like something that would come up if you tried to pass a C# Unicode
character array to a Win32 ANSI-version function.

I think that the p/invoke stuff should have a way to specify that, but if
not I suppose you could just use "waveOutGetDevCapsW" explicitly.

Pete
Jul 8 '07 #2
The problem is that the OP has an array declaration but doesn't specify
that it should be an inline array like the original structure. The array
declaration should be:

[StructLayout(LayoutKind.Sequential)]
public struct WAVEOUTCAPS
{
public ushort wMid;
public short wPid;
public ulong vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAXPNAMELEN)]
public string szPname;
Jul 8 '07 #3
On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
The problem is that the OP has an array declaration but doesn't
specify that it should be an inline array like the original structure.
Ahh. I see that now.

Is it safe to assume that p/invoke will always match up the correct TCHAR
version of a Win32 function? Or are there situations in which that is
actually an issue and needs to be addressed explicitly?
Jul 8 '07 #4
It is dependent on the value set to the CharSet property of the
StructLayout attribute assigned to the structure.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> The problem is that the OP has an array declaration but doesn't
specify that it should be an inline array like the original structure.

Ahh. I see that now.

Is it safe to assume that p/invoke will always match up the correct TCHAR
version of a Win32 function? Or are there situations in which that is
actually an issue and needs to be addressed explicitly?
Jul 9 '07 #5
Thanks. Your post with the sample code was very helpful. I would have never
thought about this.

"Nicholas Paldino [.NET/C# MVP]" wrote:
It is dependent on the value set to the CharSet property of the
StructLayout attribute assigned to the structure.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
The problem is that the OP has an array declaration but doesn't
specify that it should be an inline array like the original structure.
Ahh. I see that now.

Is it safe to assume that p/invoke will always match up the correct TCHAR
version of a Win32 function? Or are there situations in which that is
actually an issue and needs to be addressed explicitly?
Jul 9 '07 #6
On Sun, 08 Jul 2007 17:35:05 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
It is dependent on the value set to the CharSet property of the
StructLayout attribute assigned to the structure.
I didn't see that attribute in the original post. That's my point. Can
one rely on the default behavior? Is there a reliable default behavior?
Or is a structure definition without that attribute a bug waiting to be
found?
Jul 9 '07 #7
On Jul 8, 10:18 pm, cnickl <cni...@discussions.microsoft.comwrote:
Thanks. Your post with the sample code was very helpful. I would have never
thought about this.
In the future, you might find the pinvoke.net website useful. They
also have an add in to make it easy to get pinvoke signatures directly
from VS.

Chris

Jul 9 '07 #8
Well, I would say that it is a bug. Depending on what version of the
API the OP is going to make the call for, it is up to the OP to set that
flag. Generally, when using structures for A or W versions of APIS which
have embedded strings in them which need to be marshaled (using the
ByValTStr UnmanagedType), it would be erroneous to not include the CharSet
property on the attribute definition.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 08 Jul 2007 17:35:05 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> It is dependent on the value set to the CharSet property of the
StructLayout attribute assigned to the structure.

I didn't see that attribute in the original post. That's my point. Can
one rely on the default behavior? Is there a reliable default behavior?
Or is a structure definition without that attribute a bug waiting to be
found?

Jul 9 '07 #9
On Mon, 09 Jul 2007 08:14:07 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
Well, I would say that it is a bug. Depending on what version of the
API the OP is going to make the call for, it is up to the OP to set that
flag. Generally, when using structures for A or W versions of APIS which
have embedded strings in them which need to be marshaled (using the
ByValTStr UnmanagedType), it would be erroneous to not include the
CharSet
property on the attribute definition.
Okay...thanks! Good to know. :)
Jul 9 '07 #10

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.