Msvm_Keyboard 클래스의 TypeText 메서드
일련의 형식화된 문자를 시뮬레이션합니다. 이는 문자열의 각 문자에 대해 PressKey 뒤에 ReleaseKey 를 호출하는 것과 같습니다.
구문
uint32 TypeText(
[in] string asciiText
);
매개 변수
-
asciiText [in]
-
형식: 문자열
입력할 일련의 ASCII 또는 유니코드 문자입니다. 이 문자열의 최대 길이는 문자열의 문자 형식에 따라 달라집니다.
string 형식 최대 문자 ASCII 512 Unicode 문자열의 서로게이트 쌍 수에 따라 256~512개입니다.
반환 값
형식: uint32
반환 값이 0이면 성공이 표시됩니다. 반환 값이 1이면 입력 문자열에서 변환할 수 없는 문자로 인해 오류가 발생했음을 나타냅니다. 다른 모든 0이 아닌 값은 키 상태를 수정하지 못했음을 나타냅니다.
-
오류 없이 완료됨 (0)
-
메서드 매개 변수 선택됨 - 작업 시작 됨(4096)
-
실패( 32768)
-
액세스 거부됨 (32769)
-
지원되지 않음 (32770)
-
상태를 알 수 없음 (32771)
-
시간 제한 (32772)
-
잘못된 매개 변수 (32773)
-
시스템이 사용 중임 (32774)
-
이 작업에 대한 잘못된 상태 (32775)
-
잘못된 데이터 형식 (32776)
-
시스템을 사용할 수 없음 (32777)
-
메모리 부족 (32778)
설명
Msvm_Keyboard 클래스에 대한 액세스는 UAC 필터링에 의해 제한될 수 있습니다. 자세한 내용은 사용자 계정 컨트롤 및 WMI를 참조하세요.
예제
다음 C# 샘플은 텍스트 입력을 시뮬레이션합니다. 참조된 유틸리티는 V2(가상화 샘플)에 대한 공통 유틸리티에서 찾을 수 있습니다.
using System;
using System.Management;
namespace HyperVSamples
{
class TypeTextClass
{
static ManagementObject GetComputerKeyboard(ManagementObject vm)
{
ManagementObjectCollection keyboardCollection = vm.GetRelated
(
"Msvm_Keyboard",
"Msvm_SystemDevice",
null,
null,
"PartComponent",
"GroupComponent",
false,
null
);
ManagementObject keyboard = null;
foreach (ManagementObject instance in keyboardCollection)
{
keyboard = instance;
break;
}
return keyboard;
}
static void TypeText(string vmName, string text)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObject keyboard = GetComputerKeyboard(vm);
ManagementBaseObject inParams = keyboard.GetMethodParameters("TypeText");
inParams["asciiText"] = text;
ManagementBaseObject outParams = keyboard.InvokeMethod("TypeText", inParams, null);
if ((UInt16)outParams["ReturnValue"] == ReturnCode.Completed)
{
string.Format("Text {0} was typed on {1}", text, vm["ElementName"]);
}
else
{
string.Format("Unable to type '{0}' on {1}", text, vm["ElementName"]);
}
inParams.Dispose();
outParams.Dispose();
keyboard.Dispose();
vm.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: TypeText vmName Text");
return;
}
TypeText(args[0], args[1]);
}
}
}
다음 VBScript(Visual Basic Scripting Edition) 샘플은 입력 텍스트를 시뮬레이트합니다.
option explicit
dim objWMIService
dim fileSystem
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main routine
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, vmName, computerSystem, text, keyboard
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization\v2")
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 2 then
vmName= objArgs.Unnamed.Item(0)
text = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript TypeText.vbs vmName text"
WScript.Quit
end if
set computerSystem = GetComputerSystem(vmName)
set keyboard = GetComputerKeyboard(computerSystem)
if TypeText(keyboard, text) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "TypeText operation failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
dim query
On Error Resume Next
query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Retrieve Msvm_Keyboard from given computer system
'
'-----------------------------------------------------------------
Function GetComputerKeyboard(computerSystem)
dim query
On Error Resume Next
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_Keyboard", computerSystem.Path_.Path)
set GetComputerKeyboard = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Type the given text to the given keyboard
'-----------------------------------------------------------------
Function TypeText(keyboard, text)
WriteLog Format2("TypeText({0}, {1})", keyboard.ElementName, text)
dim objInParam, objOutParams
TypeText = false
set objInParam = keyboard.Methods_("TypeText").InParameters.SpawnInstance_()
objInParam.asciiText = text
set objOutParams = keyboard.ExecMethod_("TypeText", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
WriteLog Format2("'{0}' was typed on {1}", text, keyboard.ElementName)
TypeText = true
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\Typetext.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format2(myString, arg0, arg1)
Format2 = Format1(myString, arg0)
Format2 = Replace(Format2, "{1}", arg1)
End Function
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
Format1 = Replace(myString, "{0}", arg0)
End Function
요구 사항
요구 사항 | 값 |
---|---|
지원되는 최소 클라이언트 |
Windows 8 [데스크톱 앱만 해당] |
지원되는 최소 서버 |
Windows Server 2012 [데스크톱 앱만 해당] |
네임스페이스 |
Root\Virtualization\V2 |
MOF |
|
DLL |
|