Skip to content

Commit

Permalink
simplified locking strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedonovan committed Jun 22, 2012
1 parent bff8d39 commit 27fecfe
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 130 deletions.
12 changes: 5 additions & 7 deletions examples/caption.lua
@@ -1,8 +1,6 @@
local W = require 'winapi'
W.set_encoding(W.CP_UTF8)
console = W.get_foreground_window()
console:set_text 'ελληνική'
W.set_clipboard 'ελληνική'
print 'Press any key'
local W = require 'winapi'
local console = W.get_foreground_window()
console:set_text 'e???????'
W.set_clipboard 'e???????'
print 'Press enter'
io.read()

33 changes: 17 additions & 16 deletions examples/event.lua
@@ -1,18 +1,19 @@
local W = require 'winapi'
local e = W.event()
local count = 1
local W = require 'winapi'
local e = W.event()
local count = 1
local finished

W.make_timer(500,function()
print 'finis'
if count == 5 then
print 'finished!'
os.exit()
end
e:signal()
count = count + 1
end)

while true do
e:wait()
print 'gotcha'
W.make_timer(500,function()
print 'tick'
if count == 5 then
print 'finished!'
finished = true
end
e:signal()
count = count + 1
end)

while not finished do
e:wait()
print 'gotcha'
end
16 changes: 14 additions & 2 deletions examples/test-reg.lua
Expand Up @@ -2,13 +2,25 @@ require 'winapi'
k,err = winapi.open_reg_key [[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers]]
if not k then return print('bad key',err) end

print(k:get_value(""))
print(k:get_value("1"))
k:close()

k,err = winapi.open_reg_key [[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion]]

t = k:get_keys()
for _,k in ipairs(t) do print(_,k) end
--for _,k in ipairs(t) do print(_,k) end
k:close()

k,err = winapi.open_reg_key ([[HKEY_CURRENT_USER\Environment]],true)
path = k:get_value("PATH")
print(path)
if #arg > 0 then
--print(k:set_value("PATH",path..';'..arg[1]))
--print(k:get_value("PATH"))
k:set_value(arg[1],arg[2])
print(k:get_value(arg[1]))
end
k:close()



4 changes: 4 additions & 0 deletions examples/test-timer.lua
Expand Up @@ -4,15 +4,19 @@ local t1,t2
t1 = winapi.make_timer(500,function()
print 'gotcha'
end)

local k = 1
t2 = winapi.make_timer(400,function()
k = k + 1
print(k)
if k > 5 then
print 'killing'
t1:kill() -- kill the first timer
t2 = nil
return true -- and we will end now
end
end)

winapi.make_timer(1000,function()
print 'doo'
if not t2 then os.exit(0) end -- we all finish
Expand Down
18 changes: 17 additions & 1 deletion readme.md
Expand Up @@ -390,11 +390,27 @@ The console handle is signalled as soon as you type any character, but the read

## Reading from the Registry

The registry is an unavoidable part of living with Windows, since much useful information can be found in it, if you know the key.

For instance, the environment for the _current user_ can be queried:

local key = winapi.open_reg_key [[HKEY_CURRENT_USER\Environment]]
print(key:get_value("PATH"))
k:close()

And `Regkey:set_value` will then allow you to update this path, which is useful for install programs. In that case, set the optional second argument to `true` to get write-access.

Please note that the data must be a plain ASCII string currently.

This comment has been minimized.

Copy link
@moteus

moteus Jun 22, 2012

to set path like %USERPROFILE%\Local Settings\Temp the type of value in registry mast be EXPAND_SZ.
So set_value need second parameter - value type. See winreg (http://luaforge.net/projects/jaslatrix)


`Regkey:get_keys` will return a list of all subkeys of the current key.

When finished, it's best to explicitly use the `close` method.

## Pipe Server

Interprocess communication (IPC) is one of those tangled, operating-system-dependent things that can be terribly useful. On Unix, _named pipes_ are special files which can be used for two processes to easily exchange information. One process opens the pipe for reading, and the other process opens it for writing; the first process will start reading, and this will block until the other process writes to the pipe. Since pipes are a regular part of the filesystem, two Lua scripts can use regular I/O to complete this transaction.

Life is more complicated on Windows (as usual) but with a little bit of help from the API you can get the equivalent mechanism from Windows named pipes. They do work a little differently; they are more like Unix domain sockets; a server waits for a client to connect ('accept') and then produces a handle for the new client to use; it then goes back to waiting for connections.
Life is more complicated on Windows (as usual) but with a little bit of help from the API you can get the equivalent mechanism from Windows named pipes. They do work differently; they are more like Unix domain sockets; a server waits for a client to connect ('accept') and then produces a handle for the new client to use; it then goes back to waiting for connections.

require 'winapi'

Expand Down

0 comments on commit 27fecfe

Please sign in to comment.