Registry Reading and Writing UDF's

,

These are little more than wrapper functions for xp_regread and xp_regwrite, but lets them be callable from views, if you desire to do so.

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnRegRead]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnRegRead]
GO

CREATE   FUNCTION fnRegRead (@rootkey varchar(100) = 'HKEY_LOCAL_MACHINE', 
	@key varchar(500),
	@value_name varchar(100))
RETURNS varchar(4000)
AS
BEGIN
	DECLARE @ret varchar(4000)
	
	/* 
	xp_regread 
	xp_regread [@rootkey=]'rootkey', [@key=]'key'[, [@value_name=]'value_name'][, [@value=]@value OUTPUT] 

	@rootkey Possible values :
		HKEY_CLASSES_ROOT
		HKEY_CURRENT_USER
		HKEY_LOCAL_MACHINE
		HKEY_USERS
		HKEY_CURRENT_CONFIG
	*/


	EXEC master..xp_regread @rootkey, @key, @value_name, @ret OUTPUT
	
	RETURN @ret
END

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnRegWrite]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnRegWrite]
GO

CREATE   FUNCTION fnRegWrite (@rootkey varchar(100) = 'HKEY_LOCAL_MACHINE', 
	@key varchar(500),
	@value_name varchar(100),
	@type varchar(30) = 'REG_SZ',
	@value varchar(1024))
RETURNS int
AS
BEGIN
	DECLARE @ret int
	
	/* 
	xp_regwrite 
	xp_regwrite [@rootkey=]'rootkey', [@key=]'key', [@value_name=]'value_name', [@type=]'type', [@value=]'value', 
	
	@type can be REG_SZ for string values or REG_DWORD for integers 

	@rootkey Possible values :
		HKEY_CLASSES_ROOT
		HKEY_CURRENT_USER
		HKEY_LOCAL_MACHINE
		HKEY_USERS
		HKEY_CURRENT_CONFIG

	*/

	EXEC @ret = master..xp_regwrite @rootkey, @key, @value_name, @type, @value
	
	RETURN @ret
END


GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Rate

Share

Share

Rate