Sign in to follow this  
Followers 0
twbradio

FindMimeFromData - And yeah, I need to actually try to make it work

3 posts in this topic

Ok my friends ...

It has been a long time since I asked for your help, but I need it once more. I need to use the FindMimeFromData call from urlmon.dll. Unfortunately, this is for a work project, so some of the "free for personal use" options are not available to me (TrIDLib for instance). The core issue is trying to detect files that have been corrupted by a specific virus family. The standard detection method includes checking the mime type against the file extension. The URL for the .net documentation for this is at: http://msdn.microsoft.com/en-us/library/ms775107(VS.85).aspx. One person that sucessfully got this to work in C# can be found in the thread located here: http://forums.asp.net/t/1292209.aspx. Below is the test code I am working with (using 3.3.10 for the Null function) ... any help or comments would be greatly appreciated:

$DLL = DllOpen("urlmon.dll")
$file = FileOpenDialog ( "Test File", @ScriptDir, "All (*.*)" )
$filedata = FileRead ($file)
$filelen = StringLen($filedata)
If $filelen > 4096 Then
 $filedata = FileRead ($file, 4096)
 $filelen = 4096
EndIf

Dim $Null
Dim $MimeOut

$Null = Null


;First call by filename with dwMimeFlags set to 1 (URL as a filename)
$Output = DllCall ( $DLL, "int", "FindMimeFromDataW", "BC*", $Null, "wstr", $file, "ptr", $Null, "DWORD", 0, "wstr", $Null, "DWORD", 1, "wstr", $MimeOut, "DWORD", 0)
msgbox (0, "", $Output&@tab&$MimeOut)
;Second call using bytes pulled from the file into a "buffer" with a buffer size
$Output = DllCall ( $DLL, "int", "FindMimeFromDataW", "BC*", $Null, "wstr", $Null, "ptr", $filedata, "DWORD", $filelen, "wstr", $Null, "DWORD", 0, "wstr", $MimeOut, "DWORD", 0)
msgbox (0, "", $Output&@tab&$MimeOut)

Anyone have a TRS 80 Model III for sale?

Share this post


Link to post
Share on other sites



Maybe something like this:

#include <WinAPI.au3>

$sFile = "whatever.png"

$aCall = DllCall("urlmon.dll", "long", "FindMimeFromData", _
        "ptr", 0, _
        "wstr", "http:///" & $sFile, _
        "ptr", 0, _
        "DWORD", 0, _
        "ptr", 0, _
        "DWORD", 0, _
        "ptr*", 0, _
        "DWORD", 0)

If @error Then
    ; kind of not good. Do something here
Else
    $sMime = DllStructGetData(DllStructCreate("wchar[" & _WinAPI_StringLenW($aCall[7]) & "]", $aCall[7]), 1)
    MsgBox(4096, "", "File = " & $sFile & @CRLF & "MIME type = " & $sMime & @CRLF)
EndIf

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

That did it ... I did go ahead and change dwMimeFlags to a 1 to allow pwzUrl to be a file name and dropped the http://, but other than that it worked right out of the box. Thank you so much.

Final code I tested with:

#include <WinAPI.au3>

$sFile = FileOpenDialog ( "Test File", @ScriptDir, "All (*.*)" )

$aCall = DllCall("urlmon.dll", "long", "FindMimeFromData", _
        "ptr", 0, _
        "wstr", $sFile, _
        "ptr", 0, _
        "DWORD", 0, _
        "ptr", 0, _
        "DWORD", 1, _
        "ptr*", 0, _
        "DWORD", 0)

If @error Then
    ; kind of not good. Do something here
Else
    $sMime = DllStructGetData(DllStructCreate("wchar[" & _WinAPI_StringLenW($aCall[7]) & "]", $aCall[7]), 1)
    MsgBox(4096, "", "File = " & $sFile & @CRLF & "MIME type = " & $sMime & @CRLF)
EndIf

Anyone have a TRS 80 Model III for sale?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0