在PowerShell中以编程方式转储路径时,将跳过重复字符。 [英] Repeat character is getting skipped while dumping path programmatically in powershell.

查看:134
本文介绍了在PowerShell中以编程方式转储路径时,将跳过重复字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SendMessage API将一个脚本文件路径中的字符逐个转储到powershell控制台上。



在第一次执行程序时,powershell会跳过重复的字符从产生问题的路径。



例如C:\ myFolder \abbc\test.ps1



从上面的路径中,当我将字符转储到powershell窗口时,它从abbc中跳过1 b 所以在控制台上转储的最终路径是C:\ myFolder \abc\test.ps1。



只有首次执行应用程序才会发生这种情况,在随后的执行中,它工作正常并接受重复的字符。



我尝试过:



我尝试在倾倒其他角色之前加入1秒的睡眠时间。





  for  int  i =  0 ; scriptPath [i]!= '  \ 0 '; i ++)
{
:: SendMessage(g_hwndConsole,WM_CHAR,scriptPath [i],1L);
睡眠( 3 * 1000 );
}

这里 g_hwndConsole 是powershell控制台句柄

[/ EDIT]

解决方案

正如我在评论中已经提到的那样:

当接收窗口不属于接收窗口时,切勿使用 SendMessage 发送消息的线程。请改用 PostMessage 。也不需要设置一个重复计数。请改为尝试:

 :: PostMessage(g_hwndConsole,WM_CHAR,scriptPath [i],0); 


< blockquote>请参阅 WM_CHAR消息(Windows) [ ^ ]正确解释此消息。


I am dumping characters one by one of a script file path onto powershell console using SendMessage API.

At the first execution of program, powershell skips the repeated character from a path which creates an issue.

For e.g. "C:\myFolder\abbc\test.ps1"

From above path when I dump a characters onto powershell window, it skips 1 b from "abbc" so the final path that gets dumped on console is "C:\myFolder\abc\test.ps1".

This happens only first execution of application, on subsequent executions it works fine and accepts repeat characters as well.

What I have tried:

I tried adding sleep time of 1 seconds after before dumping other character.

[EDIT by Jochen Arndt: Copied code and text from comment]

for(int i = 0;scriptPath[i] != '\0';i++)
{
    ::SendMessage(g_hwndConsole,WM_CHAR,scriptPath[i],1L);
    Sleep(3*1000);
}

Here g_hwndConsole is the powershell console handle
[/EDIT]

解决方案

As already mentioned in my comment:
Never use SendMessage when the receiving window is not owned by the thread sending the message. Use PostMessage instead. There should be also no need to set a repeat count of one. Try this instead:

::PostMessage(g_hwndConsole, WM_CHAR, scriptPath[i], 0);


See WM_CHAR message (Windows)[^] for a proper explanation of this message.


这篇关于在PowerShell中以编程方式转储路径时,将跳过重复字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