[Win32utils-devel] Win32-ole start

Heesob Park phasis at gmail.com
Wed Jun 4 22:19:28 EDT 2008


Hi,

2008/6/5 Berger, Daniel <Daniel.Berger at qwest.com>:
> Hi all,
>
> Since I didn't get any takers on the win32-ole rewrite I thought I'd
> take a stab at starting it. Here's what I've got so far (along with some
> extra constants in Windows::COM only in CVS), but the call to
> CoCreateInstance() isn't working. I'm not sure if I'm dealing with the
> IDispatch interface properly:
>
> # win32/ole.rb
> require 'windows/com'
> require 'windows/com/automation'
> require 'windows/error'
> require 'windows/unicode'
> require 'socket'
>
> module Win32
>   class OLE
>      include Windows::Error
>      include Windows::COM
>      include Windows::COM::Automation
>      include Windows::Unicode
>
>      # Error raised if any of the OLE related methods fail.
>      class Error < StandardError; end
>
>      # The name of the OLE automation server specified in the
> constructor.
>      attr_reader :server
>
>      # The host the OLE automation object was created on.
>      attr_reader :host
>
>      # Did I declare these properly?
>      # http://tinyurl.com/3z8z4h
>      IID_IUnknown     = [0,0,0,192,0,0,0,0,0,0,70].pack('ISSCCCCCCCC')
>      IID_IDispatch    =
> [132096,0,0,192,0,0,0,0,0,0,70].pack('ISSCCCCCCCC')
>      IID_IEnumVARIANT =
> [132100,0,0,192,0,0,0,0,0,0,70].pack('ISSCCCCCCCC')
>
>      def initialize(server, host = Socket.gethostname)
>         @server = server
>         @host   = host
>
>         clsid    = 0.chr * 16
>         dispatch = 0.chr * 16 # I'm not sure what
>
>         hr = CLSIDFromProgID(multi_to_wide(server, CP_UTF8), clsid)
>
>         if FAILED(hr)
>            hr = CLSIDFromString(multi_to_wide(server, CP_UTF8), clsid)
>            if FAILED(hr)
>               raise Error, "unknown OLE server '#{server}'"
>            end
>         end
>
>         # Fails here
>         hr = CoCreateInstance(
>            clsid,
>            nil,
>            CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
>            IID_IDispatch,
>            dispatch
>         )
>
>         if FAILED(hr)
>            raise Error, "failed to create OLE object from '#{server}'"
>         end
>      end
>   end
> end
>
> if $0 == __FILE__
>   include Win32
>   excel = OLE.new('excel.application')
> End
>
> Any ideas?
>
The return value of CoCreateInstance()  -2147221008 is due to the lack
of CoInitialize() call.

Insert
hr = CoInitialize(nil)
before
hr = CoCreateInstance
seems to work for me.

Regards,

Park Heesob


More information about the win32utils-devel mailing list