[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problem whit CopySID

Posted on 2008-06-17
3
Medium Priority
?
374 Views
Last Modified: 2013-12-03
Hi guys i have a problem (i think little), the CopySID fuction return a good SID structure but in the main function the check give a totaly different textual SID! Why? :/
BOOL GetProcessOwnerSid(DWORD dwProcessId, LPTSTR lpszOwnerSid, PSID ptagOwnerSid) {
	HANDLE hProcess;
	HANDLE hProcessToken;
	DWORD dwTokenInfoLength = 0;
	DWORD dwSidLength;
	LPTSTR lpszOwnerSidHeap;
	PTOKEN_USER ptagTokenUser;
	SID_NAME_USE eSidNameUse;
	if ((lpszOwnerSid) && (ptagOwnerSid)) {
		hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
		if (!hProcess) {
			return FALSE;
		}
		if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken)) {
			return FALSE;
		}
		GetTokenInformation(hProcessToken, TokenUser, ptagTokenUser, 
			                dwTokenInfoLength, &dwTokenInfoLength);
		ptagTokenUser = (PTOKEN_USER)GlobalAlloc(GPTR, dwTokenInfoLength);
		if (!ptagTokenUser) {
			return FALSE;
		}
		if (!GetTokenInformation(hProcessToken, TokenUser, ptagTokenUser, 
			                     dwTokenInfoLength, &dwTokenInfoLength)) {
				return FALSE;
		}
		if (IsValidSid(ptagTokenUser->User.Sid)) {
			if (lpszOwnerSid != NULL) {
				if (!ConvertSidToStringSid(ptagTokenUser->User.Sid, &lpszOwnerSidHeap)) {
					return FALSE;
				}
				lstrcpy(lpszOwnerSid, lpszOwnerSidHeap);
				LocalFree((HLOCAL)lpszOwnerSidHeap);
			}
			if (ptagOwnerSid != NULL) {
				dwSidLength = GetLengthSid(ptagTokenUser->User.Sid);
				ptagOwnerSid = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSidLength);
				if (!ptagOwnerSid) {
					return FALSE;
				}
				if(!CopySid(dwSidLength, ptagOwnerSid, ptagTokenUser->User.Sid)) {
                   return FALSE;
				}
				if (!IsValidSid(ptagOwnerSid)) {
				    return FALSE;
				}
			}
		} else {
			return FALSE;
		}
	}
	GlobalFree((HGLOBAL)ptagTokenUser);
	return TRUE;
}
 
int _tmain(int argc, LPCTSTR *argv[]) {
 
	TCHAR szSidName[256];
	PSID ptagSid;
    
 
    GetProcessOwnerSid(932, szSidName, ptagSid);
    LPTSTR lpszSidName = szSidName;
	ZeroMemory(&szSidName, lstrlen(szSidName));
	ConvertSidToStringSid(ptagSid, &lpszSidName);
	
	HeapFree(GetProcessHeap(), NULL, ptagSid);
	Pause();
	ExitProcess(TRUE);
}

Open in new window

0
Comment
Question by:0x7F
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
0x7F earned 0 total points
ID: 21832810
I HAVE RESOLVED BY MY SELF! I HOPE THIS HELP SOMEONE ;)
BOOL GetProcessOwnerSid(DWORD dwProcessId, LPTSTR lpszOwnerSid, PSID *ptagOwnerSid) {
	HANDLE hProcess;
	HANDLE hProcessToken;
	DWORD dwTokenInfoLength = 0;
	DWORD dwSidLength;
	LPTSTR lpszOwnerSidHeap;
	PTOKEN_USER ptagTokenUser;
	SID_NAME_USE eSidNameUse;
	if ((lpszOwnerSid) && (ptagOwnerSid)) {
		hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
		if (!hProcess) {
			return FALSE;
		}
		if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken)) {
			return FALSE;
		}
		GetTokenInformation(hProcessToken, TokenUser, ptagTokenUser, 
			dwTokenInfoLength, &dwTokenInfoLength);
		ptagTokenUser = (PTOKEN_USER)GlobalAlloc(GPTR, dwTokenInfoLength);
		if (!ptagTokenUser) {
			return FALSE;
		}
		if (!GetTokenInformation(hProcessToken, TokenUser, ptagTokenUser, 
			dwTokenInfoLength, &dwTokenInfoLength)) {
				return FALSE;
		}
		if (IsValidSid(ptagTokenUser->User.Sid)) {
			if (lpszOwnerSid != NULL) {
				if (!ConvertSidToStringSid(ptagTokenUser->User.Sid, &lpszOwnerSidHeap)) {
					return FALSE;
				}
				lstrcpy(lpszOwnerSid, lpszOwnerSidHeap);
				LocalFree((HLOCAL)lpszOwnerSidHeap);
			}
			if (ptagOwnerSid != NULL) {
				dwSidLength = GetLengthSid(ptagTokenUser->User.Sid);
				*ptagOwnerSid = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSidLength);
				if (*ptagOwnerSid == NULL) {
					return FALSE;
				}
				if(!CopySid(dwSidLength, *ptagOwnerSid, ptagTokenUser->User.Sid)) {
                   HeapFree(GetProcessHeap(), NULL, *ptagOwnerSid);
				   return FALSE;
				}
				if (!IsValidSid(*ptagOwnerSid)) {
					return FALSE;
				}
			}
		} else {
			return FALSE;
		}
	}
	GlobalFree((HGLOBAL)ptagTokenUser);
	return TRUE;
}

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question