943,718 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1353
  • ASP.NET RSS
Jul 20th, 2008
0

Casting Problem with FindMimeFromData Function

Expand Post »
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



ASP.NET Syntax (Toggle Plain Text)
  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.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deniseaddy is offline Offline
5 posts
since Jul 2008
Jul 22nd, 2008
0

Re: Casting Problem with FindMimeFromData Function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deniseaddy is offline Offline
5 posts
since Jul 2008
Jul 24th, 2008
0

Re: Casting Problem with FindMimeFromData Function

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deniseaddy is offline Offline
5 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Gridview footer not showing
Next Thread in ASP.NET Forum Timeline: calculate allowance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC