- PR -

C#で名前付きパイプをセキュリティ記述子を使って使用する際に、、、

1
投稿者投稿内容
まつとうや
常連さん
会議室デビュー日: 2004/09/04
投稿数: 42
投稿日時: 2004-11-04 12:07
C#で名前付きパイプをセキュリティ記述子を使って
アクセスフリーな名前付きパイプサーバを構築したいのですが、なかなか上手く動作しません。
C#は標準で名前付きパイプをサポートしてないのでWIN32APIからのインポートなのですが、ポインタの替わりにRefを使うのは問題があるのでしょうか。

// 外部実装されていることを明示
[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode,
uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize,
uint nDefaultTimeOut, ref SECURITY_ATTRIBUTES lpSecurityAttributes);

// 外部実装されていることを明示
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool ConnectNamedPipe(IntPtr hNamedPipe,
IntPtr lpOverlapped);

// 外部実装されていることを明示
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle; // BOOL
}


// 外部実装されていることを明示
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_DESCRIPTOR
{
public byte revision;
public byte size;
public short control;
public IntPtr owner;
public IntPtr group;
public IntPtr sacl;
public IntPtr dacl;
}

// 外部実装されていることを明示
[DllImport("advapi32.dll", SetLastError=true)]
static extern bool InitializeSecurityDescriptor(ref SECURITY_DESCRIPTOR pSecurityDescriptor, uint dwRevision);
// 外部実装されていることを明示
[DllImport("advapi32.dll", SetLastError=true)]
static extern bool SetSecurityDescriptorDacl(ref SECURITY_DESCRIPTOR pSecurityDescriptor, int bDaclPresent, IntPtr pDacl, int bDaclDefaulted);

public static System.IntPtr ConnectNamedPipe(string strPipeName)
{
System.IntPtr hNmPipe =IntPtr.Zero; // 名前付きパイプハンドル
unsafe
{
//セキュリティ記述子作成
SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR();
bool bTemp = InitializeSecurityDescriptor(ref sd, 1);
bool bTemp2 = SetSecurityDescriptorDacl(ref sd, 1, IntPtr.Zero, 0);
GCHandle sdHandle = GCHandle.Alloc(sd, GCHandleType.Pinned);
try
{
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.nLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = sdHandle.AddrOfPinnedObject();
sa.bInheritHandle = 0;

// 名前付きパイプのインスタンスの作成を行う。
hNmPipe = CreateNamedPipe(strPipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFSIZE,
BUFSIZE,
PIPE_TIMEOUT,
ref sa);
}
finally
{
sdHandle.Free();
}
}
if (hNmPipe == (System.IntPtr)(int)-1)
{
return IntPtr.Zero;
}
まつとうや
常連さん
会議室デビュー日: 2004/09/04
投稿数: 42
投稿日時: 2004-11-04 15:14
自己レスです。

クライアントからサーバへの接続の指定をIPアドレスからマシン名に変更したら上手くいきました。
Windowsネットワークでアプリを作成する際の仕様なんでしょうか。
毎度お騒がせしました。
1

スキルアップ/キャリアアップ(JOB@IT)