有关此小程序的源代码,请参见EndPointController.exe:http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/。
基本上,它是一个Visual C++程序,它使用printf
函数将信息写入命令shell窗口。
下面是我在Windows7 x64上运行程序的一个例子(使用上面链接提供的编译后的二进制文件):
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>
这工作得很好。现在,我将尝试将输出重定向到一个文件:
C:\Users\James\Desktop>EndPointController.exe > test.txt
C:\Users\James\Desktop>type test.txt
C:\Users\James\Desktop>
它没有工作;test.txt
是空的。这是权限问题吗?
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>
不,这似乎不是权限问题。有人能解释一下这个printf
函数是如何绕过标准的out重定向过程的吗?
发布于 2014-04-20 09:31:04
由于某种原因,当程序退出时,输出缓冲区似乎没有被刷新。
在fflush(stdout);
行之前添加return hr;
,为我修复它。
我尝试了其他几种方法,比如将宽字符串转换为窄字符串,将其传递给printf,使用wprintf,将字符串编译为多字节,将字符串转换为窄字符串以传递到printf,但只能手动刷新缓冲区。
发布于 2014-04-20 14:47:41
我已经尝试从您包含的链接下载项目文件,并运行已经构建并包含在Release文件夹中的可执行文件,并且按预期工作。我还在VC++2013中重新构建了代码,它也如预期的那样工作。
我怀疑操作符错误或系统问题--但是,问题中的信息似乎并不意味着操作符错误;您已经提供了证据,说明情况并非如此。
我运行了C:\Users\<userprofile>\Documents\Visual Studio 2013\test\Release
的代码。Desktop
是Windows中的一个“特殊”文件夹,虽然我对它表示怀疑,但它可能有一定的意义。不管怎样,我不认为这是一个编程问题。
https://stackoverflow.com/questions/23179931
复制相似问题