#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Problem using FindMimeFromData function


    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

    Code:
    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)
            fs.Close()
    
            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)
    
            Marshal.FreeCoTaskMem(mimeout)
    
            Return mime
    
        End Function
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0
    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.
  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0
    I've finally fixed this by using "<MarshalAs(UnmanagedType.U4)> ByRef ppwzMimeOut As Integer" in the function declare.. 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!
  6. #4
  7. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2922
    Originally Posted by deniseaddy
    I've finally fixed this by using "<MarshalAs(UnmanagedType.U4)> ByRef ppwzMimeOut As Integer" in the function declare.. 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!
    I don't know either, anyway thanks for sharing the solution here.
    much appreciated!

Similar Threads

  1. Function problem
    By jon123 in forum ASP Development
    Replies: 1
    Last Post: April 20th, 2007, 10:16 AM
  2. Calling VBScript function via onclick event
    By bluh_11 in forum ASP Development
    Replies: 5
    Last Post: August 16th, 2006, 06:18 PM
  3. Login procedure with .NET 2.0 & SQL Server 2000
    By mickyjtwin in forum .NET Development
    Replies: 2
    Last Post: December 29th, 2005, 07:31 PM
  4. simple function problem
    By magyartoth in forum ASP Development
    Replies: 7
    Last Post: August 25th, 2005, 03:04 AM
  5. Replies: 0
    Last Post: February 6th, 2004, 12:04 PM

IMN logo majestic logo threadwatch logo seochat tools logo