Showing posts with label Application programming interface. Show all posts
Showing posts with label Application programming interface. Show all posts

25 April, 2018

Forcing A Windows Application To Quit On Exit

English: Graphic which hints to Microsoft Windows
English: Graphic which hints to Microsoft Windows (Photo credit: Wikipedia)
25-April-2018

Forcing A Windows Application To Quit On Exit. That was my recent problem with a Windows Forms Application I was finalizing for deployment. It is not a big program. And it is not even one that is complicated. Straightforward and simple. That's what it is. But somehow, it carried a bug.

The .exe isn't terminated by Windows. It remains in the system. I can see it using Task Manager. And how did I find out about it? I am not able to execute a Ctrl-F5 properly. I always get an error.

That is when I started checking here and there, and I found out, the program's .exe remained in the system even after I close the program. Why is that? Beats me!

I opened one of my Windows Forms program, compared the code side-by-side, and no, there is nothing different. It is the same lines of code for the 2 programs, but the former closes and system disposes of everything. The latter, no. the Form closes, but the .exe remains in the system. So I can't start a new program since to the system, it is 'still running'!

Why it behaves that way, I do not know.

Now, I said this is a piece of cake. It is easy. I know how to terminate a program so that it is removed from the Taskbar. I was thinking that the same trick should work on this new program. I mean, why shouldn't it?

But I was wrong. It did not work! How can Visual Studio codes be so quirky?

Anyway, I did some direct codes, testing a few lines a couple of times. And guess what? Here's what worked:

Application.DoEvents();
if (Application.MessageLoop) {
    // WinForms app
    Application.DoEvents();
    Application.Exit();
}
else {
    // Console app
    Environment.Exit(0);
}

That 'if' section, it is meant to be for Windows Forms applications, and the 'else' clause, that is meant to be for non-Windows forms programs, like a Console application. That's the 'normal' way of things. Supposedly the 'right' logic.

But wait.

After trying it the normal way, it didn't work. So I tried it the extra-normal way: use Environment.Exit(0) in my Windows Forms application. And you'll guess it already. It worked!

I guess that is an experienced programmer's edge over the newbie. Not all the time, things work normally. And when it happens, the veteran knows how it is done rightfully, or maverick.

So I hope that you have learned a new thing today: Forcing A Windows Application To Quit On Exit.

Till then!


[Drafted on 26-March-2018]


06 September, 2017

Singleton: Create A Single-Instance Application

UML class diagram of the singleton design pattern
UML class diagram of the singleton design pattern (Photo credit: Wikipedia)
06-September-2017


Create a single-instance application. That was the objective I thought of lately on one small program I am creating. That is due to some database update needed. So no double-update or better yet, multiple updates, must happen.

Okay. It was actually simple, but here I will just outline the simple steps - again, for my own use when I need it in the future - and also for anybody else who might need to implement this same feature in their programs.

So how do you create a single-instance application? How to enforce a singleton pattern?

It is very easy. Just look at the code snippet below. And this is using Mutex in the Program section where your code execution starts.


internal static class Program {
private static Mutex mutex = new Mutex(true, "{a381c74c-c9a1-4716-beff-e8a08e964462}");

[STAThread]
private static void Main() {
if (mutex.WaitOne(TimeSpan.Zero, true)) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
mutex.ReleaseMutex();
}
else {
MessageBox.Show("Only one instance allowed.");
}
}
}


So that is the baseline requirement to satisfy a singleton pattern. And if you are wondering the seemingly cryptic string in the Mutex declaration, fret not. It is just a string that Mutex implementation requires, so it can be anything, like the name of your application, but it must be unique.

Alright, if you insist on using the same seemingly cryptic alphanumeric combination so it is unique, then add these 2 lines somewhere in the form load event, and just capture that value, and put it in the Mutex declaration. And comment those 2 lines right away as they are no longer needed.


var guiId = System.Guid.NewGuid();
MessageBox.Show("guiId");


Yes, that's how I got mine.

Now there is an additional feature that can be included, and not just simply throwing a message box. The thing is, why not bring up the instance of the running application instead?

Well, that is possible.

