다른 명령
- C++ 코드를 DLL 형태로 작성한 예시입니다.
- 해당 DLL은 윈도우 에디트 컨트롤의 핸들을 받아, 선택된 문자열을 반환합니다.
⸻
✅ 1. DLL 코드 (C++)
// SelectTextDLL.cpp
// SelectTextDLL.cpp
#include "pch.h" // 미리 컴파일된 헤더 추가
#include <windows.h>
#include <richedit.h> // EM_GETSELTEXT 정의를 위해 추가
extern "C" __declspec(dllexport) int __stdcall GetSelectedText(HWND hwndEdit, wchar_t* buffer, int bufferSize)
{
int start = 0, end = 0;
SendMessageW(hwndEdit, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
int len = end - start;
if (len <= 0 || bufferSize < len + 1) {
return 0; // Nothing selected or buffer too small
}
// 선택된 텍스트 가져오기
SendMessageW(hwndEdit, EM_GETSELTEXT, 0, (LPARAM)buffer);
buffer[len] = L'\0'; // 널 종료 문자
return len;
}
⸻
✅ 2. 헤더 파일 (선택 사항)
// SelectTextDLL.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) int __stdcall GetSelectedText(HWND hwndEdit, wchar_t* buffer, int bufferSize);
#ifdef __cplusplus
}
#endif
⸻
✅ 3. 빌드 방법
- Visual Studio에서 Win32 DLL 프로젝트 생성
- 위 코드 붙여넣기
- Character Set을 Use Unicode Character Set으로 설정
- 빌드 → SelectTextDLL.dll 생성됨
⸻
✅ 4. AutoIt에서 사용 예시
; 에디트 컨트롤 핸들 얻기 (예: 메모장 Edit1)
$hEdit = ControlGetHandle("[CLASS:Notepad]", "", "Edit1")
; 버퍼 준비
Local $bufferSize = 1024
Local $tBuffer = DllStructCreate("wchar[" & $bufferSize & "]")
; DLL 호출
Local $ret = DllCall("SelectTextDLL.dll", "int", "GetSelectedText", _
"hwnd", $hEdit, _
"struct*", $tBuffer, _
"int", $bufferSize)
If @error Then
MsgBox(16, "Error", "DLL 호출 실패")
Else
Local $text = DllStructGetData($tBuffer, 1)
MsgBox(64, "선택된 텍스트", $text)
EndIf
⸻
✅ 보안 및 주의사항 • hwndEdit는 반드시 유효한 에디트 컨트롤 핸들이어야 합니다. • 비정상 접근 시 Access Violation이 발생할 수 있으므로 AutoIt에서 핸들 유효성 검사