AttachConsole() - Console.WriteLine() doesn't work sometimes

I know, that this question has been stated before - but I didn't find any answer.
Let's take this simple example:
static class Program
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AttachConsole([MarshalAs(UnmanagedType.U4)] int dwProcessId);
[STAThread]
static void Main()
if (AttachConsole(-1))
Console.WriteLine(@"AttachConsole() succeeded!");
else
MessageBox.Show(@"AttachConsole() failed!");
When I start cmd.exe (For example by Run) - it works - I get "AttachConsole() succeeded".
When I start it in any named window (e.g. Developer Command Prompt) - I get nothing. No console output, no message box. Moreover - when I run Marshal.GetLastWin32Error() - I get 2 (File not found).
Is there any way to lose this problem?

Thanks for the tip. I tried this, I tried also the same procedure for standard error, but I haven't noticed any dfference in the application behavior.
I made some further tests only to confirm my suspition, that the stdout/stderr redirection in console window seems not to be working correctly under Window 7 (probably other versions as well).
Once again some code. First C#, to be compiled as WindowsApplcation with the name app.exe.
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication12
internal static class Program
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AttachConsole([MarshalAs(UnmanagedType.U4)] int dwProcessId);
[STAThread]
private static void Main(string[] args)
if (AttachConsole(-1))
if (args.Length > 0)
Console.WriteLine(args.Aggregate((x,y) => x + " " + y));
else
MessageBox.Show(@"AttachConsole() failed!");
then the batch file for the testing
@echo off
echo Start
start /wait app App call Start
call :BatchLevel1
start /wait app App call Finito
goto End
:BatchLevel1
echo in BatchLevel1
start /wait app App call Level 1
call :BatchLevel2 > nul 2>&1
echo out BatchLevel1
exit /B 0
:BatchLevel2
echo in BatchLevel2
start /wait app App call Level 2
call :BatchLevel3 > nul 2>&1
echo out BatchLevel2
exit /B 0
:BatchLevel3
echo in BatchLevel3
start /wait app App call Level 3
echo out BatchLevel3
exit /B 0
:end
echo Finito
I used start /wait calls to make the output synchronous.
What I have noticed, is bizzare behavior of console outputs. What at first glance seems to be unpredictable, builds in reality a strange pattern. Here are the outputs of three sequential calls of the batch file:
Start
App call Start
in BatchLevel1
App call Level 1
App call Level 2
App call Level 3
out BatchLevel1
Finito
Start
in BatchLevel1
App call Level 2
App call Level 3
out BatchLevel1
App call Finito
Finito
Start
App call Start
in BatchLevel1
App call Level 1
out BatchLevel1
App call Finito
Finito
The echo outputs from the entry point and level 1 are always and the outputs from level 2 and 3 are never present (as expected). The application seems to ignore the stdout redirection, but not for all calls. I can see some kind of looping - the results
of the 4th call are the same as those from the 1st call, 5th are the same as the 2nd and so on.
Even more interesting is the situation when I remove two stderr redirections (2>&1). When I start a fresh console I can see the "right" output:
Start
App call Start
in BatchLevel1
App call Level 1
out BatchLevel1
App call Finito
Finito
But in the console window when some calls have been already made this output can be another:
Start
in BatchLevel1
App call Level 2
App call Level 3
out BatchLevel1
Finito
What is also interesting to notice, without stderr redirection the batch output is always the same.
I will try to perform some more tests, but for me it seems to be an application error in cmd.exe...

