-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineToArgv.lua
More file actions
40 lines (38 loc) · 1.24 KB
/
CommandLineToArgv.lua
File metadata and controls
40 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local ffi = require"ffi"
local Shell32,C = ffi.load"Shell32",ffi.C
ffi.cdef[[
// https://learn.microsoft.com/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw
LPWSTR * CommandLineToArgvW(
LPCWSTR lpCmdLine,
int *pNumArgs // [out]
);
// https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-lstrlenw
int lstrlenW(
LPCWSTR lpString
);
]]
local wsize = ffi.sizeof"wchar_t"
local function CommandLineToArgv (CommandLine)
local pNumArgs = ffi.new("int[1]")
local CommandlineW = win.Utf8ToUtf16(CommandLine).."\0"
local Argv = Shell32.CommandLineToArgvW(ffi.cast("LPCWSTR",CommandlineW), pNumArgs)
if Argv~=nil then
local argv = {}
for i=0,pNumArgs[0]-1 do
argv[i] = win.Utf16ToUtf8(ffi.string(Argv[i], C.lstrlenW(Argv[i])*wsize))
end
return argv
end
error("Internal error: CommandLineToArgvW failed")
end
if _cmdline=="" then
print "Export: arg[] = sh.CommandLineToArgv(string)"
print "Splits commandline into separate arguments"
print "Example:"
print("sh.CommandLineToArgv([["..[[1 "2 a" \"3" "b\"]].."]])", "=>",
sh.dump(CommandLineToArgv([[1 "2 a" \"3" "b\"]])))
elseif _cmdline then -- test
print(unpack(CommandLineToArgv(_cmdline), 0))
else -- export
return CommandLineToArgv
end