WinApp with Console

WinApp with Console

 

    Hi

       I am writing a code that uses a 'c' library. That library has a lot of output to the console.How can I open the console in a Windows app to see those messages and how can I grab those messages and print it to a file or to a MessageBox (because console display it is not pretty in a windows app)?

 

   thanks

   

9 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione
Ritratto di app4619

You can use the  sdk routine attachconsole (you need the console process id) you can then  read and write to that console using the sdk WriteConsole and ReadConsole I have attached a code extract snippet...

BTW if you create the console from Fortran using CreateProcess with StartupInfo you can make the console look prettier by setting foreground and background colours etc. If you want I can post an example.

        use ifwin, only: bret
        use ifwinW, only: AttachConsole
        integer(bool)           :: bret
        bret = AttachConsole(ProcessInfo%dwProcessId)
        .....



module IfwinW
    !missing unicode interfaces from IFWIN +  other missing interfaces
    use ifwinty
    !DEC$OBJCOMMENT LIB:"USER32.LIB"
    !DEC$OBJCOMMENT LIB:"KERNEL32.LIB"
    interface 
        function AttachConsole(dwProcessId)
            import !if intel add this missing inteface we should then get an error
            integer(bool)  :: AttachConsole
            !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'AttachConsole' :: AttachConsole
            integer(dword) :: dwProcessId
            !DEC$ ATTRIBUTES VALUE :: dwProcessId
        end function AttachConsole
    end interface
END MODULE IfwinW

 

 

 

 

   Hi

       Thanks. If that is not too much work for you I would appreciate that also.

     thanks

   

 

Ritratto di app4619
    subroutine OpenCloseConsole(Iopen) !iopen=1 open, iopen=0 close
        use ifwin
        use IfwinW, only: AttachConsole
        use ifport, only: sleep, getlasterror
        
        implicit none
        integer, intent(in)     :: iopen
        integer(bool)           :: bret
        integer(handle)         :: fhandle
        integer(dword)          :: dwX, dwY, dwXSize, dwYSize ! initial size and position of console (pix/screen)
        Type(T_COORD)           :: wpos
        type (T_STARTUPINFO)               :: StartupInfo
        type (T_PROCESS_INFORMATION), save :: ProcessInfo

        !call cc_GetLastError()                     
        if(iopen == 0) then
            !BOOL WINAPI TerminateProcess(_In_  HANDLE hProcess,_In_  UINT uExitCode);
            bret = FreeConsole()   !close/Deallocate the console to free its resources.            
            !call cc_GetLastError()
            bret = TerminateProcess(ProcessInfo%hProcess,0)
            !call cc_GetLastError()
           return
        endif

        dwX=0; dwY=0; dwXSize=0; dwYSize=0 !doesn't seem to work
        ! Initialize StartupInfo iany([STARTF_USEFILLATTRIBUTE,STARTF_USEPOSITION,STARTF_USEPOSITION])
        StartupInfo = T_STARTUPINFO(C_SIZEOF(StartupInfo),NULL, NULL,                       &
                                        transfer(C_loc("User Console"C),fhandle),           & !initial name of console
                                                      dwX, dwY, dwXSize, dwYSize,           & ! initial position and size
                                                                       NULL,NULL,           & ! x and y count chars
             iany([FOREGROUND_BLUE,BACKGROUND_RED,BACKGROUND_GREEN,BACKGROUND_BLUE]),       & ! blue text on  white backgr
                                     ior(STARTF_USEFILLATTRIBUTE,STARTF_USEPOSITION),       & ! flags to tell items are present
                                                        NULL,NULL,NULL,NULL,NULL,NULL)
        ProcessInfo%dwProcessId=0 !init                                                
        ! now Open a custom console
        bret =  CreateProcess (NULL_CHARACTER,"cmd.exe"C, &   ! Application Name, cmdline
                    NULL_SECURITY_ATTRIBUTES, &
                    NULL_SECURITY_ATTRIBUTES, &
                    TRUE, &                                   ! InheritHandles
                    0, &                                      ! CreationFlags CREATE_NEW_CONSOLE
                    NULL, &                                   ! Environment variables
                    NULL_CHARACTER, &                         ! Current directory
                    StartupInfo, &
                    ProcessInfo)                              ! process id etc returned
        if(bret /= true) then
            !call cc_GetLastError()
        else
            call sleep(1000) !create process returns before proc fully running 
            !call SetLastError(0) !reset the non-error that always is happening set in creatproc
            bret = AttachConsole(ProcessInfo%dwProcessId)
            !call cc_GetLastError()
        endif

        bret = SetConsoleTitle("My Text Window"C) ! we know we have connected.....
        !if(bret /= true) call cc_GetLastError()  

        fhandle = GetStdHandle(STD_OUTPUT_HANDLE)
        !call cc_GetLastError()  

        ! Set buffer size variables
        wpos%x = 132   ! line length must be >= currently set console window line length
        wpos%y = 300   ! lines must be >= currently set console window number of lines
        ! Set a console buffer bigger than the console window. This provides
        ! scroll bars on the console window to scroll through the console buffer
        bret = SetConsoleScreenBufferSize(fhandle, wpos)
        !if(bret /= true) call cc_GetLastError()                 
    end subroutine OpenCloseConsole

The code above illustrates some points, it opens  console with attributes, closes it, set the title etc. The missing cc_  routines are just wrappers for the IFPORT routines

 

    Hi

      But, is there an alternative to write the console messages in a MessageBox instead of console itself?

How, specifically, does the C library do its IO?  printf and friends?

 Hi

    Some of the libraries use printf and 'friends'. Concerning another library I must ask first to PETSc development team because and still don't know how that is done.

 Hi

    I tried the code above but you didn't send gutils and asx6 modules.

 

   regards

 

Ritratto di app4619

Citazione:

P R. ha scritto:

 Hi

    I tried the code above but you didn't send gutils and asx6 modules.

 

   regards

 

 

You do not need them, I edited the source above, axs6 only declared fhandle varaiable, I replaced cc_sleep with the std sleep and commented out the calls to the error handler routines.

Accedere per lasciare un commento.