質問するログイン新規登録

質問編集履歴

1

\+=解決しました

2016/09/23 11:28

投稿

IchigoTaruto
IchigoTaruto

スコア159

title CHANGED
File without changes
body CHANGED
@@ -82,4 +82,63 @@
82
82
  ---
83
83
  そもそも何故異なるログオンセッションで同じ名前のミューテックスを生成出来ないのでしょうか...
84
84
  その理由もお教えいただけると幸いです。
85
- よろしくお願いします。
85
+ よろしくお願いします。
86
+
87
+ 解決しました
88
+ ---
89
+ ご回答いただいた内容を元に以下の通り修正したところ、うまくいきました。
90
+ ありがとうございます。
91
+ ```C
92
+ #pragma comment(lib, "advapi32.lib")
93
+
94
+ #include <stdio.h>
95
+ #include <conio.h>
96
+ #include <stdlib.h>
97
+ #include <windows.h>
98
+ #include <winbase.h>
99
+
100
+ int main(int argc, char **argv)
101
+ {
102
+ char *name;
103
+ HANDLE hdl;
104
+ int ret;
105
+
106
+ if(argc != 2)
107
+ return;
108
+
109
+ name = argv[1];
110
+ printf("name == '%s'\n", name);
111
+
112
+ {
113
+ SECURITY_DESCRIPTOR sd;
114
+ SECURITY_ATTRIBUTES secAttribute;
115
+ InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
116
+ SetSecurityDescriptorDacl(&sd, TRUE, 0, FALSE);
117
+ secAttribute.nLength = sizeof(secAttribute);
118
+ secAttribute.lpSecurityDescriptor = &sd;
119
+ secAttribute.bInheritHandle = TRUE;
120
+ hdl = CreateMutexA(&secAttribute, FALSE, name);
121
+ }
122
+
123
+ printf("hdl == %d\n", (int)hdl);
124
+
125
+ if(hdl == NULL)
126
+ return;
127
+
128
+ // _getch();
129
+
130
+ printf("LOCKING...\n");
131
+ ret = WaitForSingleObject((HANDLE)hdl, INFINITE);
132
+
133
+ if(ret != WAIT_OBJECT_0)
134
+ return; // fatal
135
+
136
+ printf("LOCKED\n");
137
+
138
+ _getch();
139
+
140
+ ReleaseMutex(hdl);
141
+ CloseHandle(hdl);
142
+ printf("RELEASE & CLOSED\n");
143
+ }
144
+ ```