선언:
C#
[DllImport("shell32", CharSet = CharSet.Unicode)]
public static extern IntPtr CommandLineToArgv([MarshalAs(UnmanagedType.LPWStr)] String lpCommandLine, out Int32 lpNumArgs);
VB.NET
<DllImport("shell32", CharSet := CharSet.Unicode)> _
Public Shared Function CommandLineToArgv(<MarshalAs(UnmanagedType.LPWStr)> lpCommandLine As [String], ByRef lpNumArgs As Int32) As IntPtr
End Function
사용 예제:
C#
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace ApiReference {
class ApiExample {
[DllImport("shell32", CharSet = CharSet.Unicode)]
public static extern IntPtr CommandLineToArgv([MarshalAs(UnmanagedType.LPWStr)] String lpCommandLine, out Int32 lpNumArgs);
public static void Main(string[] args) {
Console.WriteLine("파싱할 문자열을 입력하세요:");
inputAgain:
String stringToParse = Console.ReadLine().Trim();
if ( String.IsNullOrEmpty(stringToParse) ) {
Console.WriteLine("다시 입력하세요:");
goto inputAgain;
}
Int32 nArgs;
IntPtr pArgs;
// 명령줄 인수를 파싱한다.
pArgs = CommandLineToArgv(stringToParse, out nArgs);
// 실패한 경우 예외 출력
if ( pArgs == IntPtr.Zero )
throw new Win32Exception();
// 파싱된 명령줄을 출력한다.
try {
for ( Int32 i = 0; i < nArgs; i++ ) {
IntPtr lpArg = Marshal.ReadIntPtr(pArgs, i * IntPtr.Size);
Console.WriteLine("{0}\t\t{1}", i, Marshal.PtrToStringUni(lpArg));
}
} finally {
Marshal.FreeHGlobal(pArgs);
}
Console.ReadKey(true);
}
}
}
VB.NET
Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Namespace ApiReference
Class ApiExample
<DllImport("shell32", CharSet := CharSet.Unicode)> _
Public Shared Function CommandLineToArgv(<MarshalAs(UnmanagedType.LPWStr)> lpCommandLine As [String], ByRef lpNumArgs As Int32) As IntPtr
End Function
Public Shared Sub Main(args As String())
Console.WriteLine("파싱할 문자열을 입력하세요:")
inputAgain:
Dim stringToParse As [String] = Console.ReadLine().Trim()
If [String].IsNullOrEmpty(stringToParse) Then
Console.WriteLine("다시 입력하세요:")
GoTo inputAgain
End If
Dim nArgs As Int32
Dim pArgs As IntPtr
' 명령줄 인수를 파싱한다.
pArgs = CommandLineToArgv(stringToParse, nArgs)
' 실패한 경우 예외 출력
If pArgs = IntPtr.Zero Then
Throw New Win32Exception()
End If
' 파싱된 명령줄을 출력한다.
Try
For i As Int32 = 0 To nArgs - 1
Dim lpArg As IntPtr = Marshal.ReadIntPtr(pArgs, i * IntPtr.Size)
Console.WriteLine("{0}" & vbTab & vbTab & "{1}", i, Marshal.PtrToStringUni(lpArg))
Next
Finally
Marshal.FreeHGlobal(pArgs)
End Try
Console.ReadKey(True)
End Sub
End Class
End Namespace
예제 실행 결과:
매개 변수 설명:
lpCommandLine - 파싱할 문자열을 입력합니다.
lpNumArgs - 매개변수의 갯수가 저장될 변수를 입력합니다.
API 설명:
문자열을 명령줄 인수로 파싱합니다.
참고:
CommandLineToArgvW (MSDN)
요구 사항:
클라이언트: Windows 2000, Windows XP+
서버: Windows 2000 Server, Windows Server 2003+
비고:
이 API는 유니코드(Unicode) 만 지원합니다.
역슬래시, 따옴표 등을 표시하려면 \", \\ 와 같은 형식으로 입력하시면 됩니다.
이 API의 반환 값은 파싱된 문자열 배열의 주소입니다. 그래서 Marshal 클래스의 ReadIntPtr, PtrToStringUni 를 사용하여 문자열로 변경하였습니다.
API 호출 결과로 반환된 포인터는 사용이 끝난 시기에 FreeHGlobal 메서드를 이용하여 메모리에서 해제해야 합니다.
* 파싱 클래스를 따로 만들 필요가 없이 이 API 하나면 OK 입니다. 정말 유용하게 사용됩니다~~!!
'API Reference' 카테고리의 다른 글
54. ShowWindow (0) | 2015.01.04 |
---|---|
53. FindWindow (1) | 2015.01.04 |
51. GetProcessMemoryInfo (0) | 2014.12.12 |
50. PROCESS_MEMORY_COUNTERS_EX (0) | 2014.12.12 |
49. PROCESS_MEMORY_COUNTERS (0) | 2014.12.11 |