반응형

파일이나 폴더를 삭제하려고 할 때 아래와 같이 'XXX에서 열려 있어 작업을 완료 할 수 없다'는 메세지가 나오는 경우가 있다.

파일이 사용 중이기 때문에 삭제할 수 없다.

물론 이런 경우 대부분 엑셀을 종료하면 간단히 해결 할 수 있지만 간혹 엑셀이 종료되었는데도 파일을 삭제할 수 없는 상황이 발생하기도 한다. 파일(폴더)의 핸들을 어디선가 다른 프로세스가 잡고 있기 때문이다.

 

간단히 비슷한 상황을 만들어 해결책을 알아보자.

 

삭제하고 싶은 폴더(Target)를 만들고 안에 파일 하나를 생성한다.

 

생성된 파일을 열어준다.

 

폴더(Target) 삭제를 시도한다. 폴더 내 파일이 사용 중이기 때문에 실패한다.

물론 우리는 메모장(notepad)이 파일을 잡고 있다는걸 알지만 여기서는 메모장의 상황은 모르는걸로 가정하자.

 

 

어떤 프로그램이 폴더(Target)를 사용 하는지 알아내기 위해 프로세스 익스플로러를 실행하고 Find - Find Handle or DLL...을 선택한다.

 

찾고 싶은 이름(target)을 입력하고 Search 버튼을 클릭한다.

'target' 문자열이 들어가는 이름의 핸들을 잡고 있는 프로세스를 모두 찾아준다. 위 예에선 우리가 삭제하고자 하는 'G:\Test\Target'을 notepad.exe 프로세스가 잡고 있는걸 확인 할 수 있다. 클릭한다.

 

notepad.exe 프로세스가 소유한 파일 핸들 중 'G:\Test\Target'이 있다.

어떤 프로세스가 핸들을 잡고 있는지 알아냈으므로 그 프로세스를 종료시키거나 다른 작업을 통해 핸들을 반환하도록 할 수 있다.

 

※ 참고

Process Explorer

 

반응형
Posted by J-sean
:
반응형

프로그램의 어떤 함수가 컴퓨터 CPU 자원을 독점하는지 찾아보자.

 

프로세스 익스플로러를 실행한다.

 

Options - Configure Symbols... 를 선택한다.

 

Dbghelp.dll 경로와 분석할 프로그램 Symbols 경로를 지정한다.

 

CPU 자원을 많이 소비하고 있는 프로세스를 찾는다.

 

 

우클릭 - Properties... 를 선택한다.

 

Threads 탭을 선택하고 CPU를 많이 사용하는 스레드를 찾아 선택하고 Stack 버튼을 클릭한다.

 

ThreadFunc2 함수가 실행되고 있음을 확인한다.

 

ThreadFunc2()

 

반응형
Posted by J-sean
:
반응형

 

2021.12.30 - [Unity] - Unity3D - 유니티 3D with OpenCV 1 에서 만든 라이브러리(OpenCVDll.dll)와 

2021.12.29 - [Unity] - Unity3D - 유니티 3D WebCamTexture 라이브 비디오 텍스쳐 의 WebCamTexture를 이용해 라이브 비디오 데이터를 프로세싱하고 오브젝트의 텍스쳐로 사용해 보자.

 

이미지 프로세싱에 사용할 OpenCVDll 라이브러리를 세팅하고 Cube를 생성한다.

 

Cube에 스크립트를 추가한다.

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using System.Runtime.InteropServices;
 
public class UnityWithOpenCV : MonoBehaviour
{
    [DllImport("OpenCVDll")]
    private static extern void FlipImage(ref Color32[] rawImage, int width, int height);
 
    Renderer renderer;
    WebCamTexture webCamTexture;
    Color32[] image;
 
    // Start is called before the first frame update
    void Start()
    {
        renderer = GetComponent<Renderer>();
        webCamTexture = new WebCamTexture(64048060);
        webCamTexture.Play();
 
        image = new Color32[webCamTexture.width * webCamTexture.height];
    }
 
    // Update is called once per frame
    void Update()
    {
        webCamTexture.GetPixels32(image);
 
        // Image processing
        FlipImage(ref image, webCamTexture.widthwebCamTexture.height);
 
        Texture2D texture2D = new Texture2D(webCamTexture.width, webCamTexture.height);
        texture2D.SetPixels32(image);
        renderer.material.mainTexture = texture2D as Texture;
        (renderer.material.mainTexture as Texture2D).Apply();
    }
}
 

 

Cube에 추가한 스크립트에 코드를 작성하고 저장한다. 모든 프레임에 텍스쳐가 이미지 프로세서(FlipImage())에 의해 상하좌우 반전된다.

 

유니티로 돌아오면 스크립트가 컴파일 된다. 실행 버튼을 클릭하면 매 프레임마다 Cube의 텍스쳐가 상하좌우 반전 처리된다.

 

 

스크립트의 FlipImage()를 주석처리하고 실행하면 원래 이미지의 텍스쳐가 표시된다.

 

