Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Wednesday, September 21, 2005

VBScript to install COM DLL as COM+ application

''' Create COM+ application via COMAdmin.COMAdminCatalog
''' Onega(www.fruitfruit.com)
''' SAMPLE usage BEGIN
test_prog_id = "scdebugutil.DebugStream"
start_time = Now()
register_result = register_com_plus_app(test_prog_id)
end_time = Now()

if register_result then
WScript.Echo "create COM+ application for " & test_prog_id & " successfully takes " _
& CStr(DateDiff("s",start_time,end_time)) & " seconds"
else
WScript.Echo "Failed to create COM+ application for " & test_prog_id & " takes " _
& CStr(DateDiff("s",start_time,end_time)) & " seconds"
end if
''' SAMPLE usage END


''' register COM+ application via its ProgID
''' return true if succeeded
function register_com_plus_app( ProgID)
com_application_name = ProgID
register_com_plus_app = false
On Error Resume Next
FPath = get_com_path_by_prog_id(ProgID)
if len(FPath) < 1 then
WScript.Echo ProgID & " can't be find in registry"
exit function
end if
Set objCat=CreateObject("COMAdmin.COMAdminCatalog")
Set objApps = objCat.GetCollection("Applications")
objApps.Populate
''' check if this application already exists
for each app in objapps
if app.Name = com_application_name then
WScript.Echo com_application_name & " already exists. Action aborted"
exit function
end if
next

''' Create a default application
objApps.Add
objApps.SaveChanges

Set objApp = objApps.Item(0)
Do While objApp.Name <> "New Application"
count = count + 1
Set objApp = objApps.Item(count)
Loop

if objApp.Name = "New Application" then

objApp.Value("Name") = com_application_name
objApps.SaveChanges
objCat.InstallComponent com_application_name, FPath, "", ""
objApps.SaveChanges
register_com_plus_app = true
end if
set objApps = nothing
set objCat = nothing
end function


''' get COM library path by its ProgID via looking registry
''' return empty string if the COM is not registered or invalid ProgID
function get_com_path_by_prog_id(com_prog_id)
Set WSHShell = WScript.CreateObject("WScript.Shell")
strRegKey = "HKCR\" & com_prog_id & "\CLSID\"
com_clsid = WSHShell.RegRead(strRegKey)
strRegKey = "HKCR\CLSID\" & com_clsid & "\InprocServer32\"
com_path = WSHShell.RegRead( strRegKey)
if len(com_path)<1 then
strRegKey = "HKCR\CLSID\" & com_clsid & "\LocalServer32\"
com_path = WSHShell.RegRead( strRegKey)
end if
get_com_path_by_prog_id = com_path
end function

0 Comments:

Post a Comment

<< Home