windows获取MIME类型的例子

[cpp]
#include <stdio.h>
#include <Windows.h>

char *get_mime_type(const char *sExtension)
{
HKEY hItem;
if(!sExtension) return NULL;
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, sExtension, 0, KEY_READ, &hItem) == ERROR_SUCCESS)
{
char *sPath = (char*)malloc(MAX_PATH);
DWORD dwSize = MAX_PATH;
DWORD dwType = REG_SZ;
if (RegQueryValueEx(hItem, "Content Type", NULL, &dwType, (LPBYTE)sPath, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hItem);
return sPath;
}
RegCloseKey(hItem);
}
return NULL;
}

int main()
{
char *mime = get_mime_type(".jpg");
printf("%sn", mime);

if(mime) free(mime);
return 0;
}
[/cpp]
通过查询注册表来查询 MIME。