프로그램을 실행하는 동안 처리된 이미지가 Inpector창의 Material Preview에도 실시간 적용된다.

 

반응형
Posted by J-sean
:
반응형

실행중인 모든 프로세스와 현재 포커스를 갖고 있는 프로세스를 조사하고 각 프로세스의 윈도우 사이즈를 구해 보자.

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
using System.Diagnostics;
using System.Runtime.InteropServices;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string className, string windowName);
        // 이 예제에서는 쓰이지 않았지만 Findwindow()는 아래와 같이 사용하면 된다.
        // IntPtr hwnd = FindWindow(className, windowName);
        // windowName = proc.ProcessName;
        // className = proc.MainWindowTitle;
 
        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
 
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
 
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hWnd, out Rectangle rect);
 
        [DllImport("dwmapi.dll")]
        private static extern int DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, out Rectangle pvAttribute, int cbAttribute);
        
        public Form1()
        {
            InitializeComponent();
 
            timer1.Interval = 5000// 5초마다 갱신
        }
        
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Process[] processes = Process.GetProcesses();            
            Rectangle rect;
            // GetWindowRect(), DwmGetWindowAttribute()에 의해 rect에는 아래와 같은 값이 들어간다.
            // rect.X = 좌상단 X 좌표
            // rect.Y = 좌상단 T 좌표
            // rect.Width = 우하단 X 좌표 (너비가 아니다)
            // rect.Height = 우하단 Y 좌표 (높이가 아니다)
            int rectangleSize = Marshal.SizeOf(typeof(Rectangle));
            int index;  // Process.GetProcesses()로 조사된 모든 프로세스 중 몇 번째 인지 나타낼 인덱스.
            int row = 0;    // 내용 표시 Y 위치
            string str;
 
            foreach (Process p in processes)
            { 
                // A process has a main window associated with it only if the process has a graphical interface. If the associated process
                // does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden,
                // that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the
                // notification area, at the far right of the taskbar.
                // 테스크 바에 표시되는(보이는) 프로세스만 조사. 윈도우 핸들이 0(IntPtr.Zero)이 아닌 윈도우인데 그렇다고 다 보이는 건 아니다.
                if (p.MainWindowHandle != IntPtr.Zero)
                {
                    index = Array.IndexOf(processes, p); // 비효율적인 배열의 인덱스 가져오기. foreach()말고 for(;;)를 쓰는게 낫다.
                    DwmGetWindowAttribute(p.MainWindowHandle, 9out rect, rectangleSize);
                    // 9 = DWMWA_EXTENDED_FRAME_BOUNDS
                    str = string.Format("[{0}] {1}: ({2}, {3}, {4}, {5})", index, p.ProcessName, rect.X, rect.Y, rect.Width, rect.Height);
                    e.Graphics.DrawString(str, Font, Brushes.Black, 0, row);
                    row += 15;
                }
            }
            
            // 포커스를 갖고 있는 윈도우(Foreground window) 조사
            uint foregroundProcessID = 0;
            IntPtr foregroundHWnd = GetForegroundWindow(); // Get foreground window handle
            uint threadID = GetWindowThreadProcessId(foregroundHWnd, out foregroundProcessID); // Get PID from window handle
            Process foregroundProcess = Process.GetProcessById(Convert.ToInt32(foregroundProcessID)); // Get it as a C# obj.
 
            // 윈도우 사이즈 조사
            DwmGetWindowAttribute(foregroundHWnd, 9out rect, rectangleSize);
            // 9 = DWMWA_EXTENDED_FRAME_BOUNDS            
            str = string.Format("[Foreground] {0}: ({1}, {2}, {3}, {4})", foregroundProcess.ProcessName, rect.X, rect.Y, rect.Width, rect.Height);
            e.Graphics.DrawString(str, Font, Brushes.Black, 0, row);
            row += 30;
 
            // Windows 10 has thin invisible borders on left, right, and bottom, it is used to grip the mouse for resizing.
            // The borders might look like this: 7, 0, 7, 7(left, top, right, bottom)
            // 윈도우 사이즈 조사 시 예전과 같이 GetWindowRect()를 사용하면 보통 7픽셀의 resizing을 위한 테두리가 포함된다.
            e.Graphics.DrawString("[Foreground window with invisible border]", Font, Brushes.Red, 0, row);
            row += 15;
            GetWindowRect(foregroundHWnd, out rect);
            str = string.Format("[Foreground] {0}: ({1}, {2}, {3}, {4})", foregroundProcess.ProcessName, rect.X, rect.Y, rect.Width, rect.Height);
            e.Graphics.DrawString(str, Font, Brushes.Black, 0, row);
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
 

 

소스를 입력하고 실행한다.

 

인덱스를 확인하면 수백개의 프로세스가 실행중임을 알 수 있다.

 

GetWindowRect()로 윈도우 사이즈를 구하면 resizing을 위한 보이지 않는 테두리가 포함 되므로 눈에 보이는 정확한 사이즈는 DwmGetWindowAttribute()로 구하자.

 

반응형
Posted by J-sean
: