Win32ole confusion

OK, I'm trying to wade my way through Rationals Rose RealTime using
their extensibility interface, just to get some information out of
*various* models.
So I need to load one model, do some work, open another model do some
work etc.

The following code works without a problem:
require 'win32ole'
modelname="some_model"
app=WIN32OLE.new('RoseRT.Application')
model=app.OpenModel(modelname)
app.visible
and at the end of the script the RoseRT.Application process is exited.

If I put the same code in an initialize method for a class

class Finder
  def initialize modelname
    @app=WIN32OLE.new('RoseRT.Application')
    @model=@app.OpenModel(modelname)
    @app.visible
  end
end

I get the following error

./rt_logical.rb:168:in `method_missing': OpenModel (WIN32OLERuntimeError)
     OLE error code:0 in <Unknown>
       <No Description>
     HRESULT error code:0x80020005
       Type mismatch. from ./rt_logical.rb:168:in `initialize'
  from structure.rb:69:in `new'

If I take the code and put it in a start method in the class

class Finder2
  def initialize modelname
    @modelname=modelname
    @model=nil
    @app=nil
  end
  
  def start
    @app=WIN32OLE.new('RoseRT.Application')
    @model=@app.OpenModel(@modelname)
    @app.visible
  end

  def search objectname
    raise RuntimeError,"Finder not started" unless @model
    #some search thingy
  end
end

and then execute the following:

ff=Finder.new('some_model')
begin
  ff.search 'capsule'
rescue
  ff.start
  retry
end

I got the same error as before (once) and then it worked (although at
the end of the script the RoseRT process is not exited).
Not to forget to mention that the class Finder is in a module.
So what am I missing in the whole create/initialize/destroy cycle that
breaks my script? Do I have some racing condition I need to worry about?
Cheers,
V.-

···

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.

Well, I managed to confuse myself even more. I moved the code outside the application and it works. So I probably screwed up.

module Test

  class FinderException<RuntimeError
    def initialize modelname
      @modelname=modelname
    end
  end
  class Finder
    attr_reader :modelname, :model
    #Initialize will throw an exception if the model cannot be opened.
    def initialize modelname
      @modelname=modelname
      @app=WIN32OLE.new('RoseRT.Application')
      @model=@app.OpenModel(@modelname)
    end
    
    def start
      @app=WIN32OLE.new('RoseRT.Application')
      @model=@app.OpenModel(@modelname)
    end
    
    def stop
      @app.exit
      @model=nil
      @app=nil
    end
    
    def show
      raise FinderException.new(@modelname),"This Finder instance is invalid" unless @model
      @app.visible=true
    end
    
    def hide
      raise FinderException.new(@modelname),"This Finder instance is invalid" unless @model
      @app.visible=false
    end
  end
end

ff=Test::Finder.new('a_model')
ff.show
ff.stop
ff.hide
Damphyr wrote:

···

OK, I'm trying to wade my way through Rationals Rose RealTime using
their extensibility interface, just to get some information out of
*various* models.
So I need to load one model, do some work, open another model do some
work etc.

The following code works without a problem:
require 'win32ole'
modelname="some_model"
app=WIN32OLE.new('RoseRT.Application')
model=app.OpenModel(modelname)
app.visible
and at the end of the script the RoseRT.Application process is exited.

If I put the same code in an initialize method for a class

class Finder
    def initialize modelname
        @app=WIN32OLE.new('RoseRT.Application')
        @model=@app.OpenModel(modelname)
        @app.visible
    end
end

I get the following error

./rt_logical.rb:168:in `method_missing': OpenModel (WIN32OLERuntimeError)
    OLE error code:0 in <Unknown>
      <No Description>
    HRESULT error code:0x80020005
      Type mismatch. from ./rt_logical.rb:168:in `initialize'
    from structure.rb:69:in `new'

If I take the code and put it in a start method in the class

class Finder2
    def initialize modelname
        @modelname=modelname
        @model=nil
        @app=nil
    end
        def start
        @app=WIN32OLE.new('RoseRT.Application')
        @model=@app.OpenModel(@modelname)
        @app.visible
    end

    def search objectname
        raise RuntimeError,"Finder not started" unless @model
        #some search thingy
    end
end

and then execute the following:

ff=Finder.new('some_model')
begin
    ff.search 'capsule'
rescue
    ff.start
    retry
end

I got the same error as before (once) and then it worked (although at
the end of the script the RoseRT process is not exited).
Not to forget to mention that the class Finder is in a module.
So what am I missing in the whole create/initialize/destroy cycle that
breaks my script? Do I have some racing condition I need to worry about?
Cheers,
V.-

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.

Probably. OLE is an asynchronous protocol. So WIN32OLE.new() can return an object before it is actually initialized and ready to use.

···

At 07:52 AM 9/9/2005, Damphyr wrote:

So what am I missing in the whole create/initialize/destroy cycle that
breaks my script? Do I have some racing condition I need to worry about?

_____________________
  Bret Pettichord
  www.pettichord.com