473,498 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this code?

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
3 2980
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14534
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
5733
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
9898
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
13286
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
4152
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
4996
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
1454
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
4485
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
2107
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
2793
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
7125
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
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
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
7379
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
5464
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
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.