Casting Problem using FindMimeFromData function Netherlands
  • 12 years ago


    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?






    Imports System.Security.Permissions

    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

    Public Shared Function getMimeFromFile(ByVal file As String) As String

            Dim mimeout As IntPtr

            If Not System.IO.File.Exists(file) Then
                Throw New FileNotFoundException(file + " not found")
            End If

            Dim MaxContent As Integer = CInt(New FileInfo(file).Length)

            If MaxContent > 4096 Then
                MaxContent = 4096
            End If

            Dim fs As New FileStream(file, FileMode.Open)

            Dim buf(MaxContent) As Byte
            fs.Read(buf, 0, MaxContent)

            Dim result As Integer = FindMimeFromData(IntPtr.Zero, file, buf, MaxContent, Nothing, 0, mimeout, 0)

            If result <> 0 Then
                'Throw Marshal.GetHRForExceptionresult)
            End If

            Dim mime As String = Marshal.PtrToStringUni(mimeout)


            Return mime

        End Function





Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay