您的位置:首页 > 编程语言

RMS (Rights Management Services)基于SDK2.1的二次开发代码示例

2016-10-18 17:26 537 查看
最近研究了RMS的二次开发方法,主要实现针对指定用户设置文档权限的方法,收集的参考网站如下:

错误码详情:
https://msdn.microsoft.com/zh-cn/library/windows/desktop/hh535248(v=vs.85).aspx
官方FAQ
http://social.technet.microsoft.com/wiki/contents/articles/3440.ad-rms-frequently-asked-questions-faq.aspx https://technet.microsoft.com/en-us/dn467883
SDK下载
https://www.microsoft.com/en-us/download/details.aspx?id=38397
API参考
https://docs.microsoft.com/zh-cn/information-protection/develop/api-reference-2-1
代码示例如下:

#include "stdafx.h"

#include <stdio.h>

#include <windows.h>

#include <ipcbase.h>

#include <ipcprot.h>

#include <ipcfile.h>

int _tmain( int argc, _TCHAR* argv[] )

{

//初始化动态库
HRESULT hr = IpcInitialize();

if ( FAILED(hr) ) 


printf("Failed to IpcInitialize\n");

}

//获取RMS服务器上模板
PCIPC_TIL    pcTil = NULL;

hr = IpcGetTemplateList( NULL, 0, 0, NULL, NULL, &pcTil );

if (FAILED(hr)) 


printf("Failed to IpcGetTemplateList(%0x)\n",hr);

}

if ( !pcTil || pcTil->cTi <= 0 )

      {

        
printf("Has no temple at server.\n");

}

//根据模板创建license
IPC_LICENSE_HANDLE hLicense = NULL;

hr = IpcCreateLicenseFromTemplateID( pcTil->aTi[0].wszID,
    0,
        NULL,
        &hLicense );

if ( FAILED(hr) ) 


printf("Failed to IpcCreateLicenseFromTemplateID(%0x)\n",hr);

}

// Create User Rights structure for Joe with Read permissions
IPC_USER_RIGHTS joeReadRight = {0};

LPCWSTR rgwszReadRights[1] = {IPC_GENERIC_READ};

// Assign values to members of Rights structure
joeReadRight.User.dwType = IPC_USER_TYPE_EMAIL;

joeReadRight.User.wszID = L"test@qq.com";

joeReadRight.rgwszRights = rgwszReadRights;

joeReadRight.cRights = 1;

// Create User Rights List and assign the above rights
size_t uNoOfUserRights = 1;

PIPC_USER_RIGHTS_LIST pUserRightsList = NULL;

pUserRightsList = reinterpret_cast<PIPC_USER_RIGHTS_LIST>
(new BYTE[ sizeof(IPC_USER_RIGHTS_LIST) + uNoOfUserRights * sizeof(IPC_USER_RIGHTS)]);

if(pUserRightsList == NULL)
{

printf("pUserRightsList is NULL\n");

}

// Assign values to members of Rights List structure for Joe and Mary Kay
(*pUserRightsList).cbSize = sizeof(IPC_USER_RIGHTS_LIST);

(*pUserRightsList).cUserRights = uNoOfUserRights;

(*pUserRightsList).rgUserRights[0] = joeReadRight;

// Set the Rights List property on the license via its handle
// hLicense is a license handle created with IpcCreateLicenseFromScratch
hr = IpcSetLicenseProperty(hLicense, FALSE, IPC_LI_USER_RIGHTS_LIST, pUserRightsList);

if ( FAILED(hr) ) 


printf("Failed to IpcSetLicenseProperty(%0x)\n",hr);

}

//对指定文件加密,使用上面的授权信息
LPCWSTR wszInputFilePath = L"c:\\test.docx";

LPCWSTR outputName = NULL;

hr =  IpcfEncryptFile(wszInputFilePath,
 hLicense,
 1,
 IPC_SL_FLAG_DEFAULT,
 NULL,
 NULL,
 &outputName);

if ( FAILED(hr) ) 


printf("Failed to IpcfEncryptFile(%0x)\n",hr);

}

//记得释放内存
if( pcTil )
{

IpcFreeMemory((LPVOID)pcTil);

pcTil = NULL;

}

if( hLicense )
{

IpcCloseHandle((IPC_HANDLE )hLicense);

pcTil = NULL;

}

getchar();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RMS SDK 代码 c++
相关文章推荐