Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert C# EXE to Assembly
[DllImport("shell32.dll", SetLastError = true)]
static extern IntPtr CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
public static string[] CommandLineToArgs(string commandLine)
{
int argc;
var argv = CommandLineToArgvW(commandLine, out argc);
if (argv == IntPtr.Zero)
throw new System.ComponentModel.Win32Exception();
try
{
var args = new string[argc];
for (var i = 0; i < args.Length; i++)
{
var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
args[i] = Marshal.PtrToStringUni(p);
}
return args;
}
finally
{
Marshal.FreeHGlobal(argv);
}
}
public static string Execute(string commandLine)
{
var sw = new StringWriter();
Console.SetOut(sw);
Console.SetError(sw);
Main(CommandLineToArgs(commandLine));
return sw.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment