在vb.net中的函数中处理错误 [英] Error handling in a function in vb.net

查看:139
本文介绍了在vb.net中的函数中处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调查我使用控制台应用程序SC时发现的Microsoft Security Essentials服务控制器的新锁定时,命令sc sdshow MsMpSvc返回S-1-5-85-685333868服务的SID- 2237257676-1431965530-1907094206-2438021966。使用我在网上找到的代码构建的转换工具,它转换为用户名NT SERVICE\MsMpSvc。

我开始想知道我是否可以构建一个枚举服务的应用程序来获取服务的名称,然后获取每个服务的SID。我发现在我的系统上安装的171个服务中有1个没有从Domain \Username映射到SID,那就是AdobeARMservice。该服务返回一个System.Security.Principal.IdentityNotMappedException,但这是目前唯一的一个。



我在想的是如果我可以检查一下像如果svcname(NT SERVICE \ Adob​​eARMservice)。存在然后做任何事情,但没有任何功能,我可以找到那么简单,我不知道如何写它或如果这个想法甚至可以工作。



问题在于MicrosoftTranslate函数中缺少错误处理,它会抛出错误然后你就完成了。我无法想出办法来处理错误然后继续。



下面列出的函数就像解决方法一样但我想做一个更好的植入但不知道如何。

任何想法都表示赞赏。谢谢你的时间。



While investigating the new lockdown of the Microsoft Security Essentials Service controller I discovered while using the console application ‘SC" that the command "sc sdshow MsMpSvc" returns a SID for the service of S-1-5-80-685333868-2237257676-1431965530-1907094206-2438021966. Using a conversion tool I built using code found online it converted to the username of "NT SERVICE\MsMpSvc".
I started wondering if I could build an app that enumerates the services to get the names of the services and then gets the SID for each service. As I found out 1 out of 171 services installed on my system does not map from a Domain\Username to SID and that is the "AdobeARMservice". That service returns a "System.Security.Principal.IdentityNotMappedException" but that is currently the only one that does.

What I was thinking was If I could do a check like "If svcname("NT SERVICE\AdobeARMservice").exist then" do whatever, but there is no function I can find that is that simple and I’m not sure how to write it or if this Idea would even work.

The problem is in a lack of error handling in the Microsoft "Translate" function, it throws the error then you’re done. I can''t come up with a way to handle the error then continue.

The function listed below works as is as a workaround but I would like to do a better implantation but don’t have a clue how.
Any Ideas are appreciated. And thank you for your time.

Private Function convertServiceNameToSID(ByVal name As String)
    Dim op As String

    Dim svcname As String = name
    If svcname = "NT SERVICE\AdobeARMservice" Then
        Return "not supported"
    Else
        Dim f As New Principal.NTAccount(svcname)

        Dim s As Principal.SecurityIdentifier

        s = DirectCast(f.Translate(GetType(Principal.SecurityIdentifier)), Principal.SecurityIdentifier)
        Dim sidString As String = s.ToString()
        op = sidString
    End If

    Return op
End Function

推荐答案

在Try / Catch块中包装 s = DirectCast(f.Translate ...)行。当Translate抛出时,你的Catch块会执行,你可以将任何你想要的字符串返回给调用者。



真的,这听起来好像你需要通过一个初学者关于VB.NET的书。异常处理是一项基本技能。
Wrap your s = DirectCast(f.Translate...) line in a Try/Catch block. When Translate throws, your Catch block will execute where you can return any string you want to the caller.

Really, it sounds as though you need to go through a beginners book on VB.NET. Exception handling is a basic skill.


这篇关于在vb.net中的函数中处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