Modify the Program section as per below code, which means disabling the message box and replacing it with some nifty Win32 function (API). It is sending a message to all other running applications, but since it is unique only to this application, then it is understood only by this same application, so only this particular program will respond. Mind-boggling, isn't it?

Let's get done with it then!


internal static class Program {
private static Mutex mutex = new Mutex(true, "{a381c74c-c9a1-4716-beff-e8a08e964462}");

[STAThread]
private static void Main() {
if (mutex.WaitOne(TimeSpan.Zero, true)) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
mutex.ReleaseMutex();
}
else {
// MessageBox.Show("Only one instance allowed.");
// send our Win32 message to make the currently running instance
// jump on top of all the other windows
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
}
}
}


As for NativeMethods.cs, you need to create a new class for that.


internal class NativeMethods {
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");

[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
}


Then, to complete the code, add two procedures after the Main Form's call to InitializeComponent.


public MainForm() {
InitializeComponent();
}

protected override void WndProc(ref Message m) {
if (m.Msg == NativeMethods.WM_SHOWME) {
ShowMe();
}
base.WndProc(ref m);
}

private void ShowMe() {
Show();
if (WindowState == FormWindowState.Minimized) { WindowState = FormWindowState.Normal; }
// get our current "TopMost" value (ours will always be false though)
var top = TopMost;
// make our form jump to the top of everything
TopMost = true;
// set it back to whatever it was
TopMost = top;
}


Now this is all that it takes to create a singleton implementation of a program. This is written in C#, so feel free to adapt this to other languages as you deem fit.


Sources

I am simply blogging on this singleton implementation, on how to create a single-instance application, but the source articles are here and here, thanks to the authors! Read on these 2 articles to get more explanations, and other suggestions, too.


Until then, happy coding!


28 July, 2017

Yahoo Bizmail SMTP Settings Configuration Problem

English: SMTP transfer model Blue arrows can b...
English: SMTP transfer model Blue arrows can be implemented by SMTP variations (Photo credit: Wikipedia)
28-July-2017


Lately, yahoo bizmail SMTP settings or configuration throws error. The password is not being accepted. Despite confirming it to be correct!

And if you try to search for solutions and resolutions, all that will be offered is the usual procedure for setting up a new account, POP e-mail, IMAP e-mail, keying in the user name, full e-mail and password, and the servers:

pop.bizmail.yahoo.com, port 995, SSL security
smtp.bizmail.yahoo.com, port 465, SSL security, if not, TLS and SSL security

and the usual More Settings.

But despite entering, checking and verifying at least 3 times that all the entries are correct, only incoming mail goes okay and Test passes and is marked ‘Completed’, but not for incoming mail, which uses SMTP: it will keep on asking for your password, then eventually, is marked ‘Failed’.

So what really is the problem?

It could be Yahoo SMTP Server problem, as it happened to so many users.

But one solution that I found is this: register inetcomm.dll.

I also needed to use my personal laptop, which has my personal e-mail, so that I cannot touch. But being more comfortable using my own, I just wanted to ‘add in’ my work e-mail using the same application: Microsoft Outlook.

So, I just proceed to add a new account, entered everything correctly, key in my password, and naturally, ran into the SMTP ‘enter password’ issue.

I tried everything that I can find on the web, but nothing. Nada. Zilch. Still the same problem on the SMTP outgoing mail, asking for a password, even though it is the correct password already.

Then I found one article that threw a few possible suggestions, but on top is to register inetcomm.dll:

Open up an elevated-privilege command prompt session
1. Press Windows key + R
2. In the box, type cmd
3. Press Ctrl + Shift + Enter

Go to C:\Windows\System32
1. Type C: if you are not in C: drive
2. Type cd\ and press Enter
3. Type cd windows\system32 and press Enter (Windows\System32 also can)
4. Then finally, type regsvr32 inetcomm.dll and press Enter
5. An error will be reported, but just ignore that and close the opened box.

That’s it!

This is all that fixed the problem on the outgoing mails for Yahoo's smtp.bizmail.yahoo.com, using port 465. And I have confirmed it on my own account, as on other users. I am setting up a few laptops running Windows 10 64 bit and using Microsoft Outlook 2010. Mine is Windows 10 64 bit and Microsoft Outlook 2007.

Try it!

08 July, 2017

How To Remove Cleanserp.net Search Engine Hijacker From Chrome

Logo used from the start of the Chrome project...
Logo used from the start of the Chrome project until March 2011 (Photo credit: Wikipedia)
July 8, 2017


Recently I was plagued by cleanserp.net search engine taking over Google. I do a search and out comes cleanserp.net taking over Google.com, and that is even when you type Google.com in the web address!

I let this happen for a couple of days, until I got fed up. I was thinking that it was a bit difficult to remove this search engine browser hijacker, but my curiosity got the better of me.

So I did a search, and guess what? It is easy enough, easier than I expected, even if it were to be done manually.

I'm not telling anything anymore about what cleanserp.net is, of how it got into your computer, and what it does, and more.

So here are the steps:

1. Check the browser shortcut by doing right-click on it, then Properties. If after the .exe it shows "http://cleanserp.net", remove that. Then click on OK.

2. Change your startup page, it must not be "http://cleanserp.net".

3. Uninstall any program that may be related to anything rogue or questionable. Since you know your computer better, you know what you are installing, and anything that doesn't look like a legit program, you uninstall these.

Specific to Google Chrome, if changing the search engine throws out “This setting is enforced by your administrator”, try the following (this is what fixed my problem):

1. Open %WINDIR%\System32\GroupPolicy folder (just copy that path and paste into the address bar of Windows Explorer). Delete the contents of that folder.

2. Next, open %WINDIR%\System32\GroupPolicyUsers and delete all of its contents.

3. Finally, restart your computer.

There is a possibility that a few more minutes will be added than before to your computer's restart process, but that is just the first time reboot after doing the change. it would return to normal afterwards.

Some more info can be got here: How to Remove Cleanserp.net [Chrome, Firefox, IE, Edge]

Hope this helps.

Thank you, and till then!

01 February, 2015

Android 5.0 in my Samsung Galaxy Note II (N7105)

The official online color is: #A4C639 . 한국어: 공...
The official online color is: #A4C639 . 한국어: 공식 온라인 색은: #A4C639 . (Photo credit: Wikipedia)
01-Feb-2015



Today seems a good day to write about this article... before it is forgotten...

A year or so after I flashed Android 4.4.2 ROM in my GT-N7105 phone, I started looking for some update to the OS. I believe there was one or two upgrades in the past year, and since I am getting none when I checked using my phone, I searched from the web using my PC.

Found that there is the UNOFFICIAL release for Android 5.0 - Lolllipop code name.

Read through the posts, and the found the installers. Saw that GApps is also available, so what am I waiting for?

I mean, I am more of the curious type, than the cautious one...

I went to the download sites, took the ROM and GApps files, and the supposeldy CWM recovery file that goes along with this Android 5.0 version.

It was a breeze!

But not before I had to check on how to do things altogether, I mean, after a year without actually doing it, just updates.

Which means my phone had rested for some time.

I like the new OS. Grouping of apps is naturally a function. And I found that this is already taken out from the 'All Apps' view. You cannot uninstall anymore from there. So you would have to open the apps group, and if you tap-hold, you get to drag it up to 'Uninstall' or 'Homescreen' - and it does just what you chooses. OF course, system apps would tell you that you can't do an unistallation.

Notifications also get the clear all button (is that what it is called in Android?) and you do double-tap to an item in the list to open it.

I also installed CM Launcher, after so long using Go Launcher EX. With CM Launcher, I installed Battery Doctor, to replace Green Power apps. I like CM Launcher in its simplicity. I like Battery Doctor in its verboseness of info. Charged will tell me if it is fully charged, and when it is in trickle mode, a few minutes after full charging. And when done, totally, will tell you to unplug.

For the curious one like me, here are the links:



Rooting:

Lollipop ROM:

Lollipop GApps:

Installing Custom ROM:



Odin v3.04 – 3.07 for Windows



Salute to the team for getting ahead of the rest!