By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

What's wrong with this code?

P: n/a
Hi. I'm writing an application and I'm getting a weird error in this code:

DeviceID = 0;
wOutCaps = new WaveOutCaps();
IntPtr waveCaps = Marshal.AllocHGlobal(Marshal.SizeOf(wOutCaps));
Marshal.StructureToPtr(wOutCaps, waveCaps, true);
ReturnCode = waveOutGetDevCaps((uint)DeviceID, out waveCaps,
(uint)Marshal.SizeOf(wOutCaps));
wOutCaps = (WaveOutCaps)Marshal.PtrToStructure(waveCaps,
typeof(WaveOutCaps) ); // I GET HERE AN ERROR (OBJECT NOT SET TO INSTANCE OF
AN OBJECT)
Marshal.FreeHGlobal(waveCaps);

WaveOutCaps is a structure defined as follows:

[StructLayout(LayoutKind.Sequential)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

I wonder why is this error showing up... Any ideas?

Thanks

Ignacio Domínguez
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have you checked that waveCaps is not 0, and that ReturnCode is a valid
successful return code?

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"Ignacio Domínguez" <ig*******@hotmail.com> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
Hi. I'm writing an application and I'm getting a weird error in this code:

DeviceID = 0;
wOutCaps = new WaveOutCaps();
IntPtr waveCaps = Marshal.AllocHGlobal(Marshal.SizeOf(wOutCaps));
Marshal.StructureToPtr(wOutCaps, waveCaps, true);
ReturnCode = waveOutGetDevCaps((uint)DeviceID, out waveCaps,
(uint)Marshal.SizeOf(wOutCaps));
wOutCaps = (WaveOutCaps)Marshal.PtrToStructure(waveCaps,
typeof(WaveOutCaps) ); // I GET HERE AN ERROR (OBJECT NOT SET TO INSTANCE OF AN OBJECT)
Marshal.FreeHGlobal(waveCaps);

WaveOutCaps is a structure defined as follows:

[StructLayout(LayoutKind.Sequential)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

I wonder why is this error showing up... Any ideas?

Thanks

Ignacio Domínguez

Nov 15 '05 #2

P: n/a
Ignacio,
[StructLayout(LayoutKind.Sequential)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

I wonder why is this error showing up... Any ideas?


First of all your struct declaration is incorrect. Try it like this
instead

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

Second, you seem to be doing a lot more work than you have to with the
Marshal class. If you declare the function correctly you shouldn't
have to do that, just pass in the struct variable you want to be
filled.

[DllImport("winmm.dll", CharSet=CharSet.Auto)]
static extern uint waveOutGetDevCaps(IntPtr uDeviceID, out WAVEOUTCAPS
pwoc, uint cbwoc);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3

P: n/a
thanks you so much. this worked perfectly. I've been having a lot of trouble
using API since I've never done any serious C (C++) coding. Perhaps you can
give me some advice on doing this type of declarations/conversions, maybe
also pointing to a place to translate C types into .NET. Thank you again.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
Ignacio,
[StructLayout(LayoutKind.Sequential)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

I wonder why is this error showing up... Any ideas?


First of all your struct declaration is incorrect. Try it like this
instead

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WaveOutCaps
{
public ushort wManufacturerID;
public ushort wProductID;
public uint vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szProductName;
public WaveFormats dwFormats;
public ushort wChannels;
public ushort wReserved1;
public uint dwSupport;
}

Second, you seem to be doing a lot more work than you have to with the
Marshal class. If you declare the function correctly you shouldn't
have to do that, just pass in the struct variable you want to be
filled.

[DllImport("winmm.dll", CharSet=CharSet.Auto)]
static extern uint waveOutGetDevCaps(IntPtr uDeviceID, out WAVEOUTCAPS
pwoc, uint cbwoc);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.