Bonjour,

Je ne comprends pas pourquoi je n'arrive pas à attacher une console Windows externe (s'appelant "Command Prompt") que j'ai bien évidemment préalablement ouverte.


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
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Option Explicit
 
Private m_StdError   As LongPtr
Private m_StdInput   As LongPtr
Private m_StdOutput As LongPtr
 
Declare PtrSafe Function AttachConsole Lib "kernel32" (ByVal ProcessID As Long) As Boolean
Declare PtrSafe Function FreeConsole Lib "kernel32" () As Long
Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As Long
 
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
 
'
Sub test()
 
    Dim lHwnd As LongPtr, lPID As Long
 
    lHwnd = FindWindow(lpClassName:="ConsoleWindowClass", lpWindowName:="Command Prompt")
 
    GetWindowThreadProcessId hwnd:=lHwnd, lpdwProcessId:=lPID
 
    Debug.Print AttachConsole(ProcessID:=lPID) ' <- échoue, malgré un lPID vailide
 
    CloseHandle hObject:=m_StdOutput
    CloseHandle hObject:=m_StdInput
    FreeConsole
 
End Sub
Avez-vous une idée d'où vient le problème ?

Ps : j'utilise Excel 64 bits et il y aurait visiblement des différences avec la version 32 bits, en plus des adresses mémoires plus longues.

Merci par avance !