Similar Messages

  • My safari doesn't work sometimes, but I connect with the internet.

    my safari doesn't work sometimes, but I connect with the internet. Some of the videos I can't see with the Safari browser, but I can see it with other brower. But my friend can see these videos with her safari. And I can not connect with the app store sometimes.

    Before you try to configure the settings on your Router, first thing which you need to do is, Press and hold the reset button for 60 seconds...Release the reset button...Unplug the power cable from your router, wait for 60 seconds and re-connect the power cable...
    Connect the Modem to the Linksys Router on the Internet Port and then connect your computer to the Linksys router on the LAN Port 1. On your Computer click on Start - Run - CMD and click Ok... Now in the Command Prompt window type "ipconfig" and hit enter and note down the "IP address, Subnet Mask and Default Gateway" , Now again in the Command Prompt window type "ping default gateway IP" and hit enter and check if you are getting any replies, If yes then you can login to the setup page of the configure all the settings on it from scratch. 
    If your Internet Service Provider is Cable follow this link

  • HT201401 my sleep button doesn't work sometimes

    there is a problem with my sleep button,it doesn't work sometimes
    can someone tell me how to rectify that problem

    Backup and restore your device. If still having problem, then its a hardware issue.

  • I have install io6 and now Music doesn't work, sometimes it stay with a blank page with no possibility to press anithing, sometimes it crash. I have match music active with more than 15000 title. Someone could help me?

    I have install io6 and now Music doesn't work, sometimes it stay with a blank page with no possibility to press anithing, sometimes it crash. I have match music active with more than 15000 title. Someone could help me?

    What is your current Flash Player version: http://helpx.adobe.com/flash-player/kb/installation-problems-flash-player-windows.html ?
    What is your display adapter, driver version & date?  See http://forums.adobe.com/thread/945765

  • I just downloaded ios 7 and when I'm recording video the camera screen goes black and doesn't record anything. Also in control centre music control doesn't work sometimes. It's been three days.

    I just downloaded ios 7 and when I'm recording video the camera screen goes black and doesn't record anything. Also in control centre music control doesn't work sometimes. It's been three days.

    Try:
    - Reset the iOS device. Nothing will be lost
    Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Reset all settings      
    Go to Settings > General > Reset and tap Reset All Settings.
    All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
    - Restore from backup. See:                                 
    iOS: How to back up                                                                
    - Restore to factory settings/new iOS device.      
    If still problem, make an appointment at the Genius Bar of an Apple store since it appears you have a hardware problem.
    Apple Retail Store - Genius Bar                       

  • [kde]Gamin doesn't work sometimes.

    Hi all!
    I've read arch wiki and found, that gamin is lighter and better than fam.
    But after installing gamin dolphin do not show actual file system content. If I  move a folder it doesn't show it until manually refreshing (or by F5).  Sometimes it is working, sometimes no.
    I'm using KDE4.6. Do not remember about 4.5.
    Could anyone suggest the solution?
    Last edited by vit (2011-02-03 07:42:11)

    KDE should not need neither fam nor gamin. It directly uses kernel's inotify to monitor filesystem changes.

  • HT1430 The home button doesn't work sometimes.  Am I using the wrong pressure?

    The home button doesn't response sometimes.  Is there any technique?  Am I using the wrong pressure?  Please help

    This may or may not work; no harm trying.
    Calibrate the Home button
    1. Open a pre-installed app, such as Contacts
    2. Press and hold the sleep/wake button until the red slider appears
    3. Immediately release the sleep/wake button; press and hold the Home button.
    4. Hold until the red slider disappears.

  • Iphone 5 auto screen-off doesn't work sometimes

    it holds on until you don't touch the screen again. My device has already setted 1 minute time for automatic block but sometimes it doesn't work.
    Anyone with the same problem?

    Same here, 1 time at least that i have notice.
    Btw and i had experience the oposite, example: when i press the home button or on/off button to unblock the screen sometimes i have to press more then 3 to 4 times to appear image and unlock....
    anyone???

  • I have Adobe Elements 13 on a Apple Mac Air. The color switch button doesn't work sometimes

    I have elements 13 on an apple mac but the color switch button sometimes doesn't work.

    It probably can't be turned on at all. I suspect that your liquid damage has finally died a corrosion death on your logic board.
    All you can do at this point is take the machine into your local Apple Store/AASP and have them diagnosis the problems and give you an estimate for repair.
    Clinton

  • To stop a thread doesn't work sometimes

    in my programm i have a thread, wenn i want interrupt the running thread i wrote this code: myThread.stop(), it works indeed, but not everytime. i cannot understand why it doesn't work, can somebody tell me why and how to handle it?
    I thank you in advance.

    if (a_Condition_you_have_set) myThread stop();
    is depricated, though it should still work and if I remember rightly it could cause the thread to keep running (or not stop properly) - so causing memory problems.
    You have to use boolean values eg;-
    public void start() {  // the thread
    conditions;
    run();
    public void run() {
    conditions(false or true);
    public void stop() {
    conditions(false or true);
    - its a standard method, perhaps someone could provide a link, though if you do a search for thread + whatever, you should find it easily enough.

  • TCP Read doesn't work sometimes

    Hi Everyone, I have a problem where my build .exe doesn't work on some computers but works fine on others. it uses a tcp connection to grab data off a custom server program which packages bluetooth data.
    The program build works fine on my development computer as well as another desktop, but on a 3rd computer the code simply doesn't read the data from the server. I've disabled windows firewall but i have no idea what else to try - anyone have this problem before?
    Solved!
    Go to Solution.

    That tells me there was a driver you did not install on the other computer.  LabVIEW 2013 tries to automatically figure out what drivers you need when you configure an installer.  This issue does not sound like it had anything to do with the TCP/IP.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Tabsontop console command doesn't work any more since v29.0.1

    I used to be able to put the tabs on the bottom where they belong using the console, and now this command doesn't do anything. I don't want to use Chrome, I want to use Firefox, just make it a default option instead of making me root around in console settings that don't even work any more.

    You do not need to install the Stylish extension to be able to do this.
    You can easily create a chrome folder in the Firefox profile folder and a userChrome.css file in this folder with the code.
    Note that you may need a higher number (CTR uses #TabsToolbar {-moz-box-ordinal-group: 100} and Tab on Bottom uses: #TabsToolbar {-moz-box-ordinal-group: 10000 !important;}) as there may still be toolbars around that use a higher value for this property.
    See also:
    *[[/questions/996959]]
    *[[/questions/997335]]
    Add code to the <b>userChrome.css</b> file below the default @namespace line.
    *http://kb.mozillazine.org/userChrome.css
    <pre><nowiki>@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* only needed once */
    #TabsToolbar {-moz-box-ordinal-group: 10000 !important;}
    </nowiki></pre>
    The customization files userChrome.css (user interface) and userContent.css (websites) are located in the <b>chrome</b> folder in the Firefox profile folder.
    *http://kb.mozillazine.org/Editing_configuration
    You can use this button to go to the currently used Firefox profile folder:
    *Help > Troubleshooting Information > Profile Directory: Show Folder (Linux: Open Directory; Mac: Show in Finder)

  • Business Catalyst admin console just doesn't work. Can't add domain..

    My admin console will not let me add a domain. The page loads and then disappears which is very odd. I have reset safari, emptied my cache ect and do not know what is wrong but I cannot add a domain which is very frustrating. My client wants the site up today and business catalyst is not allowing me to do that! Help please!!
    I have tried to click on 'manage site' and delete the site from BC and start over again but same thing happens here. I click on the tab, the page flashes up and then quickly changes to a page with no content showing. This is very very frustrating and I do not know what else to do....
    I have attached two screenshots to show you all what happens on my screen.
    Any help would like highly appreciated.
    Regards,
    Luke.

    Hi Luke,
    Hope this doesn't take away from your post but...
    Yesterday was only the first time I went to use Business Catalyst and the same happened to me (I think)... after following this prompt.. from dreamweaver..              Now I can't seem to get past this screen and have had no chance of finding "Business Catalyst web adminstration console"  also yesterday it would work slightly (some links were live) but no matter the screen I didn't have adminstrative authority.  Even though I'm the soul user!  
    Any chance, the site is just down for "upkeeping" or something?
    Sincerely,
    Shannon

  • Shutdown doesn't work sometimes

    Hello!
    Sometimes when I try to shutdown nothing happens. Gnome stops and the message "System is going down for halt now" appears, but then nothing happens. I can change to every vc I want, log on and also start gnome again and work normal. But the system won't halt, also after waiting a long time (5 hours).
    I couldn't find something interesting in the log files, but then I looked at each process which is running. And it seems that "mount.ntfs-3g" could not be exited. Also a "killall -s 9 mount.ntfs-3g" doesn't stop the process. In gnome-system-monitor the status column says "Uninterruptible".
    What's wrong? What can I do?
    Thanks

    Windows is not installed. It is an external hard disk (USB) with Virtual Machines on it. I use NTFS because else I can't share it with my Windows Friends (;-)). I have watched the process "mout.ntfs-3g" and when I unmount my disk it normally exits, but sometimes it hangs, as said in the first post.
    Next time when it hangs I will try to manually unload the fuse kernel module and watch if there are any errors. Until then thanks for the help.
    Last edited by sofa5000 (2008-04-09 17:10:15)

  • Pinching doesn't work sometime?

    Hello,
    Before sending feedback to Apple I wanted to make sure that this is not a hardware issue of my iPhone:
    Does anybody have the situation that pinching photos or web pages or whatever does not work fluidly? Sometimes on my phone it just zooms a bit and then stops although both fingers keep on moving. When this happens it is not possible to zoom out after zooming in within the same finger movement neither.
    Thanks for some info!

    I think I may have figured this out. It could be an issue with the accelerometer. Whenever I lay the device down flat, I encounter the same problem you described. However, the problem doesn't happen when I hold it upright, as it is meant to be used.
    Does this help?

Maybe you are looking for