1

I'm trying to iterate all my Playbackdevices on my PC, using the Naudio library.

The code is really straight forward:

for (int i = 0; i < WaveOut.DeviceCount; i++)
     {
         WaveOutCapabilities WOC = WaveOut.GetCapabilities(i);
         MessageBox.Show(WOC.ProductName);
     }

Though, I'm having a problem where a const int within the struct basically is cutting off the ProductName-string if it has more than 32 characters, which is quite annoying.

So I'm wondering how I could change this 32-character-length to a bigger one, which would fit every ProductName. The only thing I could think of would be to override the whole struct (or just the number 32, to a bigger one), but it cannot be done. How would I go about changing this value?

You can see the struct here (it's the private const int MaxProductNameLength = 32; that needs to be changed)

I've never have to done anything like this before, and I'm wondering whether it even is possible.

Tokfrans
  • 1,939
  • 2
  • 24
  • 56
  • 1
    The easiest way would be to download the source for Naudio and compile it for yourself. – DavidG Jul 07 '15 at 00:26
  • @DavidG Ah, yes. That's probably what I will have to do. But got really curious about if you actually could do this. – Tokfrans Jul 07 '15 at 00:29
  • 1
    Well it all depends on whether the Windows API can handle a larger value than 32. It looks to be pretty well defined in the docs. – DavidG Jul 07 '15 at 00:31
  • 1
    I was just going to post the same thing as David, this is probably an OS limitation, not due to NAudio. – Ron Beyer Jul 07 '15 at 00:32
  • I don't think you can modify this and have it work because it calls an underlying windows api which uses the same constant. See [this](http://stackoverflow.com/questions/1429143/get-the-full-audio-device-name-from-windows) and [this](http://stackoverflow.com/questions/1449162/get-the-full-name-of-a-wavein-device). – Mike Zboray Jul 07 '15 at 00:33
  • @DavidG Yeah maybe so. Though that would be strange since I can see everything when browsing for them myself. But probably true. – Tokfrans Jul 07 '15 at 00:35
  • Looks like @mikez has hit the nail on the head. However, my comment still stands. You could create a pull request for Naudio and add in your own bits to get the extended names using the linked questions. – DavidG Jul 07 '15 at 00:37
  • Yes. Looks like it. Funny though, that the 'founder' of Naudio asked the same question as I did just now. (see 2nd linked question from mike). I'll have to do some serious reading up on the whole interop-thingy. Will probably come in handy later on I would imagine. – Tokfrans Jul 07 '15 at 00:39

1 Answers1

0

You can't. A const value is evaluated at compile time. By the time your code gets to interact with the library, it's too late.

Note that even if you could change the const value, its use in the library is also evaluated at compile time. I.e. the [MarshalAs] attribute where it's used has already been determined. So changing the const value wouldn't have any effect on how that field is marshaled.

As some commenters have noted, with some extensive modification of the original library (which is open source?), you might be able to support longer names, though still not in a way that is visible to the underlying platform API.

Alternatively, you could just add your own wrapper around it. I.e. use the 32 characters allowed in the library to store an integer value as a string, and then elsewhere map that string-formatted integer to a longer string as necessary and back to the string-formatted integer from the longer string, also as necessary.

You could generate the integers with a simple static int field that you increment every time you need a new value (i.e. any time you try to map from a longer string to a string-formatted integer and fail to find a match).

I.e. when making calls into the library, map from your own code's longer name to the string-formatted integer. When receiving information back from the library that uses the string-formatted integer, map that back to your own code's longer name. You can use dictionaries for both directions.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136