MaxIm.Document Close method with VB.net

Discussion in 'Scripting and Programming' started by John Willman, Mar 9, 2017.

  1. John Willman

    John Willman Standard User

    Joined:
    Nov 4, 2015
    Messages:
    6
    I cannot seem to get the document.close() method to work when executing from vb.net. I must be missing something obvious here. Code below I would expect to open "MyFile", and it does, but it never closes and doc.close() returns true.

    Dim file As String = "Myfile"
    Dim doc As MaxIm.Document = New MaxIm.Document
    doc.OpenFile(file)
    If doc.Close() = False Then
    Console.WriteLine("Unable to close.")
    End If

    COM Reference is set to MaxIm Objects 1.0.0.0 (Interop.MaxIm.dll)
    MaxIm DL Pro app version is 6.13
    VS 2010, running code as 32 bit, .net 4.0
     
  2. Doug

    Doug Staff Member

    Joined:
    Sep 25, 2014
    Messages:
    3,258
    COM is a little weird about document deletion. Try adding the line:

    Set doc = Nothing
     
  3. John Willman

    John Willman Standard User

    Joined:
    Nov 4, 2015
    Messages:
    6
    Doug, thanks, but that doesn't seem to solve the issue I'm having. If I set doc=nothing before the close I can't call the close (null reference) because the doc object has been set to nothing, not the open file I'm trying to close. If I set doc=nothing after the close executes the doc object is properly set to nothing, but the document remains open in MaxIM because the close command that executed on the doc{myfile} doesn't close the document. I can work around this for what I'm trying to do. The only reason I was trying to close the docs was I'm operating on a set of ~ 1K images and wanted to limit memory use in MaxIM.
     
  4. Doug

    Doug Staff Member

    Joined:
    Sep 25, 2014
    Messages:
    3,258
    Just did a quick test with VBScript, this worked as expected:

    file = "c:\test.fit"
    set doc = CreateObject ( "MaxIm.Document" )
    doc.OpenFile(file)
    doc.Close()
     
  5. John Willman

    John Willman Standard User

    Joined:
    Nov 4, 2015
    Messages:
    6
    Interesting, that too works for me if I run it as a VB script from within MaxIm, but if I do something similar from vb.net (only real change is "dim" must be used instead of "set") as below the doc stays open. Must be something up with the COM object. Not a huge deal for what I'm trying to do. I'll work around it.

    Dim file As String = "C:\test.fit"
    Dim Doc = CreateObject("MaxIm.Document")
    Doc.openfile(file)
    Doc.close()
     
  6. Doug

    Doug Staff Member

    Joined:
    Sep 25, 2014
    Messages:
    3,258
    COM works in mysterious ways. It is a Microsoft product. ;)
     
  7. Bob Denny

    Bob Denny Cyanogen Customer

    Joined:
    Oct 12, 2014
    Messages:
    489
    Location:
    DC-3 Dreams, SP, Mesa, Arizona +1 480 396 9700
    Are your test scripts exiting after the Document.Close? I have observed some deferrals on window closing in MaxIm. In .NET try calling Marshal.ReleaseComObject() to force the release instead of relying on .NETs garbage collection to release it. I believe the Document must not only be closed, but also released, before the window will close for sure.
     
  8. Doug

    Doug Staff Member

    Joined:
    Sep 25, 2014
    Messages:
    3,258
    Good idea.
     
  9. John Willman

    John Willman Standard User

    Joined:
    Nov 4, 2015
    Messages:
    6
    That had not occurred to me. It did solve my issue -- TYVM! A test with code below opens file1 and file2 and when done file1 is closed and file2 stays open, which is what I would expect.

    Dim file1 As String = "test1.fit"
    Dim file2 As String = "test2.fit"
    Dim Doc1 As New MaxIm.Document
    Dim doc2 As New MaxIm.Document
    Doc1.OpenFile(file1)
    doc2.OpenFile(file2)
    Doc1.Close()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc1)
     
  10. Bob Denny

    Bob Denny Cyanogen Customer

    Joined:
    Oct 12, 2014
    Messages:
    489
    Location:
    DC-3 Dreams, SP, Mesa, Arizona +1 480 396 9700
    Hooray. Personally I think COM is Microsoft's best "happy accident". Like any other organically evolved concept it has some quirks.
     
  11. Doug

    Doug Staff Member

    Joined:
    Sep 25, 2014
    Messages:
    3,258
    Agreed!
     

Share This Page