GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 3802|回复: 3

让GTK程序在同一时刻只运行一个实例

[复制链接]

该用户从未签到

发表于 2011-5-9 21:28:20 | 显示全部楼层 |阅读模式
前提条件:在windows系统中,采用mingw32编译器,C语言的程序。
在主文件的头部定义变量:
  1. int g_lAppInstance __attribute__((section ("shared"), shared)) = 0;
复制代码
在主函数int main (int argc, char *argv[])的开头部分加入对变量g_lAppInstance 的判断,“0”即没有运行实例,“1”即已经运行一个实例。
  1. if (0 == g_lAppInstance)
  2.     {
  3.      g_lAppInstance = 1;
  4.          //继续运行
  5.     }
  6.     else
  7.     {
  8.      return 0;  //退出程序
  9.     }
复制代码

在其它编辑语言(如python)或不同的操作系统(如linux)应该如何实现呢?请大家多多指教。

为什么说这个只限在windows中呢?请看网址:http://gcc.gnu.org/onlinedocs/gc ... able-attribute-1817
里面有说到:
shared
On Microsoft Windows, in addition to putting variable definitions in a named section, the section can also be shared among all running copies of an executable or DLL. For example, this small program defines shared data by putting it in a named section shared and marking the section shareable:
          int foo __attribute__((section ("shared"), shared)) = 0;
          
          int
          main()
          {
            /* Read and write foo.  All running
               copies see the same value.  */
            return 0;
          }
    
You may only use the shared attribute along with section attribute with a fully initialized global definition because of the way linkers work. See section attribute for more information.

The shared attribute is only available on Microsoft Windows.
  • TA的每日心情
    奋斗
    2016-10-11 09:20
  • 签到天数: 271 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2011-5-10 01:37:42 | 显示全部楼层
    调用ps?查看当前是否有2个自己的进程,如果有,则提示已经启动?

    该用户从未签到

    发表于 2011-5-10 04:35:03 | 显示全部楼层
    跨平台的解决方案:
    一、简单点的,使用unique, 见:Unique 1.1.6 Reference Manua Unique 3.0.0 Reference Manual
    二、复杂点的,使用dbus。

    该用户从未签到

    发表于 2011-9-25 09:40:22 | 显示全部楼层
    正好最近写的程序中也要求单实例运行,代码贴给你看看吧。
    1. ......
    2. #ifdef G_OS_WIN32
    3. #include <windows.h>
    4. #include <tlhelp32.h>
    5. #endif
    6. gboolean    helloworld_is_running                  (void)
    7. {
    8. #ifdef G_OS_UNIX
    9.     const gchar* uconfig_path;
    10.     gchar *path;
    11.     FILE *pf;
    12.     gchar buffer[1024];
    13.     pid_t pid = -1;
    14.     uconfig_path = helloworld_dirs_get_user_config_dir();
    15.     path = g_build_filename (uconfig_path, "helloworld.pid", NULL);
    16.     if (!g_file_test(path, G_FILE_TEST_EXISTS))
    17.     {
    18.         g_free(path);
    19.         return FALSE;
    20.     }
    21.     pf = g_fopen (path, "r");
    22.     if (pf == NULL) {
    23.         g_free(path);
    24.         return FALSE;
    25.     }
    26.     g_free(path);
    27.     if (fgets (buffer, sizeof (buffer), pf) == NULL)
    28.     {
    29.         fclose(pf);
    30.         return FALSE;
    31.     }
    32.     fclose(pf);
    33.     pid = atoi(buffer);
    34.     if (pid == -1 || kill (pid, 0) != 0)
    35.     {
    36.         return FALSE;
    37.     }
    38.     return TRUE;
    39. #endif
    40. #ifdef G_OS_WIN32
    41.     BOOL bFlag;
    42.     gint cnt = -1;
    43.     HANDLE hSnapShot = NULL;
    44.     PROCESSENTRY32 procentry;
    45.     // Get a handle to a Toolhelp snapshot of all processes.
    46.     hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    47.     if (hSnapShot == INVALID_HANDLE_VALUE)
    48.     {
    49.         return NULL;
    50.     }
    51.     // Get the first process' information.
    52.     procentry.dwSize = sizeof(PROCESSENTRY32);
    53.     bFlag = Process32First(hSnapShot, &procentry);
    54.     // While there are processes, keep looping.
    55.     while (bFlag) {
    56.         // Did we just bump into an NTVDM?
    57.         if (_stricmp(procentry.szExeFile, g_get_prgname()) == 0) {
    58.             cnt += 1;
    59.         }
    60.         procentry.dwSize = sizeof(PROCESSENTRY32);
    61.         bFlag = Process32Next(hSnapShot, &procentry);
    62.     }
    63.     if (cnt < 1)
    64.         return FALSE;
    65.     else
    66.         return TRUE;
    67. #endif
    68. }
    复制代码
    *滑块验证:
    您需要登录后才可以回帖 登录 | 马上加入

    本版积分规则

    申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

    我要啦免费统计

    GMT+8, 2024-5-14 04:59 , Processed in 0.551460 second(s), 7 queries , Redis On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表