Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/winmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ module WinMD
f.write_string(main_file_slice)
f.close
end
comptr_file_slice = ECR.render("./src/winmd/ecr/com_ptr.ecr").to_slice
::File.open(dir.join("src/" + WinMD.top_level_namespace.downcase + "/com_ptr.cr"), "w") do |f|
f.write_string(comptr_file_slice)
f.close
end
rescue e : Exception
puts "Failed to create main file"
puts e.message
Expand Down
43 changes: 43 additions & 0 deletions src/winmd/ecr/com_ptr.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module <%= WinMD.top_level_namespace %>
class ComPtr(T)
@ptr : Pointer(T)

def initialize(@ptr : T*)
end

def address
@ptr.address
end

def to_unsafe
@ptr
end

def release
if @ptr
@ptr.value.release(@ptr)
end
end

def finalize
release
end

def self.new(clsid : LibC::GUID, iid : LibC::GUID, clsctx : Win32cr::System::Com::CLSCTX = Win32cr::System::Com::CLSCTX::CLSCTX_ALL)
result = Win32cr::System::Com::C.CoCreateInstance(pointerof(clsid), nil, clsctx, pointerof(iid), out ptr)
unless result == Win32cr::Foundation::S_OK
raise RuntimeError.new "Failed to create COM instance. (error #{result})"
end
new(ptr.as(Pointer(T)))
end

macro method_missing(method)
\{% begin %}
\{% unless m = T.instance.methods.find(&.name. == {{method.name.stringify}}) %}
\{% raise "No method named #{{{method.name.stringify}}} in #{T.name.stringify}" %}
\{% end %}
@ptr.value.{{method.name}}(@ptr, {{method.args.splat}})
\{% end %}
end
end
end
2 changes: 2 additions & 0 deletions src/winmd/ecr/library_main.ecr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./<%= WinMD.top_level_namespace.downcase %>/com_ptr"

module <%= WinMD.top_level_namespace %>
VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}
end