Need help with interface pointer.

Started by Elias Montoya, January 12, 2012, 03:24:27 AM

Previous topic - Next topic

Elias Montoya


Im not very used to this new way to use OCX controls in PB10.

When i get a pointer to an interface, and try to use it, i get a compile time error. For example:

Local iDpx     As IDispatch
Local Selection   As IHTMLDocument2

iDpx = DHTMLEdit(Onum).dom
           
Selection = iDpx.get_selection  '<------ Compile time error


What i am aiming to is to use PasteHTML method on the DHTMLEdit control. Im sure im doing something wrong. :) Any hints?


Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

José Roca

You're dimming iDpx as IDispatch. Therefore, you only can call the 4 methods of the IDispatch interface and the 3 methods of the IUnknown interface with it. You have to dim iDpx as the inerface that implements the get_Selection method that you intend to call.

Elias Montoya


Thanks for the reply Jose. Your reply makes sense to me, and I also tried that before.
the reason i tried the code i posted above was because I was getting the same results,
and i wanted to try something else.

Here's the code i used first:

Local iDpx     As IHTMLDocument2
Local Selection   As IHTMLSelectionObject

iDpx = DHTMLEdit(Onum).dom  '<--- get the pointer to the dispatch interface 'IHTMLDocument2'
           
Selection = iDpx.get_selection  '<------ Compile time error


Also tried:

Call iDpx.get_selection to Selection

But no luck....  :-\


Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

José Roca

If you're using my headers for MSHTML, there is not a get_selection method, but a selection property. Therefore, use Selection = iDpx.selection

Elias Montoya

#4
Right. That works. But i am using this headers:

########################################################################################
' Library name: MSHTML
' Version: 4.0, Locale ID = 0
' Description: Microsoft HTML Object Library
' Path: C:\Windows\System32\mshtml.tlb
' Library GUID: {3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}
' Code generated by the TypeLib Browser 5.0.0 (c) 2011 by Jose Roca
' Date: 11 ene 2012   Time: 17:29:39
' Options used to generate the code:
' - Add prefix in parameter names
' ########################################################################################


And i see this method in the interface i added to my code:

########################################################################################
' Interface name = IHTMLDocument2
' IID = {332C4425-26CB-11D0-B483-00C04FD90119}
' Attributes = 4160 [&H00001040] [Dual] [Dispatchable]
' Inherited interface = IHTMLDocument
' ########################################################################################

#If Not %DEF(%IHTMLDocument2_INTERFACE_DEFINED)
    %IHTMLDocument2_INTERFACE_DEFINED = 1

Interface IHTMLDocument2 $IID_IHTMLDocument2

   Inherit IDispatch

  [REMOVED MANYE METHODS TO SHORTEN POST SIZE]

   ' =====================================================================================
   Method get_selection ( _                             ' VTable offset = 84
   ) As IHTMLSelectionObject                            ' [retval][out] **p IHTMLSelectionObject <dispinterface>
   ' =====================================================================================
   Method get_readyState ( _                            ' VTable offset = 88
   ) As wString                                         ' [retval][out] *p VT_BSTR
   ' =====================================================================================
   Method get_frames ( _                                ' VTable offset = 92
   ) As IHTMLFramesCollection2                          ' [retval][out] **p IHTMLFramesCollection2 <dispinterface>
   ' =====================================================================================
   Method get_embeds ( _                                ' VTable offset = 96
   ) As IHTMLElementCollection                          ' [retval][out] **p IHTMLElementCollection <dispinterface>
   ' =====================================================================================
   Method get_plugins ( _                               ' VTable offset = 100
   ) As IHTMLElementCollection                          ' [retval][out] **p IHTMLElementCollection <dispinterface>
   ' =====================================================================================
   Method put_alinkColor ( _                            ' VTable offset = 104
     ByVal Variant _                                    ' [in] p VT_VARIANT <Variant>
   )                                                    ' void

  [REMOVED MANY METHODS TO SHORTEN POST SIZE]

End Interface

#EndIf   ' /* __IHTMLDocument2_INTERFACE_DEFINED__ */


My guess is that the interface is being defined somewhere before in your headers... and the:

#If Not %DEF(%IHTMLDocument2_INTERFACE_DEFINED)

Is jumping over the ones i added. I got these from your typelib browser, so, most likely that is the problem.
I will take a look at your interface definitions and use those instead.

Thanks Jose. :)

Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

José Roca

You get these if the option "Use METHOD and PROPERTY/GET statements" is unchecked; otherwise, you get the already defined ones in my headers. My headers have interface definitions for all the Windows COM subsystem, so better leave the browser for third-party COM servers.

Elias Montoya


That explains it all. And now everything is working as it should. Thanks Jose. :)
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

Elias Montoya

#7
 Again interface pointers, Things like these are the ones that puzzles me.

I DO see the script property. I can't access it. Even if i add my own interface definition, using an extravagant name that i cannot make errors with, it would not allow me to access the Script property.

FUNCTION GetDesignerCode(OCXControl As Long) As String

Local WB    As IWebBrowser2
Local Doc   As IHTMLDocument
Local wStr  As wString
   
WB = OC_GetDispatch(OCXControl)

If IsObject(WB) Then   
   Doc = WB.Document
                                   
   wStr = Doc.Script   '<---- compile time error
                                 
   FUNCTION = wStr
End If   

End FUNCTION



I saw that the 'Script' Property is also 'inherited' in IHTMLDocument2 (although i didnt see the inherit part), but defining Doc, as IHTMLDocument2, would not help either. Now what am i doing wrong??  ???
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

José Roca

You can't change interface definitions at your will and expect to work. The script property of the IHtmlDocument interface returns an IDispatch pointer to the script engine, not a string with the text of the script as you seem to want.

Quote
IHTMLDocument::Script Property

Gets an interface pointer that provides access to the scripting object via a handle to the script engine.

Syntax

    HRESULT IHTMLDocument::get_Script(IDispatch **p);

Parameters

    p
        Address of a pointer to a variable of type IDispatch interface that receives a handle to the script engine.

Return Value

    Returns S_OK if successful, or an error value otherwise.

What to do with this pointer is something that I haven't yet explored.

Elias Montoya

 You are right.

I was being deceived by the error that i was receiving:

METHOD OR PROPERTY NAME EXPECTED.

I thought it was refering to the method or property name i was using with the interface, and not the variable name where i was trying to store the result. No wonder why it wasn't working.

I guess it would be clearer to say:

IDISATCH VARIABLE EXPECTED.

or

DATA TYPE MISMATCH.

:) :)

Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.