Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > Get Content Mime Type

Thread Tools Display Modes
Old 02-16-2009, 03:27 PM
alexmiranda alexmiranda is offline
Join Date: Feb 2009
Posts: 1
Default Get Content Mime Type

I'm trying to get a content mime type from a file using FindMimeFromData Lib "URLMON" API under VB6. I couldn't find any documentation about this function. I'm trying to execute the following code in VB6 and cannot understand what's going wrong (FindMimeFromData returns 0 and I'd like to get the value the pointer pwzMimeOut as VB String)

Thank you in advance

Private Declare Function FindMimeFromData Lib "URLMON" (ByVal pbc As Long, ByVal pwzUrl As String, pBuffer As Any, _
    ByVal cbSize As Long, ByVal pwzMimeProposed As String, ByVal dwMimeFlags As Long, ByVal pwzMimeOut As String, _
    ByVal dwReserved As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long

Public Function GetMimeType(ByVal psFilePath As String, Optional ByVal psSugestedMimeType As String = vbNullString) As String
    Dim iFileNum As Integer, lFileLen As Long, buf() As Byte, buffAddr As Long, lBufferLen As Long, sMimeOut As String * 256, ret As Long
    Const MAX_BUFFER_LENGTH As Long = 4096
    If Not FileExists(psFilePath) Then
        Call Err.Raise(96001, GLOBAL_ERR_SOURCE, psFilePath & " not exists")
    End If
    lFileLen = FileLen(psFilePath)
    lBufferLen = IIf(lFileLen < MAX_BUFFER_LENGTH, lFileLen, MAX_BUFFER_LENGTH)
    buffAddr = StrPtr(sMimeOut)
    iFileNum = FreeFile()
    Open psFilePath For Binary Access Read As #iFileNum
    buf = InputB(lBufferLen, #iFileNum)
    Close #iFileNum
    ret = FindMimeFromData(0&, psFilePath, VarPtr(buf(0)), lBufferLen, vbNullString, 0&, buffAddr, 0&)
    If ret = 0 And buffAddr > 0 Then
        sMimeOut = PtrToString(buffAddr)
        sMimeOut = psSugestedMimeType
    End If
    GetMimeType = sMimeOut
End Function

' Converte um ponteiro de LPWSTR para uma String
Private Function PtrToString(ByVal lpwString As Long) As String
    Dim Buffer() As Byte, nLen As Long
    If lpwString Then
        nLen = lstrlenW(lpwString) * 2
        If nLen Then
            ReDim Buffer(0 To (nLen - 1)) As Byte
            Call CopyMemory(Buffer(0), ByVal lpwString, nLen)
            PtrToString = Buffer
        End If
    End If
End Function
Reply With Quote
Old 03-02-2009, 05:59 AM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821

You can't find any documentation? Did you search google?
I typed FindMimeFromData into google and got...

A) The documentation for the function

B) Example usage for VB, .NET and C
John, jlsd.co.uk
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..