473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MSHTML: How to IDM_GETBLOCKFMTS in C#

Hi,

how can I get the available block formats from the mshtml with
IDM_GETBLOCKFMTS? I'm pretty confused how to call the IOleCommandTarget.Exec
correctly. Can someone please provide me a small code snippet?

TIA

Peter
Nov 16 '05 #1
4 3080
Hi Peter

Below is code (in VB.NET) that does the job. Hopefully, it will be straight
forward to convert. The function to call is GetBlockFormats(), which returns
a string array.

<code>
<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

bstrVal = Marshal.StringToBSTR(Value)

End Sub
Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
' Leave this blank. Call will be redirected to external DLL.
End Sub

End Class 'OLEVARIANT

Public Function GetBlockFormats() As String()

Dim oleVar As OLEVARIANT
Dim obj As Object
Dim fmts As String()

oleVar = GetCommand(MSHTMLCommandEnum.IDM_GETBLOCKFMTS)

'IE doesn't return a "well-formed" VARIANT here so
'Or in the VT_BSTR flag:
oleVar.vt = CType(oleVar.vt Or VarEnum.VT_BSTR, Short)

'This is a little wrapper to call
Marshal.GetObjectForNativeVariant()
obj = oleVar.ToNativeObject()

'Cleanup the Native Variant to avoid a memory leak
oleVar.Clear()

fmts = CType(obj, String())

' Return the block formats
Return fmts

End Sub

Private Function GetCommand(ByVal cmdID As MSHTMLCommandEnum) As
OLEVARIANT

Dim oOut As New OLEVARIANT
Dim oIn As OLEVARIANT

Dim cmdt As IOleCommandTarget

Try
cmdt = DirectCast(Document, IOleCommandTarget)

cmdt.Exec(CGID_MSHTML, cmdID, OLECMDEXECOPT_DONTPROMPTUSER, oIn,
oOut)

Catch ex As Exception
Throw New Exception("ExecCommand: " & ex.Message)

End Try

Return oOut

End Function
</code>

HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
Hi,

how can I get the available block formats from the mshtml with
IDM_GETBLOCKFMTS? I'm pretty confused how to call the IOleCommandTarget.Exec correctly. Can someone please provide me a small code snippet?

TIA

Peter

Nov 16 '05 #2
Hi Charles,

Thank you for posting. But for some reason I don't get anything back from
the "GetCommand". The oleVar is empty, but no errors or exceptions are
fired. Any suggestions?

Peter

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:uN*************@TK2MSFTNGP11.phx.gbl...
Hi Peter

Below is code (in VB.NET) that does the job. Hopefully, it will be straight forward to convert. The function to call is GetBlockFormats(), which returns a string array.

<code>
<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

bstrVal = Marshal.StringToBSTR(Value)

End Sub
Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
' Leave this blank. Call will be redirected to external DLL.
End Sub

End Class 'OLEVARIANT

Public Function GetBlockFormats() As String()

Dim oleVar As OLEVARIANT
Dim obj As Object
Dim fmts As String()

oleVar = GetCommand(MSHTMLCommandEnum.IDM_GETBLOCKFMTS)

'IE doesn't return a "well-formed" VARIANT here so
'Or in the VT_BSTR flag:
oleVar.vt = CType(oleVar.vt Or VarEnum.VT_BSTR, Short)

'This is a little wrapper to call
Marshal.GetObjectForNativeVariant()
obj = oleVar.ToNativeObject()

'Cleanup the Native Variant to avoid a memory leak
oleVar.Clear()

fmts = CType(obj, String())

' Return the block formats
Return fmts

End Sub

Private Function GetCommand(ByVal cmdID As MSHTMLCommandEnum) As
OLEVARIANT

Dim oOut As New OLEVARIANT
Dim oIn As OLEVARIANT

Dim cmdt As IOleCommandTarget

Try
cmdt = DirectCast(Document, IOleCommandTarget)

cmdt.Exec(CGID_MSHTML, cmdID, OLECMDEXECOPT_DONTPROMPTUSER, oIn, oOut)

Catch ex As Exception
Throw New Exception("ExecCommand: " & ex.Message)

End Try

Return oOut

End Function
</code>

HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
Hi,

how can I get the available block formats from the mshtml with
IDM_GETBLOCKFMTS? I'm pretty confused how to call the

IOleCommandTarget.Exec
correctly. Can someone please provide me a small code snippet?

TIA

Peter


Nov 16 '05 #3
Hi Peter

One possibility might be the definition of IOleCommandTarget. I have
modified mine because of inaccuracies in the predefined version. Have a look
at the one below:

