Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active June 30, 2018 12:09
Show Gist options
  • Select an option

  • Save mntone/f9c174b6bef93e793c11d56ef07b5706 to your computer and use it in GitHub Desktop.

Select an option

Save mntone/f9c174b6bef93e793c11d56ef07b5706 to your computer and use it in GitHub Desktop.
MSCMS (WCS) 関数群
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace Mntone.Sample
{
internal enum DeviceClassFlags : uint
{
/// <summary>
/// 'mntr'
/// </summary>
Monitor = 0x6d6e7472,
/// <summary>
/// 'prtr'
/// </summary>
Printer = 0x70727472,
/// <summary>
/// 'scnr'
/// </summary>
Scanner = 0x73636e72
}
internal enum WCSProfileManagementScope : uint
{
SystemWide,
CurrentUser,
}
internal enum ColorProfileType : uint
{
ICC,
DMP,
CAMP,
GMMP,
}
[Flags]
internal enum ColorProfileSubType : uint
{
// intent
Perceptual = 0,
Relative = 1,
Saturation = 2,
Absolute = 3,
// working space
None,
RGBWorkingSpace,
CustomWorkingSpace,
};
internal static class NativeMethods
{
private const string MSCMS_DLL = "mscms.dll";
public static class Mscms
{
[DllImport(MSCMS_DLL, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WcsGetUsePerUserProfiles(
[In] string deviceName,
[In] DeviceClassFlags deviceClass,
[Out] out bool usePerUserProfiles);
internal static bool WcsGetUsePerUserProfiles(string deviceName, DeviceClassFlags deviceClass)
{
if (!WcsGetUsePerUserProfiles(deviceName, deviceClass, out var usePerUserProfiles))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return usePerUserProfiles;
}
[DllImport(MSCMS_DLL, SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WcsGetDefaultColorProfileSize(
[In] WCSProfileManagementScope profileManagementScope,
[In, Optional] string pDeviceName,
[In] ColorProfileType cptColorProfileType,
[In] ColorProfileSubType cpstColorProfileSubType,
[In] uint dwProfileID,
[Out] out uint cbProfileName);
[DllImport(MSCMS_DLL, SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WcsGetDefaultColorProfile(
[In] WCSProfileManagementScope profileManagementScope,
[In, Optional] string pDeviceName,
[In] ColorProfileType cptColorProfileType,
[In] ColorProfileSubType cpstColorProfileSubType,
[In] uint dwProfileID,
[In] uint cbProfileName,
[Out] StringBuilder profileName);
internal static string WcsGetDefaultColorProfile(WCSProfileManagementScope profileManagementScope, string deviceName, ColorProfileType colorProfileType, ColorProfileSubType colorProfileSubType, uint dwProfileID)
{
if (!WcsGetDefaultColorProfileSize(profileManagementScope, deviceName, colorProfileType, colorProfileSubType, dwProfileID, out var profileName))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
var builder = new StringBuilder((int)(profileName / 2));
if (!WcsGetDefaultColorProfile(profileManagementScope, deviceName, colorProfileType, colorProfileSubType, dwProfileID, profileName, builder))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return builder.ToString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment