【问题标题】:Executable behaving differently when run through WMI compared to run locally与在本地运行相比,通过 WMI 运行时可执行文件的行为不同
【发布时间】:2014-07-02 12:38:05
【问题描述】:

我将以下 c sharp 文件编译为可执行文件。

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            Thread.Sleep(5000);
        }
    }
  }
}

当我在本地机器上运行这个可执行文件时,它会生成当前窗口的日期和名称。

当我使用 wmi 从远程机器运行这个可执行文件时,它会产生日期并且当前窗口的名称是空白的,我认为这意味着它返回 null。有没有人解决这个问题?

运行 wmi 可执行文件的程序是用 python 编写的,格式如下:

import wmi

IP         = '192.168.165.x'
USERNAME   = 'username'
PASSWORD   = 'password'
REMOTE_DIR = 'c:\ ... \'

remote_pc = wmi.WMI (IP, user = USERNAME, password = PASSWORD)

exe_remote_path = join (['\\\\', IP, '\\', REMOTE_DIR, filename)

remote_pc.Win32_Process.Create (CommandLine = exe_remote_path)

【问题讨论】:

    标签: c# python python-3.x wmi executable


    【解决方案1】:

    这可能是问题.....

    出于安全原因,Win32_Process.Create 方法不能用于远程启动交互式进程。

    来自msdn

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2020-12-26
      • 1970-01-01
      • 2019-11-23
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多