반응형

user32.dll을 이용해 현재 켜져있는 모든 프로세스 목록 (process list, window titles)을 가져오는 방법.

매크로를 만들려면 현재 열려있는 게임 창에 대한 정보가 필요하다 (윈도 위치, 내부 그림 등..). 
이를 위해서는 윈도 창 목록을 전부 가져와서 --> 게임 창을 특정짓는 작업이 필요하다.

1. 열려있는 윈도를 전부 불러온다.

1) CODE

        //user32.dll import 함수들
        public delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumWindows(EnumedWindow lpEnumFunc, ArrayList lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", EntryPoint = "GetWindowTextLength", SetLastError = true)]
        internal static extern int GetWindowTextLength(IntPtr hwnd);



        //실제 코드부 (C#)
            ArrayList list = GetWindows();
            Debug.WriteLine(list.Count);
            StringBuilder winText = new StringBuilder();
            int winTitleLength = 0;
            int count = 0;
            int length = 0;
            foreach (IntPtr _winHandle in list)
            {
                try
                {
                    winTitleLength = GetWindowTextLength(_winHandle);  //윈도 타이틀의 길이 가져옴.

                    winText.Capacity = winTitleLength;  //타이틀 길이에 맞춰 stringbulder 크기 조절
                    count++;
                    GetWindowText(_winHandle, winText, winTitleLength);
                    
                    //Debug.WriteLine(count + ": " + _winHandle.ToString());
                }
                finally
                {
                    Debug.WriteLine(count + "  " + _winHandle + ": " + winText);
                    winText.Clear();
                }
            }    
            

2) 출력 확인

 

2. 원하는 창을 특정짓는다 (여기선 메모장)

1) 코드

-. If 구문을 이용해 특정 이름이 포함된 창만을 가져온다.

            ArrayList list = GetWindows();
            Debug.WriteLine(list.Count);
            StringBuilder winText = new StringBuilder();
            int winTitleLength = 0;
            int count = 0;
            int length = 0;
            foreach (IntPtr _winHandle in list)
            {
                try
                {
                    winTitleLength = GetWindowTextLength(_winHandle);  //윈도 타이틀의 길이 가져옴.

                    winText.Capacity = winTitleLength;  //타이틀 길이에 맞춰 stringbulder 크기 조절
                    count++;
                    GetWindowText(_winHandle, winText, winTitleLength);
                    
                    //Debug.WriteLine(count + ": " + _winHandle.ToString());
                }
                finally
                {
                    if(winText.ToString().Contains("메모장")) //특정 창 확인
                    {
                        Debug.WriteLine(count + "  " + _winHandle + ": " + winText);
                    }
                    
                    winText.Clear();
                }
            }   

2) 출력결과

 

728x90
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기