Using callback functions in vbscript - winapi

I'm trying to make an update script for windows7 in vbscript
when invoking IUpdateSearcher::BeginSearch how do I pass the callback to ISearchCompletedCallback::Invoke Method?
I'm just clueless on this points:
do I need an function or sub or does this a custom object with an invoke method(and how to create)
how I need to pass the callback
is it even possible in vbscript (if not what is a good next step?)
Thanks

I've never tried it, but I'd look at the ConnectObject Method.
This article about scripting events might also be useful.
So maybe something like this (complete guess):
Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher
WScript.ConnectObject objSearcher, "searcherCallBack_"
objSearcher.BeginSearch ...
sub searcherCallBack_Invoke()
' handle the callback
end sub
I'd also suggest reading Guidelines for Asynchronous WUA Operations to make sure that you clean up after yourself.
that link also mentions using Windows Script Host, so it should definitely be possible to do this, though unless you need it to be asynchronous, the synchronous methods wouls probably be a easier.

Related

Delete Word Document from location - VB UFT

As suggested in the title, it looks like a simple question but i didn't find any solution so far.
So i would like to delete a Word document from a file system, share, file explorer; so any location.
I didn't find a way to deal with that concern.
I've tried simple DeleteFile function but it seems it doesn't handle word file.
Function DeleteAFile(filespec)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filespec)
End Function
The same i tried creating a oWord object ( an instance of a COM component ) but there is no delete method or event available.
Set oWord = CreateObject("Word.Application")
oWord.'No method nor event for delete action
So i'm blocked.
Is someone having a solution, it would be helpful.
In VBA or VB.Net the simple command is:
Kill(Path and Filename)
Based on additional information provided in the comments that only VBScript can now be used. You should consider two factors. The files you are trying to delete are Read Only and you have to then use the Force option on the DeleteFile method. The other is that your routine is receiving some error condition and thus the DeleteFile method is being stopped. You should add error checking to your routine.
For further information on the FileDelete method see the following:
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/deletefile-method

How do I programmatically set a content_security_policy?

I'm configuring the Content Security Policy for our Rails 5.2 app. I need to whitelist some domains in our CSP. I'd like to put the list of domains elsewhere so I can reference them in other places in the application, then generate the CSP headers programmatically from that list.
Looking at the source code for the Content Security Policy configuration mechanisms in Rails 5, it looks like there's some magic metaprogramming going on, so it's not clear to me how to accomplish what I need to do. It looks like the functions I need to call to set headers might be picky about how exactly they want to be called. In particular, it's not clear to me if I can pass them arrays or safely call them multiple times, or if they do some metaprogramming magic that only works if the domains are passed in as individual function arguments.
Can I pass in an array to the header I want to set, like this?
whitelisted_domains = ['https://example.com', 'self']
Rails.application.configure do
config.content_security_policy do |csp|
csp.child_src whitelisted_domains
end
end
Or can I call the same function multiple times, like this?
whitelisted_domains = ['https://example.com', 'self']
Rails.application.configure do
config.content_security_policy do |csp|
whitelisted_domains.each {|domain| csp.child_src domain}
end
end
If neither of those will work, what's the best way of accomplishing what I want to do?
From what I can tell from sourcecode and documentation, it takes an array. From the edgeguides at rails, posting following
Rails.application.config.content_security_policy do |policy|
policy.default_src :self, :https
...
end
and the sourcecode, using *sources as param; it believe it takes any number of arguments, meaning you could do something along the lines of;
whitelisted_domains = ['https://example.com', 'self']
Rails.application.configure do
config.content_security_policy do |csp|
csp.child_src(*whitelisted_domains)
end
end
https://blog.sqreen.io/integrating-content-security-policy-into-your-rails-applications-4f883eed8f45/
https://edgeguides.rubyonrails.org/security.html#content-security-policy
Sourcecode of define_method for each directive
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/content_security_policy.rb#L151
(note: None of this has been tested in a Rails app, simple looking guides and source code of Rails)

QTP/UFT - Function That Creates WebLink Variables

