Code example to check only one program instance

Add the following code to your WinMain() function to check that only one instance of a program is running. The code snippet first checks to see if the application exists, if it does the application will be brought to the foreground.

In this example we have a win32 based Borland TApplication with the name "Application_name" (i.e. Application->Title = "Application_name").

Show Plain Text
C code
  1. WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  2. {
  3.     HWND HWnd;
  4.     if ((HWnd = FindWindow("TApplication", "Application_name")) != NULL)
  5.     {
  6.         ShowWindow(HWnd, SW_RESTORE);
  7.         SetForegroundWindow(HWnd);
  8.         return 0;
  9.     }
  10.     ...
  11. }
Filed under: Code Snip  Tags: C

0 Responses to “Code example to check only one program instance”

Sorry, comments have been closed for this post.
(default) 11 queries took 7 ms
NrQueryErrorAffectedNum. rowsTook (ms)
1DESCRIBE `posts`17171
2DESCRIBE `comments`11111
3DESCRIBE `tags`221
4DESCRIBE `categories`221
5DESCRIBE `posts_tags`221
6DESCRIBE `categories_posts`221
7SELECT `Post`.`id`, `Post`.`url`, `Post`.`title`, `Post`.`icon`, `Post`.`metadesc`, `Post`.`metakeys`, `Post`.`categories`, `Post`.`tease`, `Post`.`body`, `Post`.`private_body`, `Post`.`created`, `Post`.`modified`, `Post`.`status`, `Post`.`allow_comments`, `Post`.`tags`, `Post`.`hitcount`, `Post`.`hitcount_rss` FROM `posts` AS `Post` WHERE `Post`.`url` = 'code-example-to-check-only-one-program-instance' LIMIT 1110
8SELECT `Comment`.`id`, `Comment`.`post_id`, `Comment`.`body`, `Comment`.`author`, `Comment`.`url`, `Comment`.`email`, `Comment`.`ip`, `Comment`.`status`, `Comment`.`junk_score`, `Comment`.`created`, `Comment`.`modified` FROM `comments` AS `Comment` WHERE `Comment`.`status` = 2 AND `Comment`.`post_id` = (21) 001
9SELECT `Tag`.`id`, `Tag`.`tag`, `PostsTag`.`post_id`, `PostsTag`.`tag_id` FROM `tags` AS `Tag` JOIN `posts_tags` AS `PostsTag` ON (`PostsTag`.`post_id` = 21 AND `PostsTag`.`tag_id` = `Tag`.`id`) 110
10SELECT `Category`.`id`, `Category`.`category`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id` FROM `categories` AS `Category` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`post_id` = 21 AND `CategoriesPost`.`category_id` = `Category`.`id`) 110
11UPDATE `posts` AS `Post` SET `Post`.`hitcount` = Post.hitcount + 1 WHERE `Post`.`id` = 2110