天天看點

如何利用C++ Builder擷取系統的sid資訊?

如何利用C++ Builder擷取系統的sid資訊?

    頭檔案代碼:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TButton *Button1;
    TEdit *Edit1;
    TLabel *Label1;
    void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
           

實作檔案代碼:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include "Unit1.h"
#define MAX_NAME 256

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void ConvertSid(PSID pSid, LPTSTR TextualSid)
{
	PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(pSid);
	DWORD sidLen = sprintf(TextualSid, TEXT("S-%lu-"), SID_REVISION);
	sidLen += sprintf(TextualSid + strlen(TextualSid), TEXT("%lu"), psia->Value[5]);
	
	int i = 0;
	int subAuthorities = *GetSidSubAuthorityCount(pSid);
	for (i = 0; i < subAuthorities; i++)
	{
		sidLen += sprintf(TextualSid + sidLen, TEXT("-%lu"), *GetSidSubAuthority(pSid, i));
	}
}

void GetLocalSid(LPTSTR szSid)
{
	char szUserName[MAX_NAME] = {0};
	DWORD nameSize = sizeof(szUserName) ;
	GetUserName(szUserName, &nameSize);

	char szUserSid[MAX_NAME] = {0};
	char szUserDomain[MAX_NAME] = {0};
	DWORD sidSize = sizeof(szUserSid);
	DWORD domainSize = sizeof(szUserDomain);
	SID_NAME_USE snu;
	
	LookupAccountName( NULL,
		               szUserName,
		               (PSID)szUserSid,
					   &sidSize,
					   szUserDomain,
					   &domainSize,
					   &snu
					 );
	
	ConvertSid(szUserSid, szSid);	
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char szSid[MAX_NAME] = {0};
	GetLocalSid(szSid);
    Edit1->Text = szSid;
    Edit1->SetFocus();
}
//---------------------------------------------------------------------------
           

繼續閱讀