Skip to content

feat: fix encoding for Windows tasklist output in IsWindowTitleActive() - #132

Merged
nomeguy merged 3 commits into
masterfrom
copilot/fix-encoding-window-title-output
Nov 19, 2025
Merged

feat: fix encoding for Windows tasklist output in IsWindowTitleActive()#132
nomeguy merged 3 commits into
masterfrom
copilot/fix-encoding-window-title-output

Conversation

Copilot AI commented Nov 19, 2025

Copy link
Copy Markdown
Contributor

Windows tasklist command outputs text in the system's default codepage (typically GBK for Chinese systems), but Go reads it as UTF-8, causing garbled text that breaks string comparison with window titles.

Changes

  • Decode tasklist output from GBK to UTF-8 using golang.org/x/text/encoding/simplifiedchinese
  • Add fallback to raw output if decoding fails
// Before: garbled Chinese characters cause string match to fail
output := out.String()

// After: properly decoded output
decoder := simplifiedchinese.GBK.NewDecoder()
reader := transform.NewReader(&out, decoder)
decoded, err := io.ReadAll(reader)
if err != nil {
    decoded = out.Bytes()
}
output := string(decoded)

This allows the function to correctly match window titles containing non-ASCII characters.

Original prompt

This section details on the original issue you should resolve

<issue_title>[bug] wrong encoding for IsWindowTitleActive() output</issue_title>
<issue_description>```go
func IsWindowTitleActive(name string) (bool, error) {
name = getMappedName(name)
windowName := fmt.Sprintf("%s.bat - %s", name, getShortcut())

// Use tasklist to check if a window with the specific title exists
cmd := exec.Command("tasklist", "/V", "/FI", fmt.Sprintf("WINDOWTITLE eq %s", windowName))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return false, err
}

output := out.String()
// Check if cmd.exe process with the window title exists
// If window title is found, output will contain "cmd.exe" and the window title
res := strings.Contains(output, "cmd.exe") && strings.Contains(output, windowName)
return res, nil
}


in above code, output is wrong encoding, it's:

ӳ������ PID �Ự�� �Ự# �ڴ�ʹ�� ״̬ �û��� CPU ʱ�� ���ڱ���
========================= ======== ================ =========== ============ =============== ================================================== ============ ========================================================================
cmd.exe 10872 RDP-Tcp#0 1 4,912 K Running MACHINE\Administrator 0:00:00 casibase.bat - ���ݷ�ʽ


but windowName var is correct encoding, so the contains func return false wrongly.

need to fix encoding</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@nomeguy
nomeguy marked this pull request as ready for review November 19, 2025 15:06
Copilot AI and others added 2 commits November 19, 2025 15:10
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong encoding for IsWindowTitleActive() output Fix encoding for Windows tasklist output in IsWindowTitleActive() Nov 19, 2025
Copilot AI requested a review from nomeguy November 19, 2025 15:17
@nomeguy nomeguy changed the title Fix encoding for Windows tasklist output in IsWindowTitleActive() fix: fix encoding for Windows tasklist output in IsWindowTitleActive() Nov 19, 2025
@nomeguy nomeguy changed the title fix: fix encoding for Windows tasklist output in IsWindowTitleActive() feat: fix encoding for Windows tasklist output in IsWindowTitleActive() Nov 19, 2025
@nomeguy
nomeguy merged commit fee3995 into master Nov 19, 2025
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] wrong encoding for IsWindowTitleActive() output

3 participants