Get url for pdf file from AxSHDocVw.AxWebBrowser Part2

  • Thread starter Thread starter LN Mike
  • Start date Start date
L

LN Mike

This new thread is for Jie Wang.

I get the error message, "Cast from type 'IWebBrowser2' to type 'Integer' is
not valid."

URLDownloadToFile(WebDisplay.GetOcx(), mstrUrl, "C:\IMAGE\test.pdf", 0, 0)

WebDisplay is my AxWebBrowser.
mstrUrl is the "https:// [blablabla] /doc1/ [lots of numbers]' url.
 
LN said:
This new thread is for Jie Wang.

May I answer despite? :)
I get the error message, "Cast from type 'IWebBrowser2' to type 'Integer' is
not valid."

URLDownloadToFile(WebDisplay.GetOcx(), mstrUrl, "C:\IMAGE\test.pdf", 0, 0)

Is it this function?
http://msdn.microsoft.com/en-us/library/ms775123(VS.85).aspx

It would require to pass <see docs> as the first arg. If you use the
function, the arg should be declared as Intptr, but is there a reason
why you don't use a managed function?

Webclient.Downloadfile:
http://msdn.microsoft.com/en-us/library/ez801hhe.aspx


I don't know what you want to do with the webbrowser control then.


Armin
 
Hi Armin,

Thank you for helping out. :)

This thread is "Part 2" of a long running thread back in April. Due to some
sync issue Mike had to open this new one so we can continue work on it.

So this is the background story. But you're always welcome to share ideas!

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I get the error message, "Cast from type 'IWebBrowser2' to type
'Integer' is not valid."

I can't see why IWebBrowser2 is being cast into Integer... there was no
Integer in the API declare:

<DllImport("urlmon.dll", CharSet:=CharSet.Auto, preservesig:=False)> _
Private Shared Sub URLDownloadToFile( _
<MarshalAs(UnmanagedType.IUnknown)> ByVal pCaller As
Object, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Integer, _
ByVal lpfnCB As IntPtr)
End Sub

Is this the same to the declare you're using?

Besides, I also provided another way of saving the PDF file by automating
the Save a Copy dialog box (see that previous post). Is that working?

Thanks,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
The first parameter's type is integer, see intellisense. If URLDownloadToFile
can not find AxWebBrowswer.GetOcx(), then it returns a null. Therefore error
on first parameter.

I think the way the pdf is supplied to the Adobe Reader, causes URLDownload
to not see it. I'm sure this is the security block Adobe tech told me about.
Your example works because your reading the pdf from a local non-secure web
server. Try uploading the pdf file to a off site server, use https and then
run your code. I think your URLDownloadToFile will not work just like mine.

I don't like the idea of automating the "Save a Copy" because several
instances of this app are running on the same pc. If that is the only option
I'll do it, but right now I'm waiting on the web masters to decided if they
can provide the temp file that has the pdf url for all sites. It would be
nice to get URLDownloadToFile to work, but I think we found a deadend due to
a security block.

Below is from MSDN library
HRESULT URLDownloadToFile(
LPUNKNOWN pCaller,
LPCTSTR szURL,
LPCTSTR szFileName,
DWORD dwReserved,
LPBINDSTATUSCALLBACK lpfnCB
);


pCaller
A pointer to the controlling IUnknown interface of the calling Microsoft
ActiveX component, if the caller is an ActiveX component. If the calling
application is not an ActiveX component, this value can be set to NULL.
Otherwise, the caller is a Component Object Model (COM) object that is
contained in another component, such as an ActiveX control in the context of
an HTML page. This parameter represents the outermost IUnknown of the calling
component. The function attempts the download in the context of the ActiveX
client framework, and allows the caller container to receive callbacks on the
progress of the download.


"Jie Wang [MSFT]" said:
I get the error message, "Cast from type 'IWebBrowser2' to type
'Integer' is not valid."

I can't see why IWebBrowser2 is being cast into Integer... there was no
Integer in the API declare:

<DllImport("urlmon.dll", CharSet:=CharSet.Auto, preservesig:=False)> _
Private Shared Sub URLDownloadToFile( _
<MarshalAs(UnmanagedType.IUnknown)> ByVal pCaller As
Object, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Integer, _
ByVal lpfnCB As IntPtr)
End Sub

Is this the same to the declare you're using?

Besides, I also provided another way of saving the PDF file by automating
the Save a Copy dialog box (see that previous post). Is that working?

Thanks,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
No I see the problem with the URLDownloadToFile...

Well, translate the definition into VB.NET, we don't use the Integer type
for the first parameter LPUNKNOWN pCaller.

Please note the following definition:

DllImport("urlmon.dll", CharSet:=CharSet.Auto, preservesig:=False)> _
Private Shared Sub URLDownloadToFile( _
<MarshalAs(UnmanagedType.IUnknown)> ByVal pCaller As Object, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Integer, _
ByVal lpfnCB As IntPtr)
End Sub

pCaller
A pointer to the controlling IUnknown interface of the calling Microsoft
ActiveX component....

So the function actually wants a pointer to the IUnknown interface. That's
good.

However, the AxWebBrowswer.GetOcx() returns an Object. Now what?

The solution is to declare the pCaller as Object, and attach a MarshalAs
attribute to this parameter, so CLR will cast it to IUnknown for us at
runtime.

Please use the declaration above instead and try again. Let me know how it
works.

Thanks,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I copied your code, rebuild solution, then ran app. On the code line,
URLDownloadToFile(WebDisplay.GetOcx(), mstrUrl, "C:\IMAGE\test.pdf", 0, 0)
caused the same error as before, "Cast from type 'IWebBrowser2' to type
'Integer' is not valid."
 
I copied your code, rebuild solution, then ran app. On the code line,
URLDownloadToFile(WebDisplay.GetOcx(), mstrUrl, "C:\IMAGE\test.pdf", 0, 0)
caused the same error as before, "Cast from type 'IWebBrowser2' to type
'Integer' is not valid."
 
Okay, try this form (uses IntPtr to IUnknown instead of MarshalAs):

<DllImport("urlmon.dll", CharSet:=CharSet.Auto, preservesig:=False)> _
Private Shared Sub URLDownloadToFile( _
ByVal pCaller As IntPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Integer, _
ByVal lpfnCB As IntPtr)
End Sub

And call the function like this:

URLDownloadToFile(Marshal.GetIUnknownForObject(AxWebBrowser1.GetOcx()),
"http://localhost:39211/FeedPDFStream.aspx", "D:\test.pdf", 0, IntPtr.Zero)

Or

URLDownloadToFile(IntPtr.Zero, "http://localhost:39211/FeedPDFStream.aspx",
"D:\test.pdf", 0, IntPtr.Zero)

Hope this time it works.

Regards,

Jie
 
Hi,

Any updates? :)

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top