Windows Client Development--get MIME of files in Windows system

Source: Internet
Author: User
Tags pkcs12 pkcs7 rfc822

First of all, what is MIME, is not the file suffix name?

Of course not.

Sometimes we need to get the suffix of a file, which may be too small case for you, you may not think of writing a function, more file name string to find the last '. ', and then take the most one '. ' After the string, that is, we want to get the suffix name.

It looks perfect, but it's really flawed.

What if my file does not have a suffix name?
What if I have a PNG image that I force to change the suffix name to jpg?
This way you cannot get the correct suffix name based on the file name method.

So, it's the MIME!

What is MIME?

MIME (Multipurpose Internet Mail Extensions) is an Internet standard that describes the message content type.
MIME messages can contain text, images, audio, video, and other application-specific data.

The next task is on the Windows system, how to get the mime of this file based on a filename path?

At first, I felt that we should find a third-party library to implement this small function, such as libmagic.

But when you look back, you find that Windows provides an API for us. Here's a word: Windows is really the most complex operating system. Sometimes we ignore too many of the functions that Windows API provides to us.

Findmimefromdata
Role:
Determines the MIME type from the data provided.
Prototype:

HRESULT FindMimeFromData(             LPBC    pBC,             LPCWSTR pwzUrl,             LPVOID  pBuffer,             DWORD   cbSize,             LPCWSTR pwzMimeProposed,             DWORD   dwMimeFlags,             LPWSTR  *ppwzMimeOut,  _Reserved_ DWORD   dwReserved);

Parameters:

PBC
A pointer to the IBindCtx interface. Can is set to NULL.
Pwzurl
A pointer to a string value that contains the URL of the data. Can is set to NULL if Pbuffer contains the data to be sniffed.
pbuffer
A pointer to the buffer, the contains the data to is sniffed. Can is set to NULL if Pwzurl contains a valid URL.
cbsize
An unsigned long integer value that contains the size of the buffer.
Pwzmimeproposed
A pointer to a string of value that contains the proposed MIME type. This value is authoritative if type cannot are determined from the data. If The proposed type contains a semi-colon (;) it is removed. This parameter can is set to NULL.

Here's a look at how to apply, first read a file and then call the Findmimefromdata function:

#include <urlmon.h>#include <stdio.h>#include <Windows.h>#include <iostream>#pragma comment (lib, "Urlmon.lib")intMainintargcChar* argv[]) {Charbuff[ the]; LPWStr out; FILE *inch= fopen ("D:\\libsakura.lib","RB"); Fread (Buff,1, the,inch); Findmimefromdata (null, NULL, buff, the, NULL, Fmfd_default, & out,0); printf"%ls\n", out);return 0;}

The following is the MIME for the file type, for informational purposes only:

Application/envoy evyapplication/fractals Fifapplication/futuresplash Splapplication/hta htaapplication/internet-property-streamAcxapplication/mac-binhex40Hqxapplication/msword Docapplication/msword Dotapplication/octet-stream    *Application/octet-streamBinapplication/octet-streamClassapplication/octet-streamDmsapplication/octet-streamExeapplication/octet-streamLhaapplication/octet-streamLzhapplication/oda Odaapplication/olescript axsapplication/pdf Pdfapplication/pics-rulesPRFAPPLICATION/PKCS10 P10application/pkix-CRLCrlapplication/postscript aiapplication/postscript epsapplication/postscript psapplication/rtf rtfapplication/Set-payment-initiationsetpayapplication/Set-registration-initiationSetregapplication/vnd.Ms-excelXlaapplication/vnd.Ms-excelXlcapplication/vnd.Ms-excelXlmapplication/vnd.Ms-excelXlsapplication/vnd.Ms-excelXltapplication/vnd.Ms-excelXlwapplication/vnd.Ms-outlookMsgapplication/vnd.Ms-pkicertstoreSstapplication/vnd.Ms-pkiseccatCatapplication/vnd.Ms-pkistlStlapplication/vnd.Ms-powerpointPotapplication/vnd.Ms-powerpointPpsapplication/vnd.Ms-powerpointPptapplication/vnd.Ms-projectMppapplication/vnd.Ms-worksWcmapplication/vnd.Ms-worksWdbapplication/vnd.Ms-worksWksapplication/vnd.Ms-worksWpsapplication/winhlp hlpapplication/x-bcpiobcpioapplication/x-CDFcdfapplication/x-compresszapplication/x-compressedtgzapplication/x-cpiocpioapplication/x-cshcshapplication/x-directordcrapplication/x-directordirapplication/x-directordxrapplication/x-dvidviapplication/x-gtargtarapplication/x-gzipgzapplication/x-HDFhdfapplication/x-internet-signupinsapplication/x-internet-signupispapplication/x-iphoneiiiapplication/x-javascriptjsapplication/x-latexlatexapplication/x-msaccessmdbapplication/x-mscardfilecrdapplication/x-msclipclpapplication/x-msdownloaddllapplication/x-msmediaviewm13application/x-msmediaviewm14application/x-msmediaviewmvbapplication/x-msmetafilewmfapplication/x-msmoneymnyapplication/x-mspublisherpubapplication/x-msschedulescdapplication/x-msterminaltrmapplication/x-mswritewriapplication/x-NETCDFcdfapplication/x-NETCDFncapplication/x-perfmonpmaapplication/x-perfmonpmcapplication/x-perfmonpmlapplication/x-perfmonpmrapplication/x-perfmonpmwapplication/x-PKCS12p12application/x-PKCS12pfxapplication/x-PKCS7-certificatesp7bapplication/x-PKCS7-certificatesspcapplication/x-PKCS7-certreqrespp7rapplication/x-PKCS7-mimep7capplication/x-PKCS7-mimep7mapplication/x-PKCS7-signaturep7sapplication/x-shshapplication/x-sharsharapplication/x-shockwave-flashswfapplication/x-stuffitsitapplication/x-sv4cpiosv4cpioapplication/x-SV4CRCsv4crcapplication/x-tartarapplication/x-TCLtclapplication/x-textexapplication/x-texinfotexiapplication/x-texinfotexinfoapplication/x-troffroffapplication/x-trofftapplication/x-trofftrapplication/x-troff-manmanapplication/x-troff-memeapplication/x-troff-msmsapplication/x-ustarustarapplication/x-wais-sourcesrcapplication/x-x509-ca-certcerapplication/x-x509-ca-certcrtapplication/x-x509-ca-certDerapplication/ynd.Ms-pkipkoPkoapplication/zip zipaudio/basic auaudio/basic sndaudio/mid midaudio/mid rmiaudio/mpeg mp3audio/x-aiffaifaudio/x-aiffaifcaudio/x-aiffaiffaudio/x-mpegurlm3uaudio/x-PN-realaudioraaudio/x-PN-realaudioramaudio/x-wavWavimage/bmp Bmpimage/cis-codCodimage/gif gifimage/ief iefimage/jpeg jpeimage/jpeg jpegimage/jpeg jpgimage/pipeg jfifimage/svg+XMLSvgimage/tiff Tifimage/tiff tiffimage/x-CMU-rasterrasimage/x-CMXcmximage/x-iconicoimage/x-portable-anymappnmimage/x-portable-bitmappbmimage/x-portable-graymappgmimage/x-portable-pixmapppmimage/x-rgbrgbimage/x-xbitmapxbmimage/x-xpixmapxpmimage/x-xwindowdumpxwdmessage/rfc822 mhtmessage/rfc822 mhtmlmessage/rfc822 nwstext/css csstext/h323323text/html htmtext/html htmltext/html stmtext/iuls ulstext/plain bastext/plain ctext/plain htext/plain txttext/ RichText Rtxtext/scriptlet Scttext/tab-separated-valuesTsvtext/webviewhtml htttext/x-componenthtctext/x-setextetxtext/x-vcardVcfvideo/mpeg mp2video/mpeg mpavideo/mpeg mpevideo/mpeg mpegvideo/mpeg mpgvideo/mpeg mpv2video/quicktime movvideo/qu Icktime qtvideo/x-la-asflsfvideo/x-la-asflsxvideo/x-ms-asfasfvideo/x-ms-asfasrvideo/x-ms-asfasxvideo/x-msvideoavivideo/x-sgi-movieMoviex-world/x-VRMLFlrx-world/x-VRMLVrmlx-world/x-VRMLWrlx-world/x-VRMLWrzx-world/x-VRMLXafx-world/x-VRMLXof

Windows Client Development--get MIME of files in Windows system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.