Running AutoIT scripts in c#.Net - issue with WinWaitActive - c#

I am building a windows form application using c#.NET and would like to use AutoITX3.dll to run/perform simple window scripts. I have properly referenced the AutoItX3Lib and following is my code.
I tab through the program to make window "Title" active and yet, the program wouldn't continue with the script and never prints the "ITS ACTIVE" line. It seems to get stuck at WinWaitActive and I am unsure why.
autoit = new AutoItX3Lib.AutoItX3Class();
autoit.AutoItSetOption("WinTitleMatchMode", 2);
autoit.WinActivate("Title");
autoit.WinWaitActive("Title");
System.Console.WriteLine("ITS ACTIVE");

Okay, here's the deal with WinWaitActive("sometitle"), it will freeze if it can't see the title -- that is, it will wait indefinitely for the window to become active, because it's not seeing it with the parameters that you specified.
Solution:
Use the AutoIt inspector to get the title of the window, and place all that code you have into an actual AutoIt script (So you can rule out the possibility of there being an error with the library itself). Then, use that AutoIt script and tune it.
Sometimes, we have trouble matching AutoIt titles with, so what you can do is something like the following:
While( True )
$winTitle = WinGetTitle("[active]")
$matchText = "Title" ; put the title you want to match here
$match = WinActive($matchText) ; If this is 0, then it didn't match.
ConsoleWrite($winTitle & " : " & $match)
WEnd
I noticed that you also want to match substrings of the title. I've found it's best to leave that option alone and use regular expressions to match the title.
If you post the title, I'll help you find a regexp or some code to match it and give you the full code to use that in C#.
[Note: I haven't verified any of the code I posted, so I can't guarantee that it will be error-free. I'm on a Mac & I can't test it right now]
Good luck!

Related

Replace beginning and end of string with unique midle?

I have lots of code like below:
PlusEnvironment.EnumToBool(Row["block_friends"].ToString())
I need to convert them to something like this.
Row["block_friends"].ToString() == "1"
The value that gets passed to EnumToBool is always unique, meaning there is no guarantee that itll be passed by a row, it could be passed by a variable, or even a method that returns a string.
I've tried doing this with regex, but its sort of sketchy and doesn't work 100%.
PlusEnvironment\.EnumToBool\((.*)\)
I need to do this in Visual Studio's find and replace. I'm using VS 17.
If you had a few places where PlusEnvironment.EnumToBool() was called, I would have done the same thing that #IanMercer suggested: just replace PlusEnvironment.EnumToBool( with empty string and the fix all the syntax errors.
#IanMercer has also given you a link to super cool, advanced regex usage that will help you.
But if you are skeptical about using such a complex regex on hundreds of files, here is what I would have done:
Define my own PlusEnvironment class with EnumToBool functionality in my own namespace. And then just replace the using Plus; line with using <my own namespace>; in those hundreds of files. That way my changes will be limited to only the using... line, 1 line per file, and it will be simple find and replace, no regex needed.
(Note: I'm assuming that you don't want to use PlusEnvironment, or the complete library and hence you want to do this type of replacement.)
in Find and Replace Window:
Find:
PlusEnvironment\.EnumToBool\((.*))
Replace:
$1 == "1"
Make sure "Use Regular Expressions" is selected

Regex hangs trying to find match

