if exists(select 1 from sysobjects where name = 'pr_key_plus')
   drop proc pr_key_plus
go

create procedure pr_key_plus (@code varchar(64) output,@codea varchar(64) output ,@rt int) 
WITH ENCRYPTION 
AS 
begin

	DECLARE @retval int
	DECLARE @appId  int 
	DECLARE @errorMessage nvarchar(max)
	
	
	--  查找加密锁 
	SET @appId = 11259375
	EXEC @retval = dbo.Nox2Find @appId
	--PRINT @retval
	
	IF @retval <> 0 BEGIN
		SET @errorMessage = 'Nox2 key is not found.'
		GOTO ERROR;
	END 
	
	-- 获取加密锁唯一ID
	DECLARE @keyID nvarchar(32)
	EXEC @retval = dbo.Nox2GetUid  @keyID OUTPUT
	--PRINT @retval 
	IF @retval <> 0 BEGIN
		SET @errorMessage = 'Failed to get Nox2 key id.'
		GOTO ERROR;
	END 
	--PRINT 'The key hardware id is ' + @keyID
	
	-- 使用密码打开加密锁 
	DECLARE @userPin nvarchar(255) 
	
	-- 根据实际情况设置密码参数
	SET @userPin = '74ffaa8e1b75bc6b76c10928ad84c2f5'
		
	EXEC	@retval = Nox2Open @userPin
	--PRINT	@retval
	IF @retval <> 0 BEGIN
		SET @errorMessage = 'Failed to open Nox2.'
		GOTO ERROR;
	END 
	
	set @code=@keyID
	set @codea=''
	set @rt=0
	select @code,@codea,@rt
	return 
	
	-- 使用完毕后关闭加密锁
	--EXEC	Nox2Close

ERROR:
	DECLARE @errorCode int
	
	set @code=''
	set @codea=@errorMessage
	set @rt=1
	select @code,@codea,@rt
	
end