<code>
' OLECMD
<StructLayout(LayoutKind.Sequential)> _
Public Structure OLECMD
Public cmdID As Int32
Public cmdf As Int32
End Structure

' OLECMDTEXT
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class OLECMDTEXT
Public cmdtextf As OLECMDTEXTF
Public cwActual As Int32
Private cwBuf As Int32 = 256 'Make sure this is the same as SizeConst
below
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public text As String
End Class

Public Enum OLECMDTEXTF
OLECMDTEXTF_NONE = 0
OLECMDTEXTF_NAME = 1
OLECMDTEXTF_STATUS = 2
End Enum

' IOleCommandTarget interface
<ComVisible(True), ComImport(),
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )> _
Public Interface IOleCommandTarget

<PreserveSig()> Function QueryStatus(ByRef pguidCmdGroup As Guid, ByVal
cCmds As Int32, _
<InAttribute(), Out(), MarshalAs(UnmanagedType.LPArray,
SizeParamIndex:=1)> ByVal prgCmds() As OLECMD, <InAttribute(), Out()> ByVal
pCmdText As OLECMDTEXT) As Integer

<PreserveSig()> Function Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdId
As Int32, _
ByVal nCmdExecOpt As Int32, ByVal pvaIn As OLEVARIANT, ByVal pvaOut
As OLEVARIANT) As Integer

End Interface
</code>
HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:eO*************@TK2MSFTNGP11.phx.gbl...
Hi Charles,

Thank you for posting. But for some reason I don't get anything back from
the "GetCommand". The oleVar is empty, but no errors or exceptions are
fired. Any suggestions?

Peter

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:uN*************@TK2MSFTNGP11.phx.gbl...
Hi Peter

Below is code (in VB.NET) that does the job. Hopefully, it will be

straight
forward to convert. The function to call is GetBlockFormats(), which

returns
a string array.

<code>
<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

bstrVal = Marshal.StringToBSTR(Value)

End Sub
Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
' Leave this blank. Call will be redirected to external DLL.
End Sub

End Class 'OLEVARIANT

Public Function GetBlockFormats() As String()

Dim oleVar As OLEVARIANT
Dim obj As Object
Dim fmts As String()

oleVar = GetCommand(MSHTMLCommandEnum.IDM_GETBLOCKFMTS)

'IE doesn't return a "well-formed" VARIANT here so
'Or in the VT_BSTR flag:
oleVar.vt = CType(oleVar.vt Or VarEnum.VT_BSTR, Short)

'This is a little wrapper to call
Marshal.GetObjectForNativeVariant()
obj = oleVar.ToNativeObject()

'Cleanup the Native Variant to avoid a memory leak
oleVar.Clear()

fmts = CType(obj, String())

' Return the block formats
Return fmts

End Sub

Private Function GetCommand(ByVal cmdID As MSHTMLCommandEnum) As
OLEVARIANT

Dim oOut As New OLEVARIANT
Dim oIn As OLEVARIANT

Dim cmdt As IOleCommandTarget

Try
cmdt = DirectCast(Document, IOleCommandTarget)

cmdt.Exec(CGID_MSHTML, cmdID, OLECMDEXECOPT_DONTPROMPTUSER,

oIn,
oOut)

Catch ex As Exception
Throw New Exception("ExecCommand: " & ex.Message)

End Try

Return oOut

End Function
</code>

HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
Hi,

how can I get the available block formats from the mshtml with
IDM_GETBLOCKFMTS? I'm pretty confused how to call the

IOleCommandTarget.Exec
correctly. Can someone please provide me a small code snippet?

TIA

Peter



Nov 16 '05 #4
Hi Charles,

It's working now. Your thoughts were right. Thank you very much, you helped
me a lot.

Peter

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:eg**************@TK2MSFTNGP10.phx.gbl...
Hi Peter

One possibility might be the definition of IOleCommandTarget. I have
modified mine because of inaccuracies in the predefined version. Have a look at the one below:

<code>
' OLECMD
<StructLayout(LayoutKind.Sequential)> _
Public Structure OLECMD
Public cmdID As Int32
Public cmdf As Int32
End Structure

' OLECMDTEXT
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class OLECMDTEXT
Public cmdtextf As OLECMDTEXTF
Public cwActual As Int32
Private cwBuf As Int32 = 256 'Make sure this is the same as SizeConst
below
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public text As String
End Class

Public Enum OLECMDTEXTF
OLECMDTEXTF_NONE = 0
OLECMDTEXTF_NAME = 1
OLECMDTEXTF_STATUS = 2
End Enum

' IOleCommandTarget interface
<ComVisible(True), ComImport(),
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )> _
Public Interface IOleCommandTarget

