
我怎样才能在.NET(C#或VB.NET)中以编程方式从registry中删除windows产品密钥,以重现与使用/cpky参数调用Microsoft合法slmgr.vbs脚本文件相同的效果… …请不要误解我的问题“删除”为“卸载”。 我只想删除与HKLMSOFTWAREMicrosoftwindows NTCurrentVersion : DigitalProductIDregistry值中存储和编码的windows的产品密钥对应的字节,因此产品密钥仍然安装,但第三方无法访问像ProduKey的应用程序。
我试图检查slmgr.vbs脚本文件(存储在C: windows System32 ),并导致我到这个方法块:
Private Sub ClearPKeyFromregistry() Dim obJservice On Error Resume Next set obJservice = GetServiceObject("Version") QuitIfError() obJservice.ClearProductKeyFromregistry() QuitIfError() lineOut GetResource("L_MsgClearedPKey") End Sub
然而,我有点迷路,试图find并了解从哪里来,它做了什么GetServiceObject("Version")调用,因为它似乎不是一个内置的VBS成员既不似乎是声明为脚本文件中的任何本地成员,并且我没有在MSDN文档/ VBS参考中find有关“GetServiceObject”的任何信息。
PS:请注意,我不会依靠slmgr.vbs文件的存在来解决这个问题,只需从C#中调用该脚本文件即可…
如何基于单元格在DataGrIDVIEw中单击来处理C#中的事件?
multithreading代理检查器
使用ASP.NET时,我们是否可以访问.NET Framework中的所有类?
将用户凭据保存在windows应用程序中
在处理远程连接时要听什么ip? 127.0.0.1还是实际IP?
UPDATE
我只是扫描windows文件系统的dll文件中的string“ ClearProductKeyFromregistry ”,并发现它在sppwmi.dll文件中,但不幸的是,该function没有导出,然后通过对Google的简单研究,将其引导到ClearProductKeyFromregistry方法 MSDN 上的SoftwarelicensingService类 ,但现在我不知道如何使用它。 我试图find有关如何在.NET中使用现有WMI提供程序的信息,但是我在WWW周围看到的所有信息都是关于如何实现/创builDWMI提供程序。
使用windows事件日志有哪些选项?
如何在C#中停止进程,知道它的文件名?
Mono真的是跨平台吗?
在linux上编译MonoDevelop 5.3时出错
进程启动作为域pipe理员从用户启动进程与UAC在域networking中激活
在同一个脚本中,您将找到GetServiceObject方法(以及它使用的常量和全局变量)。 要找到它们,请在脚本中搜索以下术语:
函数GetServiceObject
ServiceClass =
g_objWMIService =
L_MsgClearedPKey =
所以这只是跟踪代码和转换行的问题。 下面是我想到的完整的VBScript版本的方法,它的依赖关系:
private const L_MsgClearedPKey = "Product key from registry cleared successfully." private const ServiceClass = "SoftwarelicensingService" g_strComputer = "." Set g_objWMIService = Getobject("winmgmts:\" & g_strComputer & "rootcimv2") Private Sub ClearPKeyFromregistry() Dim obJservice On Error Resume Next set obJservice = GetServiceObject("Version") QuitIfError() obJservice.ClearProductKeyFromregistry() QuitIfError() lineOut GetResource("L_MsgClearedPKey") End Sub Function GetServiceObject(strquery) Dim obJservice Dim colServices On Error Resume Next Set colServices = g_objWMIService.Execquery("SELECT " & strquery & " FROM " & ServiceClass) QuitIfError() For each obJservice in colServices QuitIfError() Exit For Next QuitIfError() set GetServiceObject = obJservice End Function
下一步是把这个减少到一个方法。 我继续去除了所有的QuitIfError()调用和On Error Resume Next ,因为我们可以将我们的代码包装在try/catch块中。 替换常量和全局变量,并结合方法后,我想出了这个:
Dim obJservice Dim colServices Dim g_objWMIService Set g_objWMIService = Getobject("winmgmts:\.rootcimv2") Set colServices = g_objWMIService.Execquery("SELECT Version FROM SoftwarelicensingService") For each obJservice in colServices Exit For Next obJservice.ClearProductKeyFromregistry() lineOut "Product key from registry cleared successfully."
现在,由于我们使用的是WMI,我们需要引用system.management程序集并添加一个using:
using System.Management;
然后这只是一个转换的问题。 其中一些我以前没有做过,但它应该做的伎俩:
private static voID ClearProductKeyFromregistry() { const string query = "SELECT Version FROM SoftwarelicensingService"; var searcherProd = new ManagementObjectSearcher("\\.\ROOT\cimv2",query); var results = searcherProd.Get(); foreach (ManagementObject result in results) { result.InvokeMethod("ClearProductKeyFromregistry",null); break; } Console.Writeline("Product key from registry cleared successfully."); }
最后我发现了调用一个方法的方法,看这个例子 。 然后,我只是按照我在那里看到的,根据需要进行调整,所以我编写了这个在VB.NET中开发的有用的代码片段:
''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Removes the windows product key from registry (to prevent unauthorized diffusion). ''' <para></para> ''' It does not uninstall the product key. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <remarks> ''' <see href="https://msdn.microsoft.com/en-us/library/cc534586(v=vs.85).aspx"/> ''' </remarks> ''' ---------------------------------------------------------------------------------------------------- ''' <exception cref="PlatformNotSupportedException"> ''' windows 7 or newer is required to use this feature. ''' </exception> ''' ''' <exception cref="Exception"> ''' UnkNown error occurred during the product key removal attempt. ''' </exception> ''' ---------------------------------------------------------------------------------------------------- <DeBUGgerStepThrough> Public Shared Sub RemovewindowsProductKeyFromregistry() ' If Not (windowsUtils.IsWin7OrGreater) Then ' Throw New PlatformNotSupportedException("windows 7 or newer is required to use this feature.") ' End If Using query As New ManagementObjectSearcher("SELECT * FROM SoftwarelicensingService") For Each product As ManagementObject In query.Get() Dim result As UInteger Try result = CUInt(product.InvokeMethod("ClearProductKeyFromregistry",nothing)) Catch ex As COMException Marshal.ThrowExceptionForHR(ex.HResult) Catch ex As Exception Throw End Try If (result <> 0UI) Then Throw New Exception("UnkNown error occurred during the product key removal attempt.") End If Next product End Using End Sub
再加上这个其他代码片段,以编程方式安装产品密钥(它可以在我的机器上运行windows 10,但可能需要更多的测试):
''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Installs a windows product key. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <remarks> ''' <see href="https://msdn.microsoft.com/en-us/library/cc534590(v=vs.85).aspx"/> ''' </remarks> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code> ''' Dim productKey As String = "YTMG3-N6DKC-DKB77-7M9GH-8HVXX" ''' InstallProductKey(productKey) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="productKey"> ''' The product key. ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <exception cref="PlatformNotSupportedException"> ''' windows 7 or newer is required to use this feature. ''' </exception> ''' ''' <exception cref="ArgumentNullException"> ''' productKey ''' </exception> ''' ''' <exception cref="Exception"> ''' The Software licensing Service determined that the product key is invalID. ''' or ''' UnkNown error occurred during the product key installation attempt. ''' </exception> ''' ---------------------------------------------------------------------------------------------------- <DeBUGgerStepThrough> Public Shared Sub InstallProductKey(ByVal productKey As String) ' If Not (windowsUtils.IsWin7OrGreater) Then ' Throw New PlatformNotSupportedException("windows 7 or newer is required to use this feature.") ' End If Using query As New ManagementObjectSearcher("SELECT * FROM SoftwarelicensingService") For Each product As ManagementObject In query.Get() Dim result As UInteger Try result = CUInt(product.InvokeMethod("InstallProductKey",{productKey})) product.InvokeMethod("RefreshlicenseStatus",nothing) Catch ex As COMException When (ex.HResult = -1073418160) Throw New Exception("The Software licensing Service determined that the product key is invalID.",ex) Catch ex As COMException Marshal.ThrowExceptionForHR(ex.HResult) Catch ex As Exception Throw End Try If (result <> 0UI) Then Throw New Exception("UnkNown error occurred during the product key installation attempt.") End If Next product End Using End Sub
总结以上是内存溢出为你收集整理的如何以编程方式从.NETregistry中删除Windows产品密钥?全部内容,希望文章能够帮你解决如何以编程方式从.NETregistry中删除Windows产品密钥?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)