IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

Lire contenu console Windows


Sujet :

C++

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut Lire contenu console Windows
    Bonjour,

    Je cherche à lire le contenu d'une console Windows. Je programmerai au final l'ensemble en VBA, mais je pense avoir plus d'aide dans forum de C++. Hélas, je dois admettre ne même pas maitriser les rudiments.

    Ci-dessous ma première tentative qui ne fonctionne pas... Pouvez-vous m'aider à la corriger ? (j'imagine qu'il y aura aussi des problèmes de conversion ANSI, Unicode,... mais je pense que ça sera transparent en VBA)'

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    #include <windows.h>
    #include <iostream>
     
    int main()
    {
    	HANDLE hWnd= FindWindow("ConsoleWindowClass", "Command Prompt"); //bien sûr, j'ai pris le soin d'ouvrir une console Windows. J'ai vérifié avec Spy++ et le handle correspond bien à la console (pas d'autres consoles masquées,...)
     
    	char buffer[100];
    	DWORD read;
     
    	ReadConsole(hWnd, buffer, sizeof(buffer), &read, nullptr);
     
    	std::cout << buffer;
     
    	return 0;
    }
    Merci par avance !

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Une console ne se lit pas depuis le HWND de sa fenêtre.
    Perso j'ai ce code-là pour capturer le contenu d'une console (identifiée par le processId du processus qui la possède) et l'enregistrer sous forme de HTML:
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    /*CaptureConsoleCode.h*/
    #pragma once
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    BOOL SaveConsoleCaptureToFile(LPCTSTR filePath, CHAR_INFO const *outputChars, short cx, short cy);
    BOOL CaptureConsole(DWORD targetProcessId, CHAR_INFO **ppOutputChars, COORD *pSize);
    void FreeOutputChars(CHAR_INFO *pOutputChars);
    BOOL CaptureAndSaveConsole(DWORD targetProcessId, LPCTSTR filePath);
     
    #ifdef __cplusplus
    }
    #endif
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    /*CaptureConsoleCode.c*/
    #define _WIN32_WINNT 0x501
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <tchar.h>
    #include "CaptureConsoleCode.h"
     
    static const wchar_t g_htmlCGAColors[16][4] = {
    	L"000", L"00A", L"0A0", L"0AA", L"A00", L"A0A", L"A50", L"AAA",
    	L"555", L"55F", L"5F5", L"5FF", L"F55", L"F5F", L"FF5", L"FFF"
    };
    static const wchar_t g_htmlConsoleColors[16][7] = {
    	L"000000", L"000080", L"008000", L"008080", L"800000", L"800080", L"808000", L"C0C0C0",
    	L"808080", L"0000FF", L"00FF00", L"00FFFF", L"FF0000", L"FF00FF", L"FFFF00", L"FFFFFF"
    };
     
    /*
    ===============================================================================
    Util functions used in saving */
    static LPCTSTR FindLastOfAnyC(LPCTSTR str, LPCTSTR chars)
    {
    	LPCTSTR ret = NULL;
    	for( ; (*str) != _T('\0') ; str++)
    	{
    		if(_tcschr(chars, *str) != NULL)
    			ret = str;
    	}
    	return ret;
    }
     
    /*
    -----------------------------------------------------------------------------*/
    static LPTSTR GetFileNameWithoutExtension(LPCTSTR filePath)
    {
    	LPTSTR ret = NULL;
    	if(filePath!=NULL)
    	{
    		LPCTSTR lastSep = FindLastOfAnyC(filePath, _T("\\/"));
    		if(lastSep!=NULL)
    		{
    			LPCTSTR fileName = lastSep+1;
    			LPCTSTR ext = _tcsrchr(filePath, _T('.'));
    			size_t cchNameLength = (ext==NULL ? _tcslen(fileName) : ext-fileName);
    			size_t cchNameSize = cchNameLength+1;
     
    			ret = calloc(sizeof *ret, cchNameSize);
    			if(ret != NULL)
    				_tcsncpy_s(ret, cchNameSize, fileName, _TRUNCATE); /*This will truncate where the extension starts.*/
    		}
    	}
    	return ret;
    }
    void FreeFileName(LPTSTR buf) { free(buf); }
     
    /*
    =============================================================================
    Saving functions */
     
    static int WriteColorSpanTag(FILE *pfOut, BYTE attr)
    {
    	BYTE fg = attr & 0x0F;
    	BYTE bg = (attr >> 4) & 0x0F;
    	if(bg==0)
    		return fwprintf(pfOut, L"<span style=\"color:#%s\">", g_htmlConsoleColors[fg]);
    	else
    		return fwprintf(pfOut, L"<span style=\"background-color:#%s; color:#%s\">", g_htmlConsoleColors[bg], g_htmlConsoleColors[fg]);
    }
     
    /*Write a character while stripping the bare minimum of HTML entities.*/
    static int WriteHtmlChar(FILE *pfOut, wchar_t c)
    {
    	switch(c)
    	{
    	case L'<': return fputws(L"&lt;", pfOut);
    	case L'>': return fputws(L"&gt;", pfOut);
    	case L'&': return fputws(L"&amp;", pfOut);
    	default: return fputwc(c, pfOut);
    	}
    }
     
    /*
    count all lines that have at least one non-space character.*/
    static int CountLines(CHAR_INFO const *outputChars, short cx, short cy)
    {
    	int ymax = 0;
    	int x;
    	int y;
    	for(y=0 ; y<cy ; y++)
    	{
    		for(x=0 ; x<cx ; x++)
    		{
    			wchar_t c = outputChars[y*cx + x].Char.UnicodeChar;
    			if(c != L' ')
    			{
    				ymax = y;
    				break;
    			}
    		}
    	}
    	return ymax;
    }
     
    /*
    -----------------------------------------------------------------------------*/
    BOOL SaveConsoleCaptureToFile(LPCTSTR filePath, CHAR_INFO const *outputChars, short cx, short cy)
    {
    	/*Open the output file*/
    	FILE *pfOut = NULL;
    	errno_t err = (filePath==NULL || outputChars==NULL)
    		? EINVAL
    		: _tfopen_s(&pfOut, filePath, _T("w, ccs=UTF-8"));
    	if(err != 0)
    	{
    		switch(err)
    		{
    		case EPERM:
    		case EACCES: SetLastError(ERROR_ACCESS_DENIED); break;
    		case ENOENT: SetLastError(ERROR_FILE_NOT_FOUND); break;
    		case EIO:    SetLastError(ERROR_IO_DEVICE); break;
    		case EISDIR: SetLastError(ERROR_DIRECTORY); break;
    		case EINVAL: SetLastError(ERROR_INVALID_PARAMETER); break;
    		default:     SetLastError(ERROR_FUNCTION_FAILED); break;
    		}
    		return FALSE;
    	}
     
    	fputws(L"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n", pfOut);
    	fputws(L"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", pfOut);
     
    	/*Write the head, with the file name in the title.*/
    	{
    		LPTSTR fileNameBuf = GetFileNameWithoutExtension(filePath);
    		fputws(L"<head>\n", pfOut);
    		fputws(L"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n", pfOut);
    		if(fileNameBuf != NULL)
    		{
    			size_t iTitleChar;
    			fputws(L"<title>", pfOut);
    			for(iTitleChar=0 ; fileNameBuf[iTitleChar] != L'\0' ; iTitleChar++)
    				WriteHtmlChar(pfOut, fileNameBuf[iTitleChar]);
    			fputws(L" - Console capture</title>\n", pfOut);
    			FreeFileName(fileNameBuf);
    		}
    		fputws(L"</head>\n", pfOut);
    	}
     
    	/*Write the body, with the output characters.*/
    	{
    		/*First, count all lines that have at least one non-space character.*/
    		int ymax = CountLines(outputChars, cx, cy);
    		int y;
     
    		fputws(L"<body style=\"background-color: #000; color: #AAA;\">\n", pfOut);
    		fputws(L"<pre style=\"font-family: 'Lucida Console',monospace; font-size: 10pt\">\n", pfOut);
    		for(y=0 ; y<ymax+1 ; y++)
    		{
    			/*Color is set with spans for each line.*/
    			int x;
    			BYTE lastAttr = (BYTE)(outputChars[y*cx + 0].Attributes & 0x00FF);
    			WriteColorSpanTag(pfOut, lastAttr);
    			for(x=0 ; x<cx ; x++)
    			{
    				wchar_t c = outputChars[y*cx + x].Char.UnicodeChar;
     
    				/*Don't write trailing spaces: For each space, check if it's trailing.*/
    				if(c == L' ')
    				{
    					/*Try to find any non-space character on this line*/
    					int x2;
    					for(x2=x ; x2<cx ; x2++)
    					{
    						if(outputChars[y*cx + x2].Char.UnicodeChar != L' ')
    							break;
    					}
    					/*If found one, proceed normally. If not, break from x loop.*/
    					if(x2==cx)
    						break;
    				}
     
    				/*Handle color change*/
    				{
    					BYTE newAttr = (BYTE)(outputChars[y*cx + x].Attributes & 0x00FF);
    					if(newAttr != lastAttr)
    					{
    						fputws(L"</span>", pfOut);
    						lastAttr = newAttr;
    						WriteColorSpanTag(pfOut, lastAttr);
    					}
    				}
     
    				WriteHtmlChar(pfOut, c);
    			}
    			/*End of line.*/
    			fputws(L"</span>\n", pfOut);
    		}
    		fputws(L"</pre>\n", pfOut);
    		fputws(L"</body>\n", pfOut);
    	}
     
    	fputws(L"</html>\n", pfOut);
    	fclose(pfOut);
     
    	return TRUE;
    }
     
    /*
    ===============================================================================
    Capture functions */
     
    BOOL CaptureConsole(DWORD targetProcessId, CHAR_INFO **ppOutputChars, COORD *pSize)
    {
    	BOOL ret = FALSE;
    	if(ppOutputChars == NULL || pSize == NULL)
    	{
    		SetLastError(ERROR_INVALID_PARAMETER);
    		return FALSE;
    	}
    	*ppOutputChars = NULL;
     
    	if(!AttachConsole(targetProcessId))
    	{
    		DWORD err = GetLastError();
    		(void)err;
    	}
    	else
    	{
    		HANDLE hOutput = CreateFile(_T("CONOUT$"), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    		if(hOutput != INVALID_HANDLE_VALUE)
    		{
    			CONSOLE_SCREEN_BUFFER_INFO info = {0};
    			BOOL bGotInfo = GetConsoleScreenBufferInfo(hOutput, &info);
    			*pSize = info.dwSize;
     
    			if(bGotInfo)
    			{
    				int outputSize = info.dwSize.X * info.dwSize.Y;
    				CHAR_INFO* pOutputChars = calloc(outputSize, sizeof *pOutputChars);
    				if(pOutputChars != NULL)
    				{
    					/*Read by blocks of incY lines, because there was something that made reading as a whole block fail.*/
    					BOOL bReadChars = TRUE;
    					const short incY = 50;
    					short blockY;
    					for(blockY=0 ; blockY<info.dwSize.Y ; blockY+=incY)
    					{
    						SMALL_RECT rgn = {0, blockY, info.dwSize.X-1, min(blockY+incY, info.dwSize.Y)-1};
    						COORD origin= {0, blockY};
    						bReadChars = bReadChars && ReadConsoleOutputW(hOutput, pOutputChars, info.dwSize, origin, &rgn);
    					}
     
    					if(bReadChars) {
    						ret = TRUE; /*Success!*/
    					} else {
    						DWORD err = GetLastError();
    						(void)err;
    					}
     
    					/*Clean up.*/
    					if(!ret)
    						free(pOutputChars), pOutputChars=NULL;
    					*ppOutputChars = pOutputChars;
    				}
    				else
    					SetLastError(ERROR_OUTOFMEMORY);
    			}
    			CloseHandle(hOutput), hOutput=INVALID_HANDLE_VALUE;
    		}
     
    		FreeConsole();
    	}
    	return ret;
    }
     
    void FreeOutputChars(CHAR_INFO *pOutputChars) { free(pOutputChars); }
     
    /*
    ===============================================================================
    Main functions */
     
    /* Capture console from target process and save it as HTML.
    hWndOwner : HWND of owner window for dialog boxes.
    targetProcessId : ID of the process whose console to capture.
    */
    BOOL CaptureAndSaveConsole(DWORD targetProcessId, LPCTSTR filePath)
    {
    	BOOL ret = FALSE;
    	CHAR_INFO *pOutputChars = NULL;
    	COORD size = {0};
    	if(CaptureConsole(targetProcessId, &pOutputChars, &size))
    	{
    		/*Save this to a HTML file.*/
    		ret = SaveConsoleCaptureToFile(filePath, pOutputChars, size.X, size.Y);
    		FreeOutputChars(pOutputChars);
    	}
    	return ret;
    }
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut
    Merci pour cette example et l'explication sur les handles.

    Je suis quelques peu confus ; j'imagine que la fonction CaptureAndSaveConsole correspond au point d'entrée dans lequel on fournit targetProcessId et filePath. Pour ma part, j'ai "découvert" la fonction GetWindowThreadProcessId qui fournit le numéro du process à partir du handle de la fenêtre (et oui, il se peut que j'ai plusieurs consoles d'ouverts).

    Pouvez-vous m'expliquer la ligne suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    BOOL bGotInfo = GetConsoleScreenBufferInfo(hOutput, &info);
    ... et plus particulièrement le lien entre targetProcessId et hOuptut ?

    Merci encore !

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    j'imagine que la fonction CaptureAndSaveConsole correspond au point d'entrée dans lequel on fournit targetProcessId et filePath.
    Oui, mais si tu veux utiliser directement le contenu de la console dans ton code (plutôt que sauvegarder cela dans un fichier) tu peux utiliser CaptureConsole() à la place.

    Citation Envoyé par Orbeaman Voir le message
    Pouvez-vous m'expliquer la ligne suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    BOOL bGotInfo = GetConsoleScreenBufferInfo(hOutput, &info);
    ... et plus particulièrement le lien entre targetProcessId et hOuptut ?
    La fonction GetConsoleScreenBufferInfo accepte un handle de "console screen buffer" et retourne une structure contenant (entre autres) la taille du "console screen buffer" (sous Windows XP, par défaut un "console screen buffer" fait 80*300 caractères, alors que la fenêtre console fait 80*25 caractères).

    Le lien entre targetProcessId et hOutput n'est pas direct: Il passe par deux étapes:
    • D'abord on utilise AttachConsole() pour que le programme de capture soit lié à la console qu'on veut capturer.
      Note: cela ne marche que si le programme appelant n'a pas déjà une console, il faut donc que ce soit un programme "fenêtré" -- fonction principale WinMain() au lieu de main().
    • Ensuite, il faut "ouvrir" le "console screen buffer" associé au processus en appelant la fonction CreateFile() (équivalent Win32 de fopen()) avec le nom de fichier spécial "CONOUT$".

    Une fois qu'on a obtenu hOutput, on récupère la taille du "console screen buffer", puis son contenu, puis on ferme le handle et on se détache de la console (CloseHandle et FreeConsole).
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut
    Merci pour ces précisions, mais j'avoue pas encore être en mesure de tout comprendre. De ce que je comprends, il y a pas de programmation "masquée", notamment au niveau au paramètre "CONOUT$".

    Dans l'idéal, je souhairetairs récupéré tout le contenu de la console dans une variable String et encore extraire les infos utiles à l'aide d'expressions régulières. Je vais donc regarder la CaptureConsole de plus près

    Dans mon cas, je lancerai la macro en VBA depuis CATIA (logiciel de CAO, mais ça serait pareil depuis Office). Il y aura par contre plusieurs fenêtres DOS ouvertes, sans pour autant être certain qu'il y en ait eu vraiment attachée à CATIA (il y a notamment un gestionnaire associé à une console DOS pour lancer CATIA). En tout cas, la console lu sera totalement découplée de CATIA.

    Ps : j'ai aussi pris connaissance de ce code http://www.vbforums.com/showthread.p...ch-console-cmd qui semble suivre les mêmes étapes essentielles, mais le test sur AttachConsole(ProcessID:=lPID) ne passe pas, pourtant j'obtiens un lPID valide...

    Merci encore !

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut
    Après quelques jours de réflexion...

    Si je comprends bien, le fait de passer par un ScreenBuffer revient à faire une "capture d'écran" de la console (du moins, tout ce qui est visible) pour ensuite lire tout ce qui se trouve bloc par bloc (j'imagine que cela n'est applicable qu'au fait que la console utilise une police à chasse fixe).

    Ci-dessous le code VBA minimaliste (à titre indicatif vu que c'est un forum de C++ ) sans la déclaration des fonctions, types et constantes de l'API Windows) :

    Code VBA : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    Sub CaptureConsole()
     
        Dim lHwnd As Long: lHwnd = FindWindow(lpClassName:="ConsoleWindowClass", lpWindowName:="Invite de commandes") 'Launching PCVaulting") 'Invite de commandes
     
        Dim targetProcessID As Long: GetWindowThreadProcessId hWnd:=lHwnd, lpdwProcessId:=targetProcessID
     
        AttachConsole ProcessID:=targetProcessID
     
        Dim lpSecurityAttributes As SECURITY_ATTRIBUTES 'doit être passé par référence, mais ce n'est visiblement pas possible d'envoyer une référence de Nothing
     
        Dim hOutput As Long: hOutput = CreateFile(lpFileName:="CONOUT$", dwDesiredAccess:=GENERIC_READ, dwShareMode:=FILE_SHARE_READ Or FILE_SHARE_WRITE Or FILE_SHARE_DELETE, lpSecurityAttributes:=lpSecurityAttributes, dwCreationDisposition:=OPEN_EXISTING, dwFlagsAndAttributes:=FILE_ATTRIBUTE_NORMAL, hTemplateFile:=0&)
     
        Dim info As CONSOLE_SCREEN_BUFFER_INFO: GetConsoleScreenBufferInfo hConsoleOutput:=hOutput, lpConsoleScreenBufferInfo:=info
     
        Dim pOutputChars As CHAR_INFO
        Dim origin As COORD
        Dim rng As SMALL_RECT
     
        Const incY As Long = 50
     
        Dim blockY As Long: For blockY = 0 To info.dwSize.y Step incY
     
            With origin
                .x = 0
                .y = blockY
            End With
     
            With rng
                .Bottom = 0
                .Left = blockY
                .Right = info.dwSize.x - 1
                .Top = WorksheetFunction.min(blockY + incY, info.dwSize.y) - 1
            End With
     
            If ReadConsoleOutput(hConsoleOutput:=hOutput, lpBuffer:=pOutputChars, dwBufferSize:=info.dwSize, dwBufferCoord:=origin, lpReadRegion:=rng) Then Debug.Print pOutputChars.Char
     
        Next blockY
     
        FreeConsole
     
    End Sub

    Visiblement, ReadConsoleOutput ne semble pas systématiquement être valide... (je ne crois pas qu'il y ait de ReadConsoleOutputW via l'API Windows en VBA).

    Code VBA : Sélectionner tout - Visualiser dans une fenêtre à part
    Public Declare Function ReadConsoleOutput Lib "kernel32" Alias "ReadConsoleOutputA" (ByVal hConsoleOutput As LongPtr, lpBuffer As CHAR_INFO, dwBufferSize As COORD, dwBufferCoord As COORD, lpReadRegion As SMALL_RECT) As Long

    D'ailleurs, qu'en est-il de pOutputChars de type CHAR_INFO [integer, integer] ? Je comprends bien que ça passe par des pointeurs (et donc des tableaux de caractère (code UNICODE je présume) / couleur), mais moi j'obtiens systématiquement (0, 0).

    En tout cas, j'obtiens bien info.dwSize = (80, 300).

    Ps : j'ai trouvé la doc suivante à propos du CONOUT$ : https://docs.microsoft.com/en-us/win...screen-buffers (tout commence à me parler...)

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut
    Finalement, j'y suis arrivé en changeant la déclaration de ReadConsoleOutput (vivement le polymorphisme )

    Code VBA : Sélectionner tout - Visualiser dans une fenêtre à part
    Public Declare Function ReadConsoleOutput Lib "kernel32" Alias "ReadConsoleOutputA" (ByVal hConsoleOutput As LongPtr, lpBuffer As CHAR_INFO, dwBufferSize As COORD, dwBufferCoord As COORD, lpReadRegion As SMALL_RECT) As Long

    Bien sûr, il me reste encore quelques p'tits trucs à creuser, car il y a encore une myriade de choses qui m'échappent.

  8. #8
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Attention, dans ton code je n'ai pas vu le CloseHandle.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  9. #9
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut
    Effectivement, merci pour la remarque. J'ignore si je l'avais quand même rajouté dans le code final ou non, mais je n'ai pas l'impression que ça ait posé le moindre problème.

Discussions similaires

  1. [XL-2016] Lire contenu console Windows
    Par Orbeaman dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 22/06/2019, 03h02
  2. lancer une application perl+tk sans console (windows/linux)
    Par mmiikkll dans le forum Interfaces Graphiques
    Réponses: 5
    Dernier message: 07/09/2010, 16h56
  3. lire path de windows
    Par niglo dans le forum Langage
    Réponses: 3
    Dernier message: 28/03/2006, 10h05
  4. lire contenu base .btr (Btrieve)
    Par Harry dans le forum Bases de données
    Réponses: 8
    Dernier message: 23/01/2005, 17h19
  5. Accents dans une console windows
    Par JolyLoic dans le forum Windows
    Réponses: 8
    Dernier message: 20/10/2004, 00h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo