Skip to content

Commit

Permalink
Merge pull request #13 from huskar-t/fix/get-remote-servers
Browse files Browse the repository at this point in the history
fix: get servers from registry
  • Loading branch information
huskar-t authored Sep 3, 2024
2 parents 3c4466b + da17717 commit 84489e4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
67 changes: 61 additions & 6 deletions opcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,35 @@ func getClsIDFromServerList(progID, node string, location com.CLSCTX) (*windows.
}

func getClsIDFromReg(progID, node string) (*windows.GUID, error) {
var clsid windows.GUID
var clsid *windows.GUID
var err error
hKey, err := registry.OpenRemoteKey(node, registry.CLASSES_ROOT)
if err != nil {
return nil, err
}
defer hKey.Close()

hProgIDKey, err := registry.OpenKey(hKey, progID, registry.READ)
if err != nil {
return nil, err
}
defer hProgIDKey.Close()
_, clsid, err = getClsidFromProgIDKey(hProgIDKey)
return clsid, err
}

func getClsidFromProgIDKey(hProgIDKey registry.Key) (string, *windows.GUID, error) {
hClsidKey, err := registry.OpenKey(hProgIDKey, "CLSID", registry.READ)
if err != nil {
return nil, err
return "", nil, err
}
defer hClsidKey.Close()
clsidStr, _, err := hClsidKey.GetStringValue("")
if err != nil {
return nil, err
return "", nil, err
}
clsid, err = windows.GUIDFromString(clsidStr)
return &clsid, err
clsid, err := windows.GUIDFromString(clsidStr)
return clsidStr, &clsid, err
}

type ServerInfo struct {
Expand All @@ -175,6 +181,14 @@ type ServerInfo struct {

// GetOPCServers get OPC servers from node
func GetOPCServers(node string) ([]*ServerInfo, error) {
result, err := getServersFromOpcServerList(node)
if err != nil {
return getServersFromReg(node)
}
return result, nil
}

func getServersFromOpcServerList(node string) ([]*ServerInfo, error) {
location := com.CLSCTX_LOCAL_SERVER
if !com.IsLocal(node) {
location = com.CLSCTX_REMOTE_SERVER
Expand All @@ -188,7 +202,7 @@ func GetOPCServers(node string) ([]*ServerInfo, error) {
sl := &com.IOPCServerList2{IUnknown: iCatInfo}
iEnum, err := sl.EnumClassesOfCategories(cids, nil)
if err != nil {
return nil, NewOPCWrapperError("server list EnumClassesOfCategories", err)
return nil, NewOPCWrapperError("enum classes of categories", err)
}
defer iEnum.Release()
var result []*ServerInfo
Expand All @@ -208,6 +222,47 @@ func GetOPCServers(node string) ([]*ServerInfo, error) {
return result, nil
}

func getServersFromReg(node string) ([]*ServerInfo, error) {
var result []*ServerInfo
var err error
hKey, err := registry.OpenRemoteKey(node, registry.CLASSES_ROOT)
if err != nil {
return nil, err
}
defer hKey.Close()
tsKeys, _ := hKey.ReadSubKeyNames(-1)
for _, tsKey := range tsKeys {
info := getServersFromKey(hKey, tsKey)
if info != nil {
result = append(result, info)
}
}
return result, nil
}

func getServersFromKey(hKey registry.Key, progID string) *ServerInfo {
hProgIDKey, err := registry.OpenKey(hKey, progID, registry.READ)
if err != nil {
return nil
}
defer hProgIDKey.Close()
hOPCKey, err := registry.OpenKey(hProgIDKey, "OPC", registry.READ)
if err != nil {
return nil
}
defer hOPCKey.Close()
clsidStr, clsid, err := getClsidFromProgIDKey(hProgIDKey)
if err != nil {
return nil
}
return &ServerInfo{
ProgID: progID,
ClsStr: clsidStr,
VerIndProgID: progID,
ClsID: clsid,
}
}

func getServer(sl *com.IOPCServerList2, classID *windows.GUID) (*ServerInfo, error) {
progID, userType, VerIndProgID, err := sl.GetClassDetails(classID)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions opcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,16 @@ func Test_getClsIDFromServerList(t *testing.T) {
})
}
}

func Test_getServersFromReg(t *testing.T) {
localNode, err := windows.ComputerName()
if err != nil {
t.Fatal(err)
}
servers, err := getServersFromReg(localNode)
assert.NoError(t, err)
assert.NotEmpty(t, servers)
for _, server := range servers {
t.Log(server)
}
}

0 comments on commit 84489e4

Please sign in to comment.