代码之家  ›  专栏  ›  技术社区  ›  golosovsky

本地计算机上的OpenSCManager-NULL与lpMachineName中的“localhost”

  •  0
  • golosovsky  · 技术社区  · 5 年前

    根据the MSDN documentation 在 lpMachineName 参数 OpenSCManager WinApi 功能:

    如果指针为NULL或指向空字符串,则函数 连接到本地计算机上的服务控制管理器。

    传递“localhost”或“127.0.0.1”是否会导致与传递NULL或空字符串完全相同的行为?或者,在调用之前,我是否应该在逻辑中执行检查,并将“localhost”或“127.0.0.1”替换为NULL OpenSCManager ?

    0 回复  |  直到 5 年前
        1
  •  1
  •   RbMm    5 年前

    OpenSCManager 当然,使用特殊的快速路径连接到本地服务控制管理器。使用此快速路径 OpenSCManager 需要以某种方式检测到我们想要连接到本地数据库。这当然是通过支票完成的 lp机器名称 .最佳且有记录的变体-将被使用 无效的 指针在这里。如果使用空字符串,则效果几乎相同。然后可能 OpenSCManager 可以比较 lp机器名称 带有一些硬编码字符串。在我的测试中,它比较 lp机器名称 具有 本地服务器 和 . 如果不匹配,需要走慢路。对于 127.0.0.1 字符串函数未检查。

    为了比较快速和慢速路径 OpenSCManager :

    快速路径:(此处和所选路径)

    enter image description here

    慢速路径:

    enter image description here

    快速路径:

    enter image description here

    慢速路径:

    enter image description here

    快速路径:

    enter image description here

    慢速路径:

    enter image description here