0

これが仕様によるものかどうかはわかりませんが、Windows7で標準ユーザーまたはパワーユーザーとして新しいセマフォを作成することはできないようです。

SemaphoreSecurity semSec = new SemaphoreSecurity(); 

// have also tried "Power Users", "Everyone", etc. 
SemaphoreAccessRule rule = new SemaphoreAccessRule("Users", SemaphoreRights.FullControl, AccessControlType.Allow);   

semSec.AddAccessRule(rule);

bool createdNew = false;

// throws exception 
sem = new Semaphore(1, 1, SEMAPHORE_ID, out createdNew, semSec);  

return true; 

UnauthorizedAccessException「ポートへのアクセスが拒否されました」というメッセージが表示されます。

これは可能ですか?

4

2 に答える 2

0

そのトピックに関するMSDNドキュメントを見ると、解決策はセマフォ作成のセキュリティレベルを設定することであるように思われます。

与えられたリンクからのソースコードの抜粋は次のとおりです。

// The value of this variable is set by the semaphore 
// constructor. It is true if the named system semaphore was 
// created, and false if the named semaphore already existed. 
// 
bool semaphoreWasCreated;

// Create an access control list (ACL) that denies the 
// current user the right to enter or release the  
// semaphore, but allows the right to read and change 
// security information for the semaphore. 
// 
string user = Environment.UserDomainName + "\\" 
    + Environment.UserName;
SemaphoreSecurity semSec = new SemaphoreSecurity();

SemaphoreAccessRule rule = new SemaphoreAccessRule(
    user, 
    SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
    AccessControlType.Deny);
semSec.AddAccessRule(rule);

rule = new SemaphoreAccessRule(
    user, 
    SemaphoreRights.ReadPermissions | SemaphoreRights.ChangePermissions,
    AccessControlType.Allow);
semSec.AddAccessRule(rule);

// Create a Semaphore object that represents the system 
// semaphore named by the constant 'semaphoreName', with 
// maximum count three, initial count three, and the 
// specified security access. The Boolean value that  
// indicates creation of the underlying system object is 
// placed in semaphoreWasCreated. 
//
sem = new Semaphore(3, 3, semaphoreName, 
    out semaphoreWasCreated, semSec);

// If the named system semaphore was created, it can be 
// used by the current instance of this program, even  
// though the current user is denied access. The current 
// program enters the semaphore. Otherwise, exit the 
// program. 
//  
if (semaphoreWasCreated)
{
    Console.WriteLine("Created the semaphore.");
}
else
{
    Console.WriteLine("Unable to create the semaphore.");
    return;
}

お役に立てれば!

于 2013-01-25T22:01:58.267 に答える
0

さらに調査して試してみた結果、ようやくこれを解決しました。

重要なのは、セマフォ名の前に「Global」を付けることです。

    const string NAME = "Global\\MySemaphore"; 

ありがとう。

于 2013-01-26T01:30:23.447 に答える