<PreserveSig()> Function QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As Int32, _
<InAttribute(), Out(), MarshalAs(UnmanagedType.LPArray,
SizeParamIndex:=1)> ByVal prgCmds() As OLECMD, <InAttribute(), Out()> ByVal pCmdText As OLECMDTEXT) As Integer

<PreserveSig()> Function Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdId As Int32, _
ByVal nCmdExecOpt As Int32, ByVal pvaIn As OLEVARIANT, ByVal pvaOut
As OLEVARIANT) As Integer

End Interface
</code>
HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:eO*************@TK2MSFTNGP11.phx.gbl...
Hi Charles,

Thank you for posting. But for some reason I don't get anything back from the "GetCommand". The oleVar is empty, but no errors or exceptions are
fired. Any suggestions?

Peter

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:uN*************@TK2MSFTNGP11.phx.gbl...
Hi Peter

Below is code (in VB.NET) that does the job. Hopefully, it will be

straight
forward to convert. The function to call is GetBlockFormats(), which

returns
a string array.

<code>
<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

bstrVal = Marshal.StringToBSTR(Value)

End Sub
Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
' Leave this blank. Call will be redirected to external DLL.
End Sub

End Class 'OLEVARIANT

Public Function GetBlockFormats() As String()

Dim oleVar As OLEVARIANT
Dim obj As Object
Dim fmts As String()

oleVar = GetCommand(MSHTMLCommandEnum.IDM_GETBLOCKFMTS)

'IE doesn't return a "well-formed" VARIANT here so
'Or in the VT_BSTR flag:
oleVar.vt = CType(oleVar.vt Or VarEnum.VT_BSTR, Short)

'This is a little wrapper to call
Marshal.GetObjectForNativeVariant()
obj = oleVar.ToNativeObject()

'Cleanup the Native Variant to avoid a memory leak
oleVar.Clear()

fmts = CType(obj, String())

' Return the block formats
Return fmts

End Sub

Private Function GetCommand(ByVal cmdID As MSHTMLCommandEnum) As
OLEVARIANT

Dim oOut As New OLEVARIANT
Dim oIn As OLEVARIANT

Dim cmdt As IOleCommandTarget

Try
cmdt = DirectCast(Document, IOleCommandTarget)

cmdt.Exec(CGID_MSHTML, cmdID,
OLECMDEXECOPT_DONTPROMPTUSER, oIn,
oOut)

Catch ex As Exception
Throw New Exception("ExecCommand: " & ex.Message)

End Try

Return oOut

End Function
</code>

HTH

Charles
"Peter Zentner" <peter@_REM_zentner-online.de> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> how can I get the available block formats from the mshtml with
> IDM_GETBLOCKFMTS? I'm pretty confused how to call the
IOleCommandTarget.Exec
> correctly. Can someone please provide me a small code snippet?
>
> TIA
>
> Peter
>
>



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dean Hallman | last post by:
I need to ensure client machine has Microsoft.mshtml installed in the GAC. And if not, deploy it. My app is a Browser Helper Object and depends on mshtml. Initially, I thought I could take care...
1
by: Peter Zentner | last post by:
I have a weird problem... A while ago I wrote some code in VB.NET which gets the block formats from mshtml like shown below. I am now converting this into C# and I have the problem that the...
4
by: David Pendrey | last post by:
Hello all, I am using the WebBrowser control to browse a webpage containing frames and am having difficulties accessing the 'frames' property of a document object. Bellow is my code and the error...
10
by: Hans Merkl | last post by:
Hi, I have written an pap with .NET 2.0 and c# that uses MSHTML. It works fine on my development machine but on machines I deploy it to I get the following exception when ever I call MSHTML. ...
4
by: Lars-Erik Aabech | last post by:
Hi! I've been walking in extacy since reading the article about test automation with IE in the latest MSDN mag. (http://msdn.microsoft.com/msdnmag/issues/05/10/TestRun/default.aspx) After a...
5
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
0
by: Atara | last post by:
Our application was build with VS 2003. I have tried to run it on a computer with .Net 2.0 (but without .Net 1.1 , as it should be used) and I got the following error - ...
11
by: Lucky | last post by:
hi guys, i need to parse html data that i've got from "Inet" object in vb6. now i want to prase the html data. here i got 2 options. one is MSXML and other is MSHTML. i tried both of them but i...
5
by: Jason | last post by:
Hi, I'm developing an HTML Editor Control using VB.Net 2003 for an application that used to use the DHTML Editor Control that is no longer supported. Well, it's been fun but I've hit a wall with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.