/********************************************************************************************/
/* xp_regread '<HKEY_LOCAL_MACHINE | HKEY_USERS>','<Key>','<Value>'						    */
/*																						    */
/* eg:																					    */
/*   xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\MSSQLServer\Server','AuditLevel'	*/
/*																						    */
/********************************************************************************************/

use master
go

if exists (select * from master.dbo.sysobjects where name = 'xp_regread')
  EXEC sp_dropextendedproc 'xp_regread'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_regwrite')
  EXEC sp_dropextendedproc 'xp_regwrite'
go
if exists (select * from master.dbo.sysobjects where name = 'xp_regdeletevalue')
  EXEC sp_dropextendedproc 'xp_regdeletevalue'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_regaddmultistring')
  EXEC sp_dropextendedproc 'xp_regaddmultistring'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_regremovemultistring')
  EXEC sp_dropextendedproc 'xp_regremovemultistring'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_regenumvalues')
  EXEC sp_dropextendedproc 'xp_regenumvalues'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_readerrorlog')
  EXEC sp_dropextendedproc 'xp_readerrorlog'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_enumerrorlogs')
  EXEC sp_dropextendedproc 'xp_enumerrorlogs'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_getfiledetails')
  EXEC sp_dropextendedproc 'xp_getfiledetails'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_perfsample')
  EXEC sp_dropextendedproc 'xp_perfsample'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_perfstart')
  EXEC sp_dropextendedproc 'xp_perfstart'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_perfend')
  EXEC sp_dropextendedproc 'xp_perfend'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_perfmonitor')
  EXEC sp_dropextendedproc 'xp_perfmonitor'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_servicecontrol')
  EXEC sp_dropextendedproc 'xp_servicecontrol'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_availablemedia')
  EXEC sp_dropextendedproc 'xp_availablemedia'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_dirtree')
  EXEC sp_dropextendedproc 'xp_dirtree'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_eventlog')
  EXEC sp_dropextendedproc 'xp_eventlog'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_fixeddrives')
  EXEC sp_dropextendedproc 'xp_fixeddrives'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_subdirs')
  EXEC sp_dropextendedproc 'xp_subdirs'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_enumqueuedtasks')
  EXEC sp_dropextendedproc 'xp_enumqueuedtasks'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_schedulersignal')
  EXEC sp_dropextendedproc 'xp_schedulersignal'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_getnetname')
  EXEC sp_dropextendedproc 'xp_getnetname'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_snmp_getstate')
  EXEC sp_dropextendedproc 'xp_snmp_getstate'
go

if exists (select * from master.dbo.sysobjects where name = 'xp_snmp_raisetrap')
  EXEC sp_dropextendedproc 'xp_snmp_raisetrap'
go

if exists(select * from sysobjects where type = 'X' and name = 'xp_sqlregister')
  EXEC sp_dropextendedproc 'xp_sqlregister'
go

if exists(select * from sysobjects where type = 'X' and name = 'xp_sqlinventory')
  EXEC sp_dropextendedproc 'xp_sqlinventory'
go

if exists (select * from sysobjects where type = 'P' and name = 'sp_sqlregister')
  drop procedure sp_sqlregister
go

if exists(select * from master.dbo.sysobjects where type = 'X' and name = 'xp_sqltrace')
  EXEC sp_dropextendedproc 'xp_sqltrace'
go

if exists(select * from master.dbo.sysobjects where type = 'X' and name = 'sp_IsMBCSLeadByte')
  EXEC sp_dropextendedproc 'sp_IsMBCSLeadByte'
go

if exists(select * from master.dbo.sysobjects where type = 'X' and name = 'sp_GetMBCSCharLen')
  EXEC sp_dropextendedproc 'sp_GetMBCSCharLen'
go


/************/
exec sp_MS_upd_sysobj_category 1
go
/************/


print 'Creating extended stored procedure xp_regread'
go
sp_addextendedproc 'xp_regread','xpstar.dll'
go
grant execute on xp_regread to repl_subscriber
go

print 'Creating extended stored procedure xp_regwrite'
go
sp_addextendedproc 'xp_regwrite','xpstar.dll'
go

print 'Creating extended stored procedure xp_regdeletevalue'
go
sp_addextendedproc 'xp_regdeletevalue','xpstar.dll'
go

print 'Creating extended stored procedure xp_regaddmultistring'
go
sp_addextendedproc 'xp_regaddmultistring','xpstar.dll'
go

print 'Creating extended stored procedure xp_regremovemultistring'
go
sp_addextendedproc 'xp_regremovemultistring','xpstar.dll'
go

print 'Creating extended stored procedure xp_regenumvalues'
go
sp_addextendedproc 'xp_regenumvalues','xpstar.dll'
go

print 'Creating extended stored procedure xp_readerrorlog'
go
sp_addextendedproc 'xp_readerrorlog','xpstar.dll'
go

print 'Creating extended stored procedure xp_enumerrorlogs'
go
sp_addextendedproc 'xp_enumerrorlogs','xpstar.dll'
go

print 'Creating extended stored procedure xp_getfiledetails'
go
sp_addextendedproc 'xp_getfiledetails','xpstar.dll'
go

print 'Creating extended stored procedure xp_perfsample'
go
sp_addextendedproc 'xp_perfsample','xpstar.dll'
go

print 'Creating extended stored procedure xp_perfstart'
go
sp_addextendedproc 'xp_perfstart','xpstar.dll'
go

print 'Creating extended stored procedure xp_perfend'
go
sp_addextendedproc 'xp_perfend','xpstar.dll'
go

print 'Creating extended stored procedure xp_perfmonitor'
go
sp_addextendedproc 'xp_perfmonitor','xpstar.dll'
go

print 'Creating extended stored procedure xp_servicecontrol'
go
sp_addextendedproc 'xp_servicecontrol','xpstar.dll'
go

print 'Creating extended stored procedure xp_availablemedia'
go
sp_addextendedproc 'xp_availablemedia','xpstar.dll'
go

print 'Creating extended stored procedure xp_dirtree'
go
sp_addextendedproc 'xp_dirtree','xpstar.dll'
go

print 'Creating extended stored procedure xp_eventlog'
go
sp_addextendedproc 'xp_eventlog','xpstar.dll'
go

print 'Creating extended stored procedure xp_fixeddrives'
go
sp_addextendedproc 'xp_fixeddrives','xpstar.dll'
go

print 'Creating extended stored procedure xp_subdirs'
go
sp_addextendedproc 'xp_subdirs','xpstar.dll'
go

print 'Creating extended stored procedure xp_enumqueuedtasks'
go
sp_addextendedproc 'xp_enumqueuedtasks','xpstar.dll'
go

print 'Creating extended stored procedure xp_schedulersignal'
go
sp_addextendedproc 'xp_schedulersignal','xpstar.dll'
go
grant execute on xp_schedulersignal to repl_subscriber
go

print 'Creating extended stored procedure xp_snmp_getstate'
go
sp_addextendedproc 'xp_snmp_getstate','xpstar.dll'
go
grant execute on xp_snmp_getstate to public
go

print 'Creating extended stored procedure xp_snmp_raisetrap'
go
sp_addextendedproc 'xp_snmp_raisetrap','xpstar.dll'
go
grant execute on xp_snmp_raisetrap to public
go

print 'Creating extended stored procedure xp_sqlregister'
go
exec sp_addextendedproc 'xp_sqlregister', 'xpstar.dll'
go
grant exec on xp_sqlregister to public
go

print 'Creating extended stored procedure xp_sqlinventory'
go
exec sp_addextendedproc 'xp_sqlinventory', 'xpstar.dll'
go

print 'Creating stored procedure sp_sqlregister'
go
-- We create this thin wrapper proc so that we can make xp_sqlregister run at startup
create proc sp_sqlregister as
begin
  exec master.dbo.xp_sqlregister
end
go

sp_makestartup 'sp_sqlregister'
go

print 'Creating extended stored procedure xp_sqltrace'
go
exec sp_addextendedproc 'xp_sqltrace', 'sqltrace.dll'
go

print 'Creating extended stored procedure xp_getnetname'
go
sp_addextendedproc 'xp_getnetname','xpstar.dll'
go
grant execute on xp_getnetname to public
go

print 'Creating extended stored procedure sp_IsMBCSLeadByte'
go
sp_addextendedproc 'sp_IsMBCSLeadByte','xpstar.dll'
go
grant execute on sp_IsMBCSLeadByte to public
go

print 'Creating extended stored procedure sp_GetMBCSCharLen'
go
sp_addextendedproc 'sp_GetMBCSCharLen','xpstar.dll'
go
grant execute on sp_GetMBCSCharLen to public
go


/************/
exec sp_MS_upd_sysobj_category 2
go
/************/


