Shell32.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Shell32
  2. //
  3. // Douglas Thrift
  4. //
  5. // Shell32.cs
  6. /* Copyright 2014 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Runtime.InteropServices;
  24. public static class Shell32
  25. {
  26. public static IList<string> CommandLineToArgv(string cmdLine)
  27. {
  28. int numArgs;
  29. var argv = CommandLineToArgvW(cmdLine, out numArgs);
  30. if (argv == IntPtr.Zero)
  31. throw new Win32Exception();
  32. try
  33. {
  34. string[] args = new string[numArgs];
  35. for (int index = 0; index != numArgs; ++index)
  36. args[index] = Marshal.PtrToStringUni(Marshal.ReadIntPtr(argv, index * IntPtr.Size));
  37. return args;
  38. }
  39. finally
  40. {
  41. Marshal.FreeHGlobal(argv);
  42. }
  43. }
  44. [DllImport("shell32.dll", SetLastError = true)]
  45. private static extern IntPtr CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
  46. }