2

For reference, see the source code for this small program, EndPointController.exe: http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/

Basically, it is a Visual Studio C++ program that is using a printf function to write information to a command shell window.

Here's an example of me running the program on Windows 7 x64 (using the provided compiled binary from the above link):

C:\Users\James\Desktop>EndPointController.exe
Audio Device 0: Speakers (High Definition Audio Device)
Audio Device 1: AMD HDMI Output (AMD High Definition Audio Device)
Audio Device 2: Digital Audio (S/PDIF) (High Definition Audio Device)
Audio Device 3: Digital Audio (S/PDIF) (High Definition Audio Device)

C:\Users\James\Desktop>

This works perfectly. Now, I'll try to redirect the output to a file:

C:\Users\James\Desktop>EndPointController.exe > test.txt

C:\Users\James\Desktop>type test.txt

C:\Users\James\Desktop>

It didn't work; test.txt is empty. Is it a permissions issue?

C:\Users\James\Desktop>dir > test.txt

C:\Users\James\Desktop>type test.txt
 Volume in drive C has no label.
 Volume Serial Number is 16EC-AE63

 Directory of C:\Users\James\Desktop

04/20/2014  03:11 AM    <DIR>          .
04/20/2014  03:11 AM    <DIR>          ..
05/31/2011  06:16 PM             7,168 EndPointController.exe
04/20/2014  03:12 AM                 0 test.txt
               2 File(s)          7,168 bytes
               3 Dir(s)  171,347,292,160 bytes free

C:\Users\James\Desktop>

No, it does not seem to be a permissions issue. Can anyone explain how this printf function is somehow circumventing the standard out redirection process?

James
  • 716
  • 1
  • 9
  • 23
  • 2
    My guess is that the runtime library is not exiting normally, though I don't know why. At any rate, if this is the problem, you can work around it by explicitly flushing the stdout buffer or by turning off buffering. See http://stackoverflow.com/a/1716621/886887 – Harry Johnston Apr 20 '14 at 08:50

2 Answers2

3

It appears that the output buffer isn't being flushed when the program exits for some reason.

Adding fflush(stdout); right before the return hr; line fixes it for me.

I tried a few other things, like converting the wide string to narrow and passing that to printf, using wprintf, and compiling as multibyte and converting the string to narrow to pass to printf but only manually flushing the buffer worked.

Retired Ninja
  • 4,343
  • 3
  • 21
  • 35
1

I have tried downloaded the project files from the link you included and run the executable that is already built and included in the Release folder and it works as expected. I also re-built the code in VC++2013 and that too works as expected.

I suspect either operator error or some system issue - the information in your question does not seem to suggest operator error however; you have provided evidence that this is not teh case.

I ran the code from C:\Users\<userprofile>\Documents\Visual Studio 2013\test\Release. Desktop is a "special" folder in Windows which may have some bearing, though I doubt it. Either way, I don't think it is a programming issue.

Clifford
  • 76,825
  • 12
  • 79
  • 145
  • Can you give more information about your environment? – James Apr 20 '14 at 15:36
  • @James: Windows 8.1, 64-bit, administrator rights. Not sure what else you might need to know. – Clifford Apr 20 '14 at 16:55
  • Windows 7 Ultimate x64 and both the supplied executable and the one I built with no changes didn't work. I write/use a lot of console apps and have never seen this behavior before. – Retired Ninja Apr 20 '14 at 22:28