커널 오브젝트 테이블 만들어서 시그널 기다리기.
여러 방식이 있겠지만 이 페이지의 예제는 Thread를 하나 만들어서 첩자를 넣었다 뺐다 하는 형태.
[참조]
FindFirstChangeNotification 함수의 MSDN 설명
- http://msdn.microsoft.com/en-us/library/aa364417(v=vs.85).aspx
// Thread의 Execute
procedure TWatcher.Execute;
var
hWatcher: THandle;
FilePath: PAnsiChar;
bSubTree: LongBool;
iFilter: Cardinal;
iNotify: Integer;
begin
// 하위 디렉토리는 감시하지 않음
bSubTree:=False;
// 최종 수정 시각 체크 (이 필터를 통해 감시하고자 하는 이벤트 지정가능)
iFilter:=FILE_NOTIFY_CHANGE_LAST_WRITE;
while not Self.Terminated do begin
Application.ProcessMessages;
// 감시경로 설정. 파일명이 아니라 디렉토리명임에 주의
FilePath:=PAnsiChar(ExtractFilePath('C:\특정디렉토리'));
// 감시자 침투 시도
hWatcher:=FindFirstChangeNotification(FilePath, bSubTree, iFilter);
// 침투 성공
if hWatcher<>INVALID_HANDLE_VALUE then begin
// 감시자의 서신을 기다림
iNotify:=WaitForSingleObject(hWatcher, 100); // INFINITE);
case iNotify of
// 우리가 원하는 서신일 경우에만 처리
WAIT_OBJECT_0: begin
// 본국에 알리는 등 필요한 행위를 한다.
PostMessage(frmMain.Handle, WM_USER+123, 0, 0);
end;
// 감시자 귀환
FindCloseChangeNotification(hWatcher);
end;
end
// 침투 실패
else begin
OutputDebugString(PAnsiChar('감시자 침투 실패 = '+IntToStr(GetLastError())));
end;
Sleep(100);
end;
end;
procedure TWatcher.Execute;
var
hWatcher: THandle;
FilePath: PAnsiChar;
bSubTree: LongBool;
iFilter: Cardinal;
iNotify: Integer;
begin
// 하위 디렉토리는 감시하지 않음
bSubTree:=False;
// 최종 수정 시각 체크 (이 필터를 통해 감시하고자 하는 이벤트 지정가능)
iFilter:=FILE_NOTIFY_CHANGE_LAST_WRITE;
while not Self.Terminated do begin
Application.ProcessMessages;
// 감시경로 설정. 파일명이 아니라 디렉토리명임에 주의
FilePath:=PAnsiChar(ExtractFilePath('C:\특정디렉토리'));
// 감시자 침투 시도
hWatcher:=FindFirstChangeNotification(FilePath, bSubTree, iFilter);
// 침투 성공
if hWatcher<>INVALID_HANDLE_VALUE then begin
// 감시자의 서신을 기다림
iNotify:=WaitForSingleObject(hWatcher, 100); // INFINITE);
case iNotify of
// 우리가 원하는 서신일 경우에만 처리
WAIT_OBJECT_0: begin
// 본국에 알리는 등 필요한 행위를 한다.
PostMessage(frmMain.Handle, WM_USER+123, 0, 0);
end;
// 감시자 귀환
FindCloseChangeNotification(hWatcher);
end;
end
// 침투 실패
else begin
OutputDebugString(PAnsiChar('감시자 침투 실패 = '+IntToStr(GetLastError())));
end;
Sleep(100);
end;
end;
'Delphi' 카테고리의 다른 글
[Delphi] 윈도우 핸들로 브라우저의 HTML 가져오기 (0) | 2012.10.25 |
---|---|
[Delphi] API Hooking (0) | 2010.12.15 |
[Delphi] UAC 권한상승 manifest를 리소스에 포함시켜 컴파일 (UAC requireAdministrator manifest resource file) (6) | 2010.11.23 |
[Delphi] 에디트플러스와 DDE 통신 (DDE, EditPlus) (0) | 2010.10.25 |
[Delphi] .lnk (바로가기) 파일에서 실행파일 경로 가져오기 (0) | 2010.10.25 |