Module: Guid_Win32_

Included in:
Guid
Defined in:
lib/guid.rb

Constant Summary collapse

PROV_RSA_FULL =
1
CRYPT_VERIFYCONTEXT =
0xF0000000
FORMAT_MESSAGE_IGNORE_INSERTS =
0x00000200
FORMAT_MESSAGE_FROM_SYSTEM =
0x00001000
CryptAcquireContext =
Win32API.new("advapi32", "CryptAcquireContext",
'PPPII', 'L')
CryptGenRandom =
Win32API.new("advapi32", "CryptGenRandom", 
'LIP', 'L')
CryptReleaseContext =
Win32API.new("advapi32", "CryptReleaseContext",
'LI', 'L')
GetLastError =
Win32API.new("kernel32", "GetLastError", '', 'L')
FormatMessageA =
Win32API.new("kernel32", "FormatMessageA",
'LPLLPLPPPPPPPP', 'L')

Instance Method Summary collapse

Instance Method Details

#initializeObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/guid.rb', line 47

def initialize
  hProvStr = " " * 4
  if CryptAcquireContext.call(hProvStr, nil, nil, PROV_RSA_FULL,
                              CRYPT_VERIFYCONTEXT) == 0
    raise SystemCallError, "CryptAcquireContext failed: #{lastErrorMessage}"
  end
  hProv, = hProvStr.unpack('L')
  @bytes = " " * 16
  if CryptGenRandom.call(hProv, 16, @bytes) == 0
    raise SystemCallError, "CryptGenRandom failed: #{lastErrorMessage}"
  end
  if CryptReleaseContext.call(hProv, 0) == 0
    raise SystemCallError, "CryptReleaseContext failed: #{lastErrorMessage}"
  end
end

#lastErrorMessageObject



37
38
39
40
41
42
43
44
45
# File 'lib/guid.rb', line 37

def lastErrorMessage
  code = GetLastError.call
  msg = "\0" * 1024
  len = FormatMessageA.call(FORMAT_MESSAGE_IGNORE_INSERTS +
                            FORMAT_MESSAGE_FROM_SYSTEM, 0,
                            code, 0, msg, 1024, nil, nil,
                            nil, nil, nil, nil, nil, nil)
  msg[0, len].tr("\r", '').chomp
end