I am thinking of doing something I am not sure if it is possible - perhaps not the best practice even, but nonetheless want to give it a try.
I have a function library and I have the action in which I do the main scripting, obviously calling functions from the library.
Originally in my action, the code looked something like this:
Code
Browser("Browser").Page("Page").WebElement("Element").Click
I have, however, changed it to this:
Code
Set WebLink = Browser("Browser").Page("Page")
WebLink.WebElement("Element").Click
I did this because i feel that it has a cleaner look with "less" code in each line.
I know that I can code a function to do this:
Public Function myLink(WebLink)
Set WebLink = Browser("Browser").Page("Page")
End Function
Then in my action, I do a call just once at the top:
Code
Call myLink(WebLink)
WebLink.WebElement("Name").HighLight
However, I was thinking that in some instances, I would have different page names, for example:
Code
Browser("Browser").Page("Page1")
Browser("Browser").Page("Page2")
Browser("Browser").Page("Page3")
So perhaps, creating a function that would store my variables and then I am able to call the variables could be an alternative.
My function could look like this:
Code
Public Function myLinks(WebLink)
Dim Pages(): Pages = ("Login","CreateUser","SelectOption","DeleteUser",)
Browser("Browser").Page(Pages())
For Each Elem In Pages
Set WebLink = Browser("Browser").Page(Pages())
End For
End Function
Then in my action, I use it like this:
Code
Call myLinks(WebLink)
Login.WebElement("Element").Click
CreateUser.WebElement("Element").Click
SelectOption.WebElement("Element").Click
DeleteUser.WebElement("Element").Click
I know that what I have is probably illogical but if something like this could perhaps work, I would like to give it a try.
well, if you do not have the Option Explicit enabled at the beginning of your vbs files (which would force you to declare each variable before usage) then you can simply say:
For Each strPage in Pages()
ExecuteGlobal "Set " & strPage & " = Browser(""Browser"").Page(""" & strPage & """)"
Next
This one would create for you Global Variables having as names the Strings in your Pages Array. You do not need any Input Parameter
P.S: A better name would be InitPageObjects - as what you are trying to achieve has nothing to do with Links

JScript handling COM events

I'm trying to figure out how I can add a JScript event handler to a COM interface. In this API
http://helpnet.installshield.com/installshield17helplib/IHelpAutoISWiRelease.htm
there are 3 Build Status Events I want to observe. Reading Microsofts documentation
http://msdn.microsoft.com/en-us/library/vstudio/06t47502(v=vs.100).aspx
leads me to believe I need to use a ISWiRelease.add_XXXXX(event_function) approach. But the ISWiRelease documentation does not list the "add event handler" methods.
Figuring this out seems like it should be simple. But I'm banging my head against a wall. There is an example for detecting the events with Visual Basic here
http://helpnet.flexerasoftware.com/installshield21helplib/helplibrary/AutomationBuildStatEv.htm
but that is of little help to me.
I tried to list the functions/methods using these approaches
How to display all methods of an object in Javascript?
How to list the functions/methods of a javascript object? (Is it even possible?)
but when I loop over the ISWiRelease object it acts as if it is empty.
How can I figure out the names of the addXXXX(..) event functions?
Additional Info:
I'm a JScript/wsf noob
The JScript is called via a .wsf file and cscript
I tried to add a basic import System; to the .js file which gave a syntax error (not sure why)
I'm only lightly scratching this project. I did not set it up, choose to use cscript or the Automation Interface, and would be much happier with calling the command line tool instead but I'm not in a position to make that sizeable a change.
This is a follow up to Redirecting the InstallShield log to console
Is your JScript code inside the WSF file? If so, you can add an <object> reference with events="true". Then you can define event handler functions using the objectName::eventName syntax. For example:
<job>
<object id="oWord" progid="Word.Application" events="true"/>
<script language="JScript">
function oWord::NewDocument(oDoc) {
WScript.Echo("New document: " + oDoc.Name);
}
oWord.Visible = true;
oWord.Documents.Add(); // fires the event handler
oWord.Documents.Add(); // fires the event handler again
WScript.Sleep(2000);
oWord.Quit();
</script>
</job>
(The MSDN article you linked to is about JScript.NET, which is separate from Windows Script Host.)

Cancel fax in vbscript

I can't find a way to do this anywhere, not even a mention of it. But is there is a way to programmatically cancel a fax using, say, a faxserver or faxdocument object? My current code looks something like this:
Set doc=CreateObject("FaxComEx.FaxDocument")
Set server=CreateObject("FaxComEx.FaxServer")
server.Connect ""
doc.Body="c:\somefile.txt"
doc.DocumentName="test fax"
doc.Recipients.Add "1555555555555"
doc.Priority = 1
doc.ConnectedSubmit(server)
Using the faxserver.faxserver and faxserver.faxdoc objects doesn't look much more promising, as the only faxdoc method I see is Send. Is canceling a fax just not possible? Thanks!
A related class to FaxDocument is FaxOutgoingJob that has a Cancel method.
I've never used it myself but I think that what you have to do is to use the FaxAccountFolders object and it's OutgoingQueue property which has a GetJobs method.

Resources