cstruct 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/doc/documents.html +572 -0
  2. data/doc/examples/anonymous_struct.rb.html +94 -0
  3. data/doc/examples/anonymous_union.rb.html +93 -0
  4. data/doc/examples/array_member.rb.html +79 -0
  5. data/doc/examples/file_io.rb.html +87 -0
  6. data/doc/examples/first_example.rb.html +104 -0
  7. data/doc/examples/get_system_info.rb.html +114 -0
  8. data/doc/examples/get_versionex.rb.html +97 -0
  9. data/doc/examples/global_memory.rb.html +102 -0
  10. data/doc/examples/inner_struct.rb.html +79 -0
  11. data/doc/examples/inner_union.rb.html +77 -0
  12. data/doc/examples/namespace.rb.html +80 -0
  13. data/doc/examples/show_processes.rb.html +95 -0
  14. data/doc/examples/struct_member.rb.html +128 -0
  15. data/doc/examples.html +42 -0
  16. data/doc/images/Thumbs.db +0 -0
  17. data/doc/images/examples.png +0 -0
  18. data/doc/images/excample1.png +0 -0
  19. data/doc/images/excample2.png +0 -0
  20. data/doc/images/green-point.png +0 -0
  21. data/doc/images/learnmore.png +0 -0
  22. data/doc/images/logo.png +0 -0
  23. data/doc/images/news.png +0 -0
  24. data/doc/images/point.png +0 -0
  25. data/doc/images/start.png +0 -0
  26. data/doc/images/synopsish.png +0 -0
  27. data/doc/index.html +149 -0
  28. data/doc/stylesheets/coderay.css +34 -0
  29. data/doc/stylesheets/ie.css +9 -0
  30. data/doc/stylesheets/style.css +216 -0
  31. data/examples/anonymous_struct.rb +43 -0
  32. data/examples/anonymous_union.rb +42 -0
  33. data/examples/array_member.rb +29 -0
  34. data/examples/file_io.rb +49 -0
  35. data/examples/first_example.rb +53 -0
  36. data/examples/inner_struct.rb +28 -0
  37. data/examples/inner_union.rb +26 -0
  38. data/examples/namespace.rb +29 -0
  39. data/examples/struct_member.rb +77 -0
  40. data/examples/win32/get_system_info.rb +64 -0
  41. data/examples/win32/get_versionex.rb +47 -0
  42. data/examples/win32/global_memory.rb +51 -0
  43. data/examples/win32/show_processes.rb +45 -0
  44. data/lib/cstruct.rb +523 -0
  45. data/lib/win32struct.rb +90 -0
  46. data/lib/win64struct.rb +13 -0
  47. metadata +109 -0
@@ -0,0 +1,42 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ # struct U in C\C++ (32-bit platform):
6
+ #
7
+ # struct U
8
+ # {
9
+ # union{
10
+ # int x;
11
+ # int y;
12
+ # }value; /* values is anonymous union's instance */
13
+ # };
14
+
15
+ # struct U in Ruby:
16
+ class U < CStruct
17
+ union:value do
18
+ int32:x
19
+ int32:y
20
+ end
21
+ end
22
+
23
+ # or like this (use brace):
24
+ # class U < CStruct
25
+ # union (:position) {
26
+ # int32:x
27
+ # int32:y
28
+ # }
29
+ # end
30
+
31
+
32
+ # create a U's instance
33
+ u = U.new
34
+
35
+ # Notice! Assign is different from C language.
36
+ # 'value' can not be omitted !
37
+ u.value.x = 10
38
+
39
+ puts "sizeof(U) = #{U.__size__}" # "__size__" is alias of "size" . You may use 'size'.
40
+ puts "u.value.x = #{u.value.x }, u.value.y = #{u.value.y }"
41
+
42
+
@@ -0,0 +1,29 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ # struct T in C\C++ (32-bit platform):
6
+ #
7
+ # struct T
8
+ # {
9
+ # int element[8];
10
+ # };
11
+
12
+ # struct T in Ruby:
13
+ class T < CStruct
14
+ int32:elements,[8]
15
+ end
16
+
17
+ # create a T's instance
18
+ t_array = T.new
19
+
20
+ (0..7).each do |i|
21
+ t_array.elements[i] = i # assign like as C language
22
+ end
23
+
24
+ # output
25
+ (0..7).each {|i| puts "t_array.elements[#{i}] = #{t_array.elements[i]}" }
26
+
27
+
28
+ # Actually,t_array.elements.class is Array. So..
29
+ t_array.elements.each {|element| puts element }
@@ -0,0 +1,49 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # struct Point in Ruby:
5
+ class Point < CStruct
6
+ int32:x
7
+ int32:y
8
+ end
9
+
10
+ # struct PointF in Ruby:
11
+ class PointF < CStruct
12
+ double:x
13
+ double:y
14
+ end
15
+
16
+ class Addition < CStruct
17
+ char :description,[32]
18
+ end
19
+
20
+ # write file
21
+ File.open("point.bin","wb")do |f|
22
+ point = Point.new {|st| st.x = 100; st.y =200 }
23
+ point_f = PointF.new{|st| st.x = 20.65; st.y =70.86 }
24
+
25
+ addition = Addition.new
26
+ addition.description= "Hello Ruby!"
27
+
28
+ f.write point.data
29
+ f.write point_f.data
30
+ f.write addition.data
31
+
32
+ end
33
+
34
+ #read file
35
+ File.open("point.bin","rb")do |f|
36
+ point = Point.new
37
+ point_f = PointF.new
38
+ addition = Addition.new
39
+
40
+ point << f.read(Point.size)
41
+ point_f << f.read(PointF.size)
42
+ addition << f.read(Addition.size)
43
+
44
+ puts point.x
45
+ puts point.y
46
+ puts point_f.x
47
+ puts point_f.y
48
+ puts addition.description.to_cstr
49
+ end
@@ -0,0 +1,53 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ # struct Point in C\C++ (32-bit platform):
6
+ #
7
+ # struct Point
8
+ # {
9
+ # int x;
10
+ # int y;
11
+ # };
12
+
13
+ # struct Point in Ruby:
14
+ class Point < CStruct
15
+ int32:x
16
+ int32:y
17
+ end
18
+
19
+ # create a Point's instance
20
+ point = Point.new
21
+
22
+ # assign like as C language
23
+ point.x = 10
24
+ point.y = 20
25
+
26
+ puts "point.x = #{point.x},point.y = #{point.y}"
27
+
28
+
29
+
30
+ # struct PointF in C\C++ (32-bit platform):
31
+ #
32
+ # struct PointF
33
+ # {
34
+ # double x;
35
+ # double y;
36
+ # };
37
+
38
+ # struct PointF in Ruby:
39
+ class PointF < CStruct
40
+ double:x
41
+ double:y
42
+ end
43
+
44
+ # create a PointF's instance
45
+ # use 'block' to initialize the fields
46
+ point2 = PointF.new do |st|
47
+ st.x = 10.56
48
+ st.y = 20.78
49
+ end
50
+
51
+ puts "sizeof(PointF) = #{PointF.size}"
52
+ puts "point2.x = #{point2.x},point2.y = #{point2.y}"
53
+
@@ -0,0 +1,28 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+ # example:
4
+ # struct A in C\C++ (32-bit platform):
5
+ # struct A{
6
+ # struct Inner
7
+ # {
8
+ # int v1;
9
+ # int v2;
10
+ # };
11
+ # Inner inner;
12
+ # };
13
+
14
+ # struct A in Ruby:
15
+ class A < CStruct
16
+ class Inner < CStruct
17
+ int32 :v1
18
+ int32 :v2
19
+ end
20
+ Inner :inner
21
+ end
22
+
23
+ a = A.new
24
+ a.inner.v1 = 1
25
+ a.inner.v2 = 2
26
+
27
+ puts a.inner.v1
28
+ puts a.inner.v2
@@ -0,0 +1,26 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ # struct U in C\C++ (32-bit platform):
6
+ #
7
+ #struct U
8
+ #{
9
+ # union NumericType
10
+ # {
11
+ # int x;
12
+ # int y;
13
+ # };
14
+ # NumericType value;
15
+ #};
16
+
17
+ # Named union is unsupported in CStruct. Fortunately, anonymous union can take the place of it.
18
+ # struct U in Ruby:
19
+ class U < CStruct
20
+ union:value do
21
+ int32:x
22
+ int32:y
23
+ end
24
+ end
25
+
26
+ # See also: 'anonymous_union.rb'
@@ -0,0 +1,29 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ module NS1 #namespace
6
+ class A < CStruct
7
+ uint32:handle
8
+ end
9
+
10
+ module NS2
11
+ class B < CStruct
12
+ A:a # directly use A
13
+ end
14
+ end
15
+
16
+ class C < CStruct
17
+ A :a
18
+ NS2_B :b # Meaning of the 'NS2_B' is NS2::B
19
+ end
20
+ end
21
+
22
+ class D < CStruct
23
+ NS1_NS2_B:b # Meaning of the 'NS1_NS2_B' is NS1::NS2::B
24
+ end
25
+
26
+ v = D.new
27
+ v.b.a.handle = 120
28
+ p D.__size__
29
+ p v.b.a.handle
@@ -0,0 +1,77 @@
1
+ # CStruct Examples
2
+ require 'cstruct'
3
+
4
+ # example:
5
+ # struct Point in C\C++ (32-bit platform):
6
+ #
7
+ # struct Point
8
+ # {
9
+ # int x;
10
+ # int y;
11
+ # };
12
+ #
13
+ # struct Line in C\C++ (32-bit platform):
14
+ #
15
+ # struct Line
16
+ # {
17
+ # Point begin_point;
18
+ # Point end_point;
19
+ # };
20
+
21
+ # struct TwoLine in C\C++ (32-bit platform):
22
+ #
23
+ # struct TwoLine
24
+ # {
25
+ # Line lines[2];
26
+ # };
27
+
28
+
29
+
30
+ # struct Point , Line and TwoLine in Ruby:
31
+ class Point < CStruct
32
+ int32:x
33
+ int32:y
34
+ end
35
+
36
+ class Line < CStruct
37
+ Point:begin_point
38
+ Point:end_point
39
+ end
40
+
41
+ class TwoLine < CStruct
42
+ Line:lines,[2]
43
+ end
44
+
45
+
46
+ line = Line.new
47
+
48
+ # assign like as C language
49
+ line.begin_point.x = 0
50
+ line.begin_point.y = 0
51
+ line.end_point.x = 20
52
+ line.end_point.y = 30
53
+
54
+
55
+ puts "sizeof(Line) = #{Line.__size__}" # "__size__" is alias of "size"
56
+ puts "line.begin_point.x = #{line.begin_point.x}"
57
+ puts "line.begin_point.y = #{line.begin_point.y}"
58
+ puts "line.end_point.x = #{line.end_point.x}"
59
+ puts "line.end_point.y = #{line.end_point.y}"
60
+
61
+ two_line = TwoLine.new
62
+
63
+ # assign like as C language
64
+ two_line.lines[0].begin_point.x = 6
65
+ two_line.lines[0].begin_point.y = 16
66
+ two_line.lines[0].end_point.x = 26
67
+ two_line.lines[0].end_point.y = 36
68
+
69
+ two_line.lines[1].begin_point.x = 6
70
+ two_line.lines[1].begin_point.y = 16
71
+ two_line.lines[1].end_point.x = 26
72
+ two_line.lines[1].end_point.y = 36
73
+
74
+
75
+
76
+
77
+
@@ -0,0 +1,64 @@
1
+ # CStruct Examples
2
+ require 'windows/system_info'
3
+ require 'win32struct'
4
+ include Windows::SystemInfo
5
+
6
+ # SYSTEM_INFO in VC6 's SDK
7
+ # typedef struct _SYSTEM_INFO {
8
+ # union {
9
+ # DWORD dwOemId; // Obsolete field...do not use
10
+ # struct {
11
+ # WORD wProcessorArchitecture;
12
+ # WORD wReserved;
13
+ # };
14
+ # };
15
+ # DWORD dwPageSize;
16
+ # LPVOID lpMinimumApplicationAddress;
17
+ # LPVOID lpMaximumApplicationAddress;
18
+ # DWORD dwActiveProcessorMask;
19
+ # DWORD dwNumberOfProcessors;
20
+ # DWORD dwProcessorType;
21
+ # DWORD dwAllocationGranularity;
22
+ # WORD wProcessorLevel;
23
+ # WORD wProcessorRevision;
24
+ # } SYSTEM_INFO, *LPSYSTEM_INFO;
25
+
26
+ # SYSTEM_INFO in Ruby
27
+ class SYSTEM_INFO < Win32Struct
28
+ union:u do
29
+ DWORD:dwOemId
30
+ struct:x do
31
+ WORD:wProcessorArchitecture
32
+ WORD:wReserved
33
+ end
34
+ end
35
+ DWORD :dwPageSize
36
+ LPVOID :lpMinimumApplicationAddress
37
+ LPVOID :lpMaximumApplicationAddress
38
+ DWORD :dwActiveProcessorMask
39
+ DWORD :dwNumberOfProcessors
40
+ DWORD :dwProcessorType
41
+ DWORD :dwAllocationGranularity
42
+ WORD :wProcessorLevel
43
+ WORD :wProcessorRevision
44
+ end
45
+
46
+ # call GetSystemInfo
47
+ sys_info = SYSTEM_INFO.new
48
+ GetSystemInfo(sys_info.__data__) # __data__ is an alias for method 'data'
49
+
50
+ #output
51
+ puts "<System Infomation>"
52
+ puts "Processor Architecture:#{sys_info.u.x.wProcessorArchitecture}" # 'u' and 'x' can not be omitted
53
+ puts "Page Size:#{sys_info.dwPageSize}"
54
+ puts "Minimum Application Address:0x#{sys_info.lpMinimumApplicationAddress.to_s 16}"
55
+ puts "Maximum Application Address:0x#{sys_info.lpMaximumApplicationAddress.to_s 16}"
56
+ puts "Active Processor Mask:#{sys_info.dwActiveProcessorMask}"
57
+ puts "Number Of Processors:#{sys_info.dwNumberOfProcessors}"
58
+ puts "Processor Type:#{sys_info.dwProcessorType}"
59
+ puts "Allocation Granularity:0x#{sys_info.dwAllocationGranularity.to_s 16}"
60
+ puts "Processor Level:#{sys_info.wProcessorLevel}"
61
+ puts "Processor Revision:#{sys_info.wProcessorRevision}"
62
+
63
+
64
+
@@ -0,0 +1,47 @@
1
+ # CStruct Examples
2
+ require 'windows/system_info'
3
+ require 'win32struct'
4
+ include Windows::SystemInfo
5
+
6
+
7
+ # OSVERSIONINFOEXA in VC6 's SDK
8
+ #typedef struct _OSVERSIONINFOEXA {
9
+ # DWORD dwOSVersionInfoSize;
10
+ # DWORD dwMajorVersion;
11
+ # DWORD dwMinorVersion;
12
+ # DWORD dwBuildNumber;
13
+ # DWORD dwPlatformId;
14
+ # CHAR szCSDVersion[ 128 ]; // Maintenance string for PSS usage
15
+ # WORD wServicePackMajor;
16
+ # WORD wServicePackMinor;
17
+ # WORD wReserved[2];
18
+ #} OSVERSIONINFOEXA;
19
+
20
+
21
+ class OSVERSIONINFOEXA < Win32Struct
22
+ DWORD :dwOSVersionInfoSize
23
+ DWORD :dwMajorVersion
24
+ DWORD :dwMinorVersion
25
+ DWORD :dwBuildNumber
26
+ DWORD :dwPlatformId
27
+ CHAR :szCSDVersion,[ 128 ]
28
+ WORD :wServicePackMajor
29
+ WORD :wServicePackMinor
30
+ WORD :wReserved,[2]
31
+ end
32
+
33
+ ver_info_ex = OSVERSIONINFOEXA.new do |st|
34
+ st.dwOSVersionInfoSize = OSVERSIONINFOEXA.__size__ # __size__ is an alias for method 'size'
35
+ end
36
+
37
+ #ANSI Version: GetVersionExA
38
+ GetVersionExA(ver_info_ex.__data__) # __data__ is an alias for method 'data'
39
+
40
+ puts "<OS Version Infomation>"
41
+ puts "Major Version:#{ver_info_ex.dwMajorVersion}"
42
+ puts "Minor Version:#{ver_info_ex.dwMinorVersion}"
43
+ puts "Build Number:#{ver_info_ex.dwBuildNumber}"
44
+ puts "Platform Id:#{ver_info_ex.dwPlatformId}"
45
+ puts "CSD Version:#{ver_info_ex.szCSDVersion.to_cstr}" # to_cstr return a string(C Style)
46
+ puts "ServicePack Major:#{ver_info_ex.wServicePackMajor}"
47
+ puts "ServicePack Minor:#{ver_info_ex.wServicePackMinor}"
@@ -0,0 +1,51 @@
1
+ # CStruct Examples
2
+ require 'windows/memory'
3
+ require 'win32struct'
4
+
5
+ include Windows::Memory
6
+
7
+ # example:
8
+
9
+ # typedef struct _MEMORYSTATUS {
10
+ # DWORD dwLength;
11
+ # DWORD dwMemoryLoad;
12
+ # DWORD dwTotalPhys;
13
+ # DWORD dwAvailPhys;
14
+ # DWORD dwTotalPageFile;
15
+ # DWORD dwAvailPageFile;
16
+ # DWORD dwTotalVirtual;
17
+ # DWORD dwAvailVirtual;
18
+ # } MEMORYSTATUS, *LPMEMORYSTATUS;
19
+
20
+ class MEMORYSTATUS < Win32Struct
21
+ DWORD :dwLength
22
+ DWORD :dwMemoryLoad
23
+ DWORD :dwTotalPhys
24
+ DWORD :dwAvailPhys
25
+ DWORD :dwTotalPageFile
26
+ DWORD :dwAvailPageFile
27
+ DWORD :dwTotalVirtual
28
+ DWORD :dwAvailVirtual
29
+ end
30
+
31
+
32
+
33
+ # create a MEMORYSTATUS's instance
34
+ stat = MEMORYSTATUS.new {|st| st.dwLength = MEMORYSTATUS.size }
35
+
36
+ # call API "GlobalMemoryStatus" - See also MSDN
37
+ GlobalMemoryStatus(stat.data)
38
+
39
+ #output
40
+ printf "[Physical Memory]\n"
41
+ printf " total:%12d bytes\n",stat.dwTotalPhys
42
+ printf " free :%12d bytes\n",stat.dwAvailPhys
43
+
44
+ printf "[Virtual Memory]\n"
45
+ printf " total:%12d bytes\n",stat.dwTotalVirtual
46
+ printf " free :%12d bytes\n",stat.dwAvailVirtual
47
+
48
+ printf "[Paging File]\n"
49
+ printf " total:%12d bytes\n",stat.dwTotalPageFile
50
+ printf " free :%12d bytes\n",stat.dwAvailPageFile
51
+
@@ -0,0 +1,45 @@
1
+ # CStruct Examples
2
+ require 'windows/tool_helper'
3
+ require 'win32struct'
4
+ include Windows::ToolHelper
5
+
6
+ module Windows
7
+ MAX_PATH = 260
8
+ module ToolHelper
9
+ class PROCESSENTRY32 < Win32Struct
10
+ DWORD :dwSize
11
+ DWORD :cntUsage
12
+ DWORD :th32ProcessID
13
+ DWORD :th32DefaultHeapID
14
+ DWORD :th32ModuleID
15
+ DWORD :cntThreads
16
+ DWORD :th32ParentProcessID
17
+ LONG :pcPriClassBase
18
+ DWORD :dwFlags
19
+ CHAR :szExeFile,[MAX_PATH]
20
+ end
21
+ end
22
+ end
23
+
24
+ def show_all_processes
25
+ proc_enrty32 = PROCESSENTRY32.new
26
+ proc_enrty32.dwSize = PROCESSENTRY32.__size__
27
+ snap_handle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS,0)
28
+
29
+ if Process32First(snap_handle,proc_enrty32.data)
30
+ show_process proc_enrty32
31
+ while true
32
+ break if !Process32Next( snap_handle,proc_enrty32.data )
33
+ show_process proc_enrty32
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ def show_process proc_enrty32
40
+ printf"%20s 0x%08X\n",proc_enrty32.szExeFile.to_cstr,proc_enrty32.th32ProcessID
41
+ end
42
+
43
+ printf"%20s %s\n","<Process Name>","<PID>"
44
+ printf"%20s %s\n","--------------","-----"
45
+ show_all_processes