Casting Problem with FindMimeFromData Function

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 5
Reputation: deniseaddy is an unknown quantity at this point 
Solved Threads: 0
deniseaddy deniseaddy is offline Offline
Newbie Poster

Casting Problem with FindMimeFromData Function

 
0
  #1
Jul 20th, 2008
Hi,

I'm getting a casting error using the code below. The error on the web page (when you click the error icon) is:

Sys.WebForms.PageRequestManagerServerErrorExceptio n: Conversion from string "application.msword" to type "Long" is not valid.

It's to do with declaring the mimeout integer pointer to the "<MarshalAs(UnmanagedType.LPWStr)> ByRef ppwzMimeOut As String" value in the function call.

If I change mimeout declaration to: "Dim mimeout as string" and return that and comment out the other mimeout referenced code it returns "application/msword" correctly but obviously the rest of the function won't work because there is no pointer to the original referenced mimeout address.

Has anyone got any ideas of what I'm doing wrong?

Thanks,

Denise



  1.  
  2.  
  3.  
  4. Imports System.Security.Permissions
  5.  
  6. Public Declare Function FindMimeFromData Lib "urlmon.dll" (ByVal pBC As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal pwzUrl As String, <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I1, SizeParamIndex:=3)> ByVal pBuffer As Byte(), ByVal cbSize As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByRef ppwzMimeOut As String, ByVal dwReserved As Integer) As Integer
  7.  
  8.  
  9. Public Shared Function getMimeFromFile(ByVal file As String) As String
  10.  
  11. Dim mimeout As IntPtr
  12.  
  13. If Not System.IO.File.Exists(file) Then
  14. Throw New FileNotFoundException(file + " not found")
  15. End If
  16.  
  17. Dim MaxContent As Integer = CInt(New FileInfo(file).Length)
  18.  
  19. If MaxContent > 4096 Then
  20. MaxContent = 4096
  21. End If
  22.  
  23. Dim fs As New FileStream(file, FileMode.Open)
  24.  
  25. Dim buf(MaxContent) As Byte
  26. fs.Read(buf, 0, MaxContent)
  27. fs.Close()
  28.  
  29. Dim result As Integer = FindMimeFromData(IntPtr.Zero, file, buf, MaxContent, Nothing, 0, mimeout, 0)
  30.  
  31. If result <> 0 Then
  32. 'Throw Marshal.GetHRForExceptionresult)
  33. End If
  34.  
  35. Dim mime As String = Marshal.PtrToStringUni(mimeout)
  36.  
  37. Marshal.FreeCoTaskMem(mimeout)
  38.  
  39. Return mime
  40.  
  41. End Function
  42.  
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 5
Reputation: deniseaddy is an unknown quantity at this point 
Solved Threads: 0
deniseaddy deniseaddy is offline Offline
Newbie Poster

Re: Casting Problem with FindMimeFromData Function

 
0
  #2
Jul 22nd, 2008
Can nobody help me with this?

I've posted the same question in six forums and no-one seems to know what I've done wrong in my coding. I'm sure it's a simple case of using the wrong variable type either in the function or pointer declaration but for the life of me I don't know where!

Please advise.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 5
Reputation: deniseaddy is an unknown quantity at this point 
Solved Threads: 0
deniseaddy deniseaddy is offline Offline
Newbie Poster

Re: Casting Problem with FindMimeFromData Function

 
0
  #3
Jul 24th, 2008
I've fixed this by using "<MarshalAs(UnmanagedType.U4)> ByRef ppwzMimeOut As Integer" in the function decalre.. but I don't really understand why it works, probably because I don't really understand the way MarshalAs works either. But at least it works!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC