internal static extern int IpcfEncryptFile(
     [In, MarshalAs(UnmanagedType.LPWStr)] string wszInputFilePath,
     [In] IntPtr pvLicenseInfo,
     [In, MarshalAs(UnmanagedType.U4)] uint dwType,
     [In, MarshalAs(UnmanagedType.U4)] uint dwFlags,
     [In, MarshalAs(UnmanagedType.LPStruct)] IpcPromptContext pContext,
     [In, MarshalAs(UnmanagedType.LPWStr)] string wszOutputFileDirectory,
     [Out] out IntPtr wszOutputFilePath);
        public static string IpcfEncryptFile(
            string inputFile,
            string templateId,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            string outputDirectory = null)
        {
            int    hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName    = null;

            IpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentForm);

            IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId);

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFile(
                    inputFile,
                    licenseInfoPtr,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID,
                    (uint)flags,
                    ipcContext,
                    outputDirectory,
                    out encryptedFileName);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFile;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(licenseInfoPtr);
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
            }

            return(outputFileName);
        }
        public static string IpcfEncryptFile(
            string inputFile,
            SafeInformationProtectionLicenseHandle licenseHandle,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            string outputDirectory = null)
        {
            int    hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName    = null;

            IpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentForm);

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFile(
                    inputFile,
                    licenseHandle.Value,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_LICENSE_HANDLE,
                    (uint)flags,
                    ipcContext,
                    outputDirectory,
                    out encryptedFileName);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
            }

            return(outputFileName);
        }
        public static string IpcfDecryptFile(
            string inputFile,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            string outputDirectory = null)
        {
            int    hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName    = null;

            IpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentForm);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFile(
                    inputFile,
                    (uint)flags,
                    ipcContext,
                    outputDirectory,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
            }

            return(decryptedFileName);
        }