473,625 Members | 3,384 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.AllocHG lobal(Marshal.S izeOf(wOutCaps) );
Marshal.Structu reToPtr(wOutCap s, waveCaps, true);
ReturnCode = waveOutGetDevCa ps((uint)Device ID, out waveCaps,
(uint)Marshal.S izeOf(wOutCaps) );
wOutCaps = (WaveOutCaps)Ma rshal.PtrToStru cture(waveCaps,
typeof(WaveOutC aps) ); // I GET HERE AN ERROR (OBJECT NOT SET TO INSTANCE OF
AN OBJECT)
Marshal.FreeHGl obal(waveCaps);

WaveOutCaps is a structure defined as follows:

[StructLayout(La youtKind.Sequen tial)]
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 2992
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*******@hotm ail.com> wrote in message
news:OJ******** ******@tk2msftn gp13.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.AllocHG lobal(Marshal.S izeOf(wOutCaps) );
Marshal.Structu reToPtr(wOutCap s, waveCaps, true);
ReturnCode = waveOutGetDevCa ps((uint)Device ID, out waveCaps,
(uint)Marshal.S izeOf(wOutCaps) );
wOutCaps = (WaveOutCaps)Ma rshal.PtrToStru cture(waveCaps,
typeof(WaveOutC aps) ); // I GET HERE AN ERROR (OBJECT NOT SET TO INSTANCE OF AN OBJECT)
Marshal.FreeHGl obal(waveCaps);

WaveOutCaps is a structure defined as follows:

[StructLayout(La youtKind.Sequen tial)]
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(La youtKind.Sequen tial)]
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(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct WaveOutCaps
{
public ushort wManufacturerID ;
public ushort wProductID;
public uint vDriverVersion;
[MarshalAs(Unman agedType.ByValT Str, 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("winm m.dll", CharSet=CharSet .Auto)]
static extern uint waveOutGetDevCa ps(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.o rg> wrote in message
news:e8******** ******@TK2MSFTN GP10.phx.gbl...
Ignacio,
[StructLayout(La youtKind.Sequen tial)]
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(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct WaveOutCaps
{
public ushort wManufacturerID ;
public ushort wProductID;
public uint vDriverVersion;
[MarshalAs(Unman agedType.ByValT Str, 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("winm m.dll", CharSet=CharSet .Auto)]
static extern uint waveOutGetDevCa ps(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
14690
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
72
5828
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
121
10012
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
51
13342
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 wrong????? Function x select case z
46
4194
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 believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5032
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
1460
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 http://www.ebay.com google http://www.google.com yahoo http://www.yahoo.com However in the drop down list displayed value used ebay http://www.yahoo.com
98
4565
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
2115
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
2804
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
8694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7184
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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 we have to send another system
2
1500
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.