golang 프로그램 아이콘 수정
매일 컴퓨터를 사용할 때 일반적으로 사용되는 프로그램을 열어야 하는 경우가 많습니다. 이러한 프로그램은 인터페이스에 특정 아이콘을 표시하므로 신속하게 식별하고 찾을 수 있습니다. 그러나 어떤 경우에는 이러한 프로그램 아이콘을 개인 취향이나 테마와 더욱 일치하도록 변경하는 등의 작업을 원할 수도 있습니다.
이 기사에서는 golang과 일부 시스템 라이브러리를 사용하여 프로그램 아이콘을 변경하는 방법에 중점을 둘 것입니다. 데모 환경으로 Windows를 사용하겠습니다.
먼저 수행해야 할 기본 단계를 간략히 설명하겠습니다.
- 프로그램의 리소스 파일(.exe 또는 .dll 파일)을 열고 해당 아이콘 리소스를 찾습니다.
- 프로그램의 리소스 파일에 새 아이콘 리소스를 추가합니다.
- 새 아이콘 리소스에 액세스할 수 있도록 프로그램의 .manifest 파일을 변경하세요.
다음으로 이러한 단계를 하나씩 완료하는 방법에 대해 논의하겠습니다.
1단계: 리소스 파일을 열고 아이콘 리소스를 찾습니다.
golang에서는 시스템 라이브러리 "syscall"의 함수를 사용하여 파일을 열고 읽을 수 있습니다. 이를 위해 몇 가지 필수 변수를 정의해야 합니다.
package main import ( "os" "syscall" "unsafe" ) var ( kernel32DLL = syscall.MustLoadDLL("kernel32.dll") BeginUpdateResourceProc = kernel32DLL.MustFindProc("BeginUpdateResourceW") UpdateResourceProc = kernel32DLL.MustFindProc("UpdateResourceW") EndUpdateResourceProc = kernel32DLL.MustFindProc("EndUpdateResourceW") )
여기서는 Windows API에서 "BeginUpdateResourceW", "UpdateResourceW" 및 "EndUpdateResourceW"와 같은 여러 함수를 사용합니다. 이러한 기능은 프로그램 리소스 파일의 리소스를 작동하는 데 도움이 될 수 있습니다.
다음으로 변경하려는 프로그램의 리소스 파일(.exe 또는 .dll 파일일 수 있음)을 열고 아이콘 리소스를 찾아야 합니다. 여기서는 "findIconIndex"라는 함수를 사용하여 프로그램 리소스 파일을 탐색하고 해당 아이콘 리소스가 있는 인덱스 번호를 찾습니다.
func findIconIndex(exePath string) (int, error) { exeFile, err := os.OpenFile(exePath, os.O_RDWR, 0666) defer exeFile.Close() if err != nil { return 0, err } exeStat, err := exeFile.Stat() if err != nil { return 0, err } exeSize := exeStat.Size() // DOS header dosHeader := new(image.DosHeader) err = binary.Read(exeFile, binary.LittleEndian, dosHeader) if err != nil { return 0, err } exeFile.Seek(int64(dosHeader.Lfanew), 0) // File header and optional header fileHeader := new(image.FileHeader) err = binary.Read(exeFile, binary.LittleEndian, fileHeader) if err != nil { return 0, err } extHeader := make([]byte, fileHeader.SizeOfOptionalHeader-2) exeFile.Read(extHeader) // Section headers sections := make([]image.SectionHeader, fileHeader.NumberOfSections) err = binary.Read(exeFile, binary.LittleEndian, sections) if err != nil { return 0, err } // Find icon resource for _, section := range sections { if section.Name == ".rsrc" { exeFile.Seek(int64(section.Offset), 0) resourceHeader := new(resourceHeader) err = binary.Read(exeFile, binary.LittleEndian, resourceHeader) if err != nil { return 0, err } stack := []resourceDirectoryEntry{resourceDirectoryEntry{uint32(resourceHeader.RootID), int64(resourceHeader.OffsetToDirectory)}} for len(stack) > 0 { currentEntry := stack[len(stack)-1] stack = stack[:len(stack)-1] exeFile.Seek(currentEntry.offset, 0) directoryHeader := new(resourceDirectoryHeader) err = binary.Read(exeFile, binary.LittleEndian, directoryHeader) if err != nil { return 0, err } entries := make([]resourceDirectoryEntry, directoryHeader.NumNamedEntries+directoryHeader.NumIDEntries) for i := range entries { err = binary.Read(exeFile, binary.LittleEndian, &entries[i]) if err != nil { return 0, err } if entries[i].nameIsString { nameBytes := make([]byte, entries[i].nameOffset&0x7FFFFFFF) exeFile.Read(nameBytes) entries[i].name = syscall.UTF16ToString(nameBytes) } } for _, entry := range entries { if entry.ID&^0xFFFF == rtIcon { return int(entry.ID & 0xFFFF), nil } else if entry.name == "ICON" { stack = append(stack, resourceDirectoryEntry{entry.ID, int64(entry.offset)}) } else if entry.name == "#0" && entry.ID&^0xFFFF == rtGroupIcon { groupIconDirHeader := new(resourceGroupIconDirectoryHeader) exeFile.Seek(int64(entry.offset), 0) err = binary.Read(exeFile, binary.LittleEndian, groupIconDirHeader) if err != nil { return 0, err } var largestIcon resourceGroupIconDirectoryEntry for i := 0; i < int(groupIconDirHeader.Count); i++ { groupIconDirEntry := new(resourceGroupIconDirectoryEntry) err = binary.Read(exeFile, binary.LittleEndian, groupIconDirEntry) if err != nil { return 0, err } if groupIconDirEntry.Width > largestIcon.Width || groupIconDirEntry.Height > largestIcon.Height { largestIcon = *groupIconDirEntry } } return int(largestIcon.ID), nil } else if entry.name == "ICONGROUP" { stack = append(stack, resourceDirectoryEntry{entry.ID, int64(entry.offset)}) } else if entry.name == "MAINICON" { stack = append(stack, resourceDirectoryEntry{entry.ID, int64(entry.offset)}) } else { stack = append(stack, resourceDirectoryEntry{entry.ID, int64(entry.offset)}) } } } return 0, fmt.Errorf("Icon not found") } } return 0, fmt.Errorf("Resource not found") }
이 함수는 프로그램 리소스 파일의 각 섹션(.rsrc)을 반복하여 아이콘 리소스의 인덱스를 찾습니다. 일반적으로 인덱싱은 아이콘 리소스의 크기와 형식에 따라 달라집니다. 우리는 우리의 재량에 따라 리소스 파일에서 변경될 아이콘 리소스의 인덱스를 결정할 수 있습니다.
2단계: 리소스 파일에 새 아이콘 리소스 추가
프로그램 리소스 파일에 새 아이콘 리소스를 추가하려면 먼저 ICO 파일 형식으로 저장해야 합니다. golang의 이미지 라이브러리를 사용하여 ICO 파일을 만들 수 있습니다.
package main import ( "image" "image/draw" "image/png" "os" ) func writeIcoFile(icon image.Image, filename string) error { file, err := os.Create(filename) if err != nil { return err } defer file.Close() // Create icon file header iconSize := icon.Bounds().Size() fileHeader := new(resourceIconFileHeader) fileHeader.Reserved = 0 fileHeader.Type = 1 fileHeader.Count = 1 // Create icon directory entry dirEntry := new(resourceIconDirectoryEntry) dirEntry.Width = uint8(iconSize.X) dirEntry.Height = uint8(iconSize.Y) dirEntry.Colors = 0 dirEntry.Reserved = 0 dirEntry.Plane = 1 dirEntry.BitCount = 32 dirEntry.SizeInBytes = uint32(40 + 4*iconSize.X*iconSize.Y) dirEntry.Offset = 22 // Create bitmap info header and color mask for bitmap graphics colorMask := [12]byte{0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00} infoHeader := new(bitmapInfoHeader) infoHeader.Size = 40 infoHeader.Width = int32(iconSize.X) infoHeader.Height = int32(2 * iconSize.Y) infoHeader.Planes = 1 infoHeader.BitCount = 32 infoHeader.Compression = 0 infoHeader.SizeImage = uint32(4 * iconSize.X * iconSize.Y) infoHeader.XPelsPerMeter = 0 infoHeader.YPelsPerMeter = 0 infoHeader.ClrUsed = 0 infoHeader.ClrImportant = 0 // Write icon file header, directory entry, bitmap info header and color mask binary.Write(file, binary.LittleEndian, fileHeader) binary.Write(file, binary.LittleEndian, dirEntry) binary.Write(file, binary.LittleEndian, infoHeader) binary.Write(file, binary.LittleEndian, colorMask) // Write bitmap graphics rgba := image.NewRGBA(image.Rect(0, 0, iconSize.X, 2*iconSize.Y)) draw.Draw(rgba, rgba.Bounds(), image.Black, image.ZP, draw.Src) draw.Draw(rgba, image.Rect(0, 0, iconSize.X, iconSize.Y), icon, image.ZP, draw.Over) draw.Draw(rgba, image.Rect(0, iconSize.Y, iconSize.X, 2*iconSize.Y), image.Transparent, image.ZP, draw.Src) err = png.Encode(file, rgba) if err != nil { return err } return nil }
이 함수는 ICO 파일 헤더를 생성하고 여기에 아이콘 리소스를 첨부합니다. ICO 파일 헤더에는 ICO 파일의 아이콘 리소스에 대한 필수 정보가 포함되어 있습니다.
다음으로 리소스 파일에 씁니다. 이를 수행하려면 Windows API의 "BeginUpdateResource", "UpdateResource" 및 "EndUpdateResource" 함수를 사용해야 합니다.
func updateIcon(exePath, icoPath string, iconIndex int) error { exeFile, err := os.OpenFile(exePath, os.O_RDWR, 0666) defer exeFile.Close() if err != nil { return err } icoFile, err := os.Open(icoPath) defer icoFile.Close() if err != nil { return err } // Read ICO file and prepare icon directory entry icoData, err := ioutil.ReadAll(icoFile) if err != nil { return err } dirEntry := new(resourceIconDirectoryEntry) dirEntry.Width = 0 dirEntry.Height = 0 dirEntry.Colors = 0 dirEntry.Reserved = 0 dirEntry.Plane = 1 dirEntry.BitCount = 0 dirEntry.SizeInBytes = uint32(len(icoData)) dirEntry.Offset = 22 // Find update handle exeHandle, err := syscall.CreateFile(syscall.StringToUTF16Ptr(exePath), syscall.GENERIC_READ|syscall.GENERIC_WRITE, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_ATTRIBUTE_NORMAL, 0) if err != nil { return err } defer syscall.CloseHandle(exeHandle) updateHandle, _, err := BeginUpdateResourceProc.Call(uintptr(exeHandle), 0) defer syscall.CloseHandle(syscall.Handle(updateHandle)) if updateHandle == 0 { return fmt.Errorf("BeginUpdateResourceW failed") } // Write resource to update handle success, _, err := UpdateResourceProc.Call(uintptr(updateHandle), uintptr(rtIcon), uintptr(iconIndex), 0, uintptr(unsafe.Pointer(&icoData[0])), uintptr(len(icoData))) if success == 0 { return fmt.Errorf("UpdateResourceW failed") } // Write updated icon directory entry success, _, err = UpdateResourceProc.Call(uintptr(updateHandle), uintptr(rtGroupIcon), uintptr(MAKEINTRESOURCE(iconIndex)), 0, uintptr(unsafe.Pointer(dirEntry)), uintptr(unsafe.Sizeof(*dirEntry))) if success == 0 { return fmt.Errorf("UpdateResourceW failed") } // Commit update handle success, _, err = EndUpdateResourceProc.Call(updateHandle, 0) if success == 0 { return fmt.Errorf("EndUpdateResourceW failed") } return nil }
3단계: 프로그램의 .manifest 파일 변경
새 아이콘 리소스에 액세스할 수 있도록 프로그램의 .manifest 파일을 변경해야 합니다. 이렇게 하려면 .manifest 파일에 다음을 추가해야 합니다.
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity type="win32" name="MyApplication" version="1.0.0.0" processorArchitecture="x86" /> <icon type="group">MyIconResourceIndex</icon> </assembly>
그러면 프로그램이 새 아이콘 리소스를 사용할 수 있도록 아이콘 리소스 인덱스 번호가 프로그램에 할당됩니다. golang의 os 라이브러리를 사용하여 .manifest 파일을 변경할 수 있습니다.
func updateManifest(manifestPath string, iconIndex int) error { manifestData, err := ioutil.ReadFile(manifestPath) if err != nil { return err } updatedManifest := strings.Replace(string(manifestData), "</assembly>", " <icon type="group">"+strconv.Itoa(iconIndex)+"</icon> </assembly>", 1) err = ioutil.WriteFile(manifestPath, []byte(updatedManifest), 0666) if err != nil { return err } return nil }
이제 golang과 시스템 라이브러리를 사용하여 프로그램 아이콘을 변경하는 방법을 알았습니다. 이러한 단계를 종합하여 완전한 기능을 구축합니다. 샘플 코드는 다음과 같습니다.
package main import ( "encoding/binary" "encoding/hex" "fmt" "image" "image/png" "io/ioutil" "os" "strconv" "strings" "syscall" "unsafe" ) const ( rtIcon = 14 rtGroupIcon = rtIcon + 11 LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020 ) // Resource header type resourceHeader struct { RootID uint16 RootType uint16 RootName [16]uint16 RootDataSize uint32 RootDataVer uint32 RootDate uint32 RootRMLow uint16 RootRMHigh uint16 RootLangID uint16 RootDataVerOS uint32 } // Resource directory header type resourceDirectoryHeader struct { Characteristics uint32 TimeDateStamp uint32 VersionMajor uint16 VersionMinor uint16 NumNamedEntries uint16 NumIDEntries uint16 } // Resource directory entry type resourceDirectoryEntry struct { nameIsString bool ID uint32 offset uint32 nameOffset uint32 name string } // Resource icon file header type resourceIconFileHeader struct { Reserved uint16 Type uint16 Count uint16 } // Resource icon directory entry type resourceIconDirectoryEntry struct { Width uint8 Height uint8 Colors uint8 Reserved uint8 Plane uint16 BitCount uint16 SizeInBytes uint32 Offset uint32 } // Resource group icon directory header type resourceGroupIconDirectoryHeader struct { Width uint16 Height uint16 ColorCount uint16 Reserved uint16 Planes uint16 BitCount uint16 Count uint32 } // Resource group icon directory entry type resourceGroupIconDirectoryEntry struct { Width uint8 Height uint8 ColorCount uint8 Reserved uint8 Planes uint16 BitCount uint16 BytesInRes uint32 ID uint16 } // Bitmap header type bitmapInfoHeader struct { Size uint32 Width int32 Height int32 Planes uint16 BitCount uint16 Compression uint32 SizeImage uint32 XPelsPerMeter int32 YPelsPerMeter int32 ClrUsed uint32 ClrImportant uint32 } var ( kernel32DLL = syscall.MustLoadDLL("kernel32.dll") user32DLL = syscall.MustLoadDLL("user32.dll") autoDetectEncodingProc = kernel32DLL.MustFindProc("AutoDetectEncoding") BeginUpdateResourceProc = kernel32DLL.MustFindProc("BeginUpdateResourceW") LoadImageProc = user32DLL.MustFindProc("LoadImageW") ResourceNotFound = fmt.Errorf("Resource not found") NoIconFound = fmt.Errorf("Icon not found") ) func main() { exePath := "path/to/program.exe" icoPath := "path/to/newicon.png" manifestPath := "path/to/program.exe.manifest" iconIndex, err := findIconIndex(exePath)
위 내용은 golang 프로그램 아이콘 수정의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











보안 통신에 널리 사용되는 오픈 소스 라이브러리로서 OpenSSL은 암호화 알고리즘, 키 및 인증서 관리 기능을 제공합니다. 그러나 역사적 버전에는 알려진 보안 취약점이 있으며 그 중 일부는 매우 유해합니다. 이 기사는 데비안 시스템의 OpenSSL에 대한 일반적인 취약점 및 응답 측정에 중점을 둘 것입니다. DebianopensSL 알려진 취약점 : OpenSSL은 다음과 같은 몇 가지 심각한 취약점을 경험했습니다. 심장 출혈 취약성 (CVE-2014-0160) :이 취약점은 OpenSSL 1.0.1 ~ 1.0.1F 및 1.0.2 ~ 1.0.2 베타 버전에 영향을 미칩니다. 공격자는이 취약점을 사용하여 암호화 키 등을 포함하여 서버에서 무단 읽기 민감한 정보를 사용할 수 있습니다.

Go Language의 부동 소수점 번호 작동에 사용되는 라이브러리는 정확도를 보장하는 방법을 소개합니다.

Go Crawler Colly의 대기열 스레딩 문제는 Colly Crawler 라이브러리를 GO 언어로 사용하는 문제를 탐구합니다. � ...

이 기사는 데비안 시스템에서 PostgresQL 데이터베이스를 모니터링하는 다양한 방법과 도구를 소개하여 데이터베이스 성능 모니터링을 완전히 파악할 수 있도록 도와줍니다. 1. PostgreSQL을 사용하여 빌드 인 모니터링보기 PostgreSQL 자체는 데이터베이스 활동 모니터링 활동을위한 여러보기를 제공합니다. PG_STAT_REPLICATION : 특히 스트림 복제 클러스터에 적합한 복제 상태를 모니터링합니다. PG_STAT_DATABASE : 데이터베이스 크기, 트랜잭션 커밋/롤백 시간 및 기타 주요 지표와 같은 데이터베이스 통계를 제공합니다. 2. 로그 분석 도구 PGBADG를 사용하십시오

백엔드 학습 경로 : 프론트 엔드에서 백엔드 초보자로서 프론트 엔드에서 백엔드까지의 탐사 여행은 프론트 엔드 개발에서 변화하는 백엔드 초보자로서 이미 Nodejs의 기초를 가지고 있습니다.

Go Language의 문자열 인쇄의 차이 : println 및 String () 함수 사용 효과의 차이가 진행 중입니다 ...

Go Language에서 메시지 대기열을 구현하기 위해 Redisstream을 사용하는 문제는 Go Language와 Redis를 사용하는 것입니다 ...

Beegoorm 프레임 워크에서 모델과 관련된 데이터베이스를 지정하는 방법은 무엇입니까? 많은 Beego 프로젝트에서는 여러 데이터베이스를 동시에 작동해야합니다. Beego를 사용할 때 ...