I am trying to match an assignment string in VB code (as in I'm passing in text that is VB code into my program that's written in C#). The assignment string that I'm trying to match is something for example like
CustomClassInitializer(someParameter, anotherParameter, someOtherClassAsParameterWithInitialization()).SomeProperty = 7
and I realize that's rather complex, but it actually isn't far off from some of the real text I'm trying to match.
In order to do so I wrote a Regex. This Regex:
#"[\w,.]+\(([\w,.]*\(*,* *\)*)+ = "
which correctly matches. The problem is it becomes VERY slow (with timeouts), which I've researched and found is probably because of "backtracking". One of the suggested solutions to help with backtracking in general was to add "?>" to the regex, which I think would go in this position:
[\w,.]+\(?>([\w,.]*\(*,* *\)*)+ =
but this no longer matches properly.
I'm fairly new to Regex, so I imagine that there is a much better pattern. What is it please? Or how can I improve my times in general?
Helpful notes:
I'm only interested in position 0 of the string I'm searching for a
match in. My code is "if (isMatch && match.index == 0) { ... }. Can
I tell it to only check position 0 and if it's not a match move on?
The reason I use all the 0 or more things is the match could be as simple as CustomClass() = new CustomClass(), and as complicated as the above or perhaps a bit worse. I'm trying to get as many cases as possible.
This Regex is interested in "[\w,.]+(" and then "whatever may be inside the parentheses" (I tried to think of what all could be inside them based on the fact that it's valid VB code) until you get to the close parenthesis and then " = ". Perhaps I can use a wildcard for literally anything until it get's to ") = " in the string? - Like I said, fairly new to Regex.
Thanks in advance!
This seems to do what you want. Normally, I like to be more specific than .*, but it is working correctly. Note that I am using the Multi-line option.
^.*=\s*.+$
Here is a working example in RegExStorm.net example

Parse a HTML containing file to get the server-sided script

I'm currently building a web-server which can receive request, and send back a response.
I've managed to embed a port of Google's v8 JavaScript engine to c# (javascript.net) to my project and I want to parse a requested file and run the server-sided JavaScript code that in it. I decided that this code will be contained inside a 2-character brackets, <: for opening and :> for closing. I started to parse it with code I written but after encountering some problems which made the code more messy and probably not very efficient I decided to go ahead and try using RegEx (I had you study it because I've never used it before). BUT WAIT. After talking to my friend about it he send me this post RegEx match open tags except XHTML self-contained tags I understood that it isn't a good idea...
So my question is, How do I parse such thing? (Taking efficiency and clean code into account, after all it's a webserver).
Thanks in advance!
Ideally, what you'd want to do is hook into V8's lexer so you don't end up catching things inside of strings and such. I looked at the source to that .NET wrapper, however, and it looks like it doesn't allow that much customization. Instead, you may want to create a small state machine. You'd probably want at least these states:
Literal data (for stuff outside of your <: and :> tags)
Left angle bracket (for once you've consumed a < and are waiting for a potential :)
Script state (for stuff inside of your <: and :> tags)
Script double-quote string state
Script double-quote string escape state
Script single-quote string state
Script single-quote string escape state
Script slash state (for comments and regular expressions1)
Script line comment state
Script block comment state
Script block comment star state
Script regular expression state
Script colon state (for when you've encountered a : and are unsure whether a > or something else is next)
It may not be so quick to write as a regular expression, but it would be able to handle code like this:
Hello, world!
<:
document.write("At least you won't think the script :> ends there.");
:>
1On second thought, it's probably not so easy to detect regular expressions.
If I understand well, you want to take everything betwen "<" and ">", even "<" and ">" which are in it? Well... Since you can use RegEx for this, maybe try to find first "<", make counter which will increase for every next "<" and decrease for every ">". When the counter will be at 0, and next ">" appears: here you have end of the server-side script. If you will have some embedded HTML and want to get rid of them, try to detect """" or something like that. This solution is slow, but the simplest i can imagine.

FindWindowByClassNameAndRegex issue with special characters

For our software testing we have a test that lets us check to see if certain windows are open using the FindWindowByClassNameAndRegex P/Invoke call. The issue we're getting is when we have windows open with more than a certain number of special characters we always get IntPtr.Zero as a return. Are there any known issues with this? Here's some of the code we use to find the window: (in this case it's for a firefox window)
Regex windowTitleRegex = new Regex(Regex.Escape(fullWindowTitle).Replace("\?", "."), RegexOptions.IgnoreCase | RegexOptions.ECMAScript);
curWindowHandle = NativeMethods.FindWindowByClassNameAndRegex("MozillaUIWindowClass", windowTitleRegex);
Where the title of the window is ~`!##$%^&*()_-+={[}]|:;'<,>.?/\"àëÉùÙâÏûâÏûÊÛçîÀË«éïÂλœÇÔêôÈŒ\
(There's no actual line break it's just a formatting thing)
There is no Windows API function by that name. I'm guessing you've found some DLL that exports this function. Odds are always good that the regex this DLL uses is of some kind that doesn't quite match the syntax that .NET's Regex class uses. There are a lot of dialects.
Best thing to do is to pinvoke EnumWindows(). You can use your own Regex in the callback to filter, GetClassName() gets you the window class name. If you already know the window name then just use FindWindow().

Is it possible to programmatically 'clean' emails?

Does anyone have any suggestions as to how I can clean the body of incoming emails? I want to strip out disclaimers, images and maybe any previous email text that may be also be present so that I am left with just the body text content. My guess is it isn't going to be possible in any reliable way, but has anyone tried it? Are there any libraries geared towards this sort of thing?
In email, there is couple of agreed markings that mean something you wish to strip. You can look for these lines using regular expressions. I doubt you can't really well "sanitize" your emails, but some things you can look for:
Line starting with "> " (greater than then whitespace) marks a quote
Line with "-- " (two hyphens then whitespace then linefeed) marks the beginning of a signature, see Signature block on Wikipedia
Multipart messages, boundaries start with --, beyond that you need to do some searching to separate the message body parts from unwanted parts (like base64 images)
As for an actual C# implementation, I leave that for you or other SOers.
A few obvious things to look at:
if the mail is anything but pure plain text, the message will be multi-part mime. Any part whose type is "image/*" (image/jpeg, etc), can probably be dropped. In all likelyhood any part whose type is not "text/*" can go.
A HTML message will probably have a part of type "multipart/alternative" (I think), and will have 2 parts, one "text/plain" and one "text/html". The two parts should be just about equivalent, so you can drop the HTML part. If the only part present is the HTML bit, you may have to do a HTML to plain text conversion.
The usual format for quoted text is to precede the text by a ">" character. You should be able to drop these lines, unless the line starts ">From", in which case the ">" has been inserted to prevent the mail reader from thinking that the "From " is the start of a new mail.
The signature should start with "-- \r\n", though there is a very good chance that the trailing space will be missing.
Version 3 of OSBF-Lua has a mail-parsing library that will handle the MIME and split a message into its MIME parts and so on. I currently have a mess of Lua scripts that do
stuff like ignore most non-text attachments, prefer plain text to HTML, and so on. (I also wrap long lines to 80 characters while trying to preserve quoting.)
As far as removing previously quoted mail, the suggestions above are all good (you must subscribe to some ill-mannered mailing lists).
Removing disclaimers reliably is probably going to be hard. My first cut would be simply to maintain a library of disclaimers that would be stripped off the end of each mail message; I would write a script to make it easy for me to add to the library. For something more sophisticated I would try some kind of machine learning.
I've been working on spam filtering since Feb 2007 and I've learned that anything to do with email is a mess. A good rule of thumb is that whatever you want to do is a lot harder than you think it is :-(
Given your question "Is it possible to programmatically ‘clean’ emails?", I'd answer "No, not reliably".
The danger you face isn't really a technological one, but a sociological one.
It's easy enough to spot, and filter out, some aspects of the messages - like images. Filtering out signatures and disclaimers is, likewise, possible to achieve (though more of a challenge).
The real problem is the cost of getting it wrong.
What happens if your filter happens to remove a critical piece of the message? Can you trace it back to find the missing piece, or is your filtering desctructive? Worse, would you even notice that the piece was missing?
There's a classic comedy sketch I saw years ago that illustrates the point. Two guys working together on a car. One is underneath doing the work, the other sitting nearby reading instructions from a service manual - it's clear that neither guy knows what he's doing, but they're doing their best.
Manual guy, reading aloud: "Undo the bold in the centre of the oil pan ..." [turns page]
Tool guy: "Ok, it's out."
Manual guy: "... under no circumstances."
If you creating your own application i'd look into Regex, to find text and replace it. To make the application a little nice, i'd create a class Called Email and in that class i have a property called RAW and a property called Stripped.
Just some hints, you'll gather the rest when you look into regex!
SigParser has an assembly you can use in .NET. It gives you the body back in both HTML and text forms with the rest of the stuff stripped out. If you give it an HTML email it will convert the email to text if you need that.
var parser = new SigParser.EmailParsing.EmailParser();
var result = await parser.GetCleanedBodyAsync(new SigParser.EmailParsing.Models.CleanedBodyInput {
FromEmailAddress = "john.smith#example.com",
FromName = "John Smith",
TextBody = #"Hi Mark,
This is my message.
Thanks
John Smith
888-333-4434"
});
// This would print "Hi Mark,\r\nThis is my message."
Console.WriteLine(result.CleanedBodyPlain);

Categories