http://keicode.com/windows/win21.php
http://codezine.jp/article/detail/434
SID(S-1-5-21-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxx-xxxx)を取得しようと調べて上記のサイトを見ました。
C++で書かれているみたいなのですがCで取得することはできないのでしょうか?
C言語で簡単に取得する方法があったら教えてください。
SID取得
Re:SID取得
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
static BOOL ConvertNameToSid(LPTSTR lpszComputer,LPTSTR lpszName,PSID *ppsid)
{
TCHAR szDomain[256];
DWORD dwSidLen = 0;
DWORD dwDomainLen = sizeof(szDomain);
SID_NAME_USE snu;
LookupAccountName(lpszComputer,lpszName,NULL,&dwSidLen,szDomain,&dwDomainLen,&snu);
*ppsid = (PSID)HeapAlloc(GetProcessHeap(),0,dwSidLen);
return LookupAccountName(lpszComputer,lpszName,*ppsid,&dwSidLen,szDomain,&dwDomainLen,&snu);
}
int main(void)
{
DWORD dwBufferSize;
TCHAR szComputer[256];
PSID pSidComputer = NULL;
PSID pSidComputerNew = NULL;
dwBufferSize = sizeof(szComputer);
GetComputerName(szComputer, &dwBufferSize);
ConvertNameToSid(szComputer,szComputer,&pSidComputer);
pSidComputerNew = pSidComputer;
HeapFree(GetProcessHeap(),0,pSidComputer);
pSidComputer = pSidComputerNew;
printf("%s\n",szComputer);
printf("%d",pSidComputer);
getch();
return 0;
}
こんな感じにしたのですが
実行結果が私の期待してた
S-1-5-21-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxx-xxxx
このようになりませんでした・・・
最初は
printf("%d",pSidComputer);
これを
printf("%s",pSidComputer);
と文字列表示していたのですが
なんかおかしかったので整数にしました。
#include <stdlib.h>
#include <windows.h>
static BOOL ConvertNameToSid(LPTSTR lpszComputer,LPTSTR lpszName,PSID *ppsid)
{
TCHAR szDomain[256];
DWORD dwSidLen = 0;
DWORD dwDomainLen = sizeof(szDomain);
SID_NAME_USE snu;
LookupAccountName(lpszComputer,lpszName,NULL,&dwSidLen,szDomain,&dwDomainLen,&snu);
*ppsid = (PSID)HeapAlloc(GetProcessHeap(),0,dwSidLen);
return LookupAccountName(lpszComputer,lpszName,*ppsid,&dwSidLen,szDomain,&dwDomainLen,&snu);
}
int main(void)
{
DWORD dwBufferSize;
TCHAR szComputer[256];
PSID pSidComputer = NULL;
PSID pSidComputerNew = NULL;
dwBufferSize = sizeof(szComputer);
GetComputerName(szComputer, &dwBufferSize);
ConvertNameToSid(szComputer,szComputer,&pSidComputer);
pSidComputerNew = pSidComputer;
HeapFree(GetProcessHeap(),0,pSidComputer);
pSidComputer = pSidComputerNew;
printf("%s\n",szComputer);
printf("%d",pSidComputer);
getch();
return 0;
}
こんな感じにしたのですが
実行結果が私の期待してた
S-1-5-21-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxx-xxxx
このようになりませんでした・・・
最初は
printf("%d",pSidComputer);
これを
printf("%s",pSidComputer);
と文字列表示していたのですが
なんかおかしかったので整数にしました。
Re:SID取得
pSidComputerはPSID型です。
APIで扱いやすい型であって,表示用のものではありません。
S-1-5-形式にするには,ConvertSidToStringSid APIを使います。
MSDN: ConvertSidToStringSid Function (Windows)
http://msdn.microsoft.com/en-us/library/aa376399.aspx
APIで扱いやすい型であって,表示用のものではありません。
S-1-5-形式にするには,ConvertSidToStringSid APIを使います。
MSDN: ConvertSidToStringSid Function (Windows)
http://msdn.microsoft.com/en-us/library/aa376399.aspx
Re:SID取得
#include <Sddl.h>
を追加して
LPTSTR string;
ConvertSidToStringSid(pSidComputer, &string);
ですね
それと
pSidComputerを開放したらpSidComputerNewの内容も保障されません
内容は変化してないでしょうがやってはだめです
pSidComputer が不要になった時点で開放しましょう
を追加して
LPTSTR string;
ConvertSidToStringSid(pSidComputer, &string);
ですね
それと
pSidComputerNew = pSidComputer; HeapFree(GetProcessHeap(),0,pSidComputer); pSidComputer = pSidComputerNew;これはだめですよ
pSidComputerを開放したらpSidComputerNewの内容も保障されません
内容は変化してないでしょうがやってはだめです
pSidComputer が不要になった時点で開放しましょう
Re:SID取得
RIDはSIDのSubAuthorityの末尾ですから,
GetSidSubAuthorityCount APIとGetSidSubAuthority APIを使えば取得できます。
MSDN: GetSidSubAuthorityCount Function (Windows)
http://msdn.microsoft.com/en-us/library/aa446658.aspx
MSDN: GetSidSubAuthority Function (Windows)
http://msdn.microsoft.com/en-us/library/aa446657.aspx
GetSidSubAuthorityCount APIとGetSidSubAuthority APIを使えば取得できます。
MSDN: GetSidSubAuthorityCount Function (Windows)
http://msdn.microsoft.com/en-us/library/aa446658.aspx
MSDN: GetSidSubAuthority Function (Windows)
http://msdn.microsoft.com/en-us/library/aa446657.aspx
Re:SID取得
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sddl.h>
static BOOL ConvertNameToSid(LPTSTR lpszComputer,LPTSTR lpszName,PSID *ppsid)
{
TCHAR szDomain[256];
DWORD dwSidLen = 0;
DWORD dwDomainLen = sizeof(szDomain);
SID_NAME_USE snu;
LookupAccountName(lpszComputer,lpszName,NULL,&dwSidLen,szDomain,&dwDomainLen,&snu);
*ppsid = (PSID)HeapAlloc(GetProcessHeap(),0,dwSidLen);
return LookupAccountName(lpszComputer,lpszName,*ppsid,&dwSidLen,szDomain,&dwDomainLen,&snu);
}
int main(void)
{
//各種宣言//
DWORD dwBufferSize;
TCHAR szComputer[256];
PSID pSidComputer = NULL;
PSID pSidComputerNew = NULL;
LPTSTR lpszSid;
DWORD nSubAuthority;
//メイン処理//
dwBufferSize = sizeof(szComputer);
GetComputerName(szComputer, &dwBufferSize);
ConvertNameToSid(szComputer,szComputer,&pSidComputer);
ConvertSidToStringSid(pSidComputer, &lpszSid);
GetSidSubAuthorityCount(pSidComputer);
GetSidSubAuthority(pSidComputer,nSubAuthority);
//出力//
printf("Domain of Owner : %s\n",szComputer);
printf("SID Adr : %d\n",pSidComputer);
printf("SID Value : %s\n",lpszSid);
printf("RID Value : %d\n",nSubAuthority);
getch();
return 0;
}
こんな感じにしてみました。
やはりおかしな整数が入ってしまいます。
というか、コンパイル時に
warning C4700: 初期化されていないローカル変数 'nSubAuthority' が使用されます
とエラーが出てしまいます。
#include <stdlib.h>
#include <windows.h>
#include <sddl.h>
static BOOL ConvertNameToSid(LPTSTR lpszComputer,LPTSTR lpszName,PSID *ppsid)
{
TCHAR szDomain[256];
DWORD dwSidLen = 0;
DWORD dwDomainLen = sizeof(szDomain);
SID_NAME_USE snu;
LookupAccountName(lpszComputer,lpszName,NULL,&dwSidLen,szDomain,&dwDomainLen,&snu);
*ppsid = (PSID)HeapAlloc(GetProcessHeap(),0,dwSidLen);
return LookupAccountName(lpszComputer,lpszName,*ppsid,&dwSidLen,szDomain,&dwDomainLen,&snu);
}
int main(void)
{
//各種宣言//
DWORD dwBufferSize;
TCHAR szComputer[256];
PSID pSidComputer = NULL;
PSID pSidComputerNew = NULL;
LPTSTR lpszSid;
DWORD nSubAuthority;
//メイン処理//
dwBufferSize = sizeof(szComputer);
GetComputerName(szComputer, &dwBufferSize);
ConvertNameToSid(szComputer,szComputer,&pSidComputer);
ConvertSidToStringSid(pSidComputer, &lpszSid);
GetSidSubAuthorityCount(pSidComputer);
GetSidSubAuthority(pSidComputer,nSubAuthority);
//出力//
printf("Domain of Owner : %s\n",szComputer);
printf("SID Adr : %d\n",pSidComputer);
printf("SID Value : %s\n",lpszSid);
printf("RID Value : %d\n",nSubAuthority);
getch();
return 0;
}
こんな感じにしてみました。
やはりおかしな整数が入ってしまいます。
というか、コンパイル時に
warning C4700: 初期化されていないローカル変数 'nSubAuthority' が使用されます
とエラーが出てしまいます。
Re:SID取得
なぜ,
> GetSidSubAuthorityCount(pSidComputer);
> GetSidSubAuthority(pSidComputer,nSubAuthority);
や
> nSubAuthority = GetSidSubAuthorityCount(pSidComputer);
> GetSidSubAuthority(pSidComputer,nSubAuthority - 1);
でRIDが取得できると考えたのですか。
また,取得したRIDはどこにあると思ったのですか。
Windows APIやSidといった環境依存の話の前に,Cの知識が根本的に不足しているように思えますが。
> GetSidSubAuthorityCount(pSidComputer);
> GetSidSubAuthority(pSidComputer,nSubAuthority);
や
> nSubAuthority = GetSidSubAuthorityCount(pSidComputer);
> GetSidSubAuthority(pSidComputer,nSubAuthority - 1);
でRIDが取得できると考えたのですか。
また,取得したRIDはどこにあると思ったのですか。
Windows APIやSidといった環境依存の話の前に,Cの知識が根本的に不足しているように思えますが。