?
Solved

Programmatically find the computer SID with C# and CPP

Posted on 2009-04-01
2
Medium Priority
?
2,871 Views
Last Modified: 2012-05-06
In need 2 routines, one in cpp and the other in C#. The code shows the cpp code to get the SID for the computer. I think this is correct, I have been using it for awhile. Then I need to be able to do the same thing in C#. Neither program will have administrative rights.

When I run these 2 code snipits on a computer on a domain I get the domain SID for the computer. I can verify this with ADSIViewer. Same results for both! Good, half way there.

If I then move the code to a machine that is not on a domain the CPP code will return a SID. But the .net C# code always throws no matter what computer name I give it.

This can't be that hard. So if someone has some ideas....


Scott
//
//	Get the size of the buffers
//
  cbSid        = 0;
  cbDomainName = 0;
  Sid          = NULL;
  LookupAccountName( NULL, szAccount, Sid, &cbSid, pszDomainName, &cbDomainName,  &eUse );
//
//	Now get the actual SID
//	
  Sid           = (PSID)  malloc(cbSid);
   pszDomainName = (LPSTR) malloc(cbDomainName);
 
   bRC = LookupAccountName( NULL, szAccount, Sid, &cbSid, pszDomainName   , &cbDomainName, &eUse ); 
  if( bRC )
  {
    strcpy_s( m_szDomainName, sizeof(m_szDomainName), pszDomainName );   	
    bRC = ConvertSidToStringSid( Sid, &pszSid ); 
    if( bRC )
   {
     sprintf_s( szWork, sizeof(szWork), "       FQDN : %s\\%s   - SID_NAME_USE: %li",     m_szDomainName,  m_szComputerName, eUse);
  printf( "%s\r\n", szWork ); 			
  strcpy_s( m_szMachineSID, sizeof(m_szMachineSID), pszSid );
  printf( "  MachineSID: %s\r\n", m_szMachineSID );    			
  if (pszSid)	LocalFree( pszSid );
 }
 }
 
==================================  C#  ===============================
	String sSid;
	SecurityIdentifier sid, sid2;
	NTAccount nta, acct;
	try
	{
		nta = new NTAccount( "SNORBERG-T5400$" );	
	
		sid = (SecurityIdentifier) nta.Translate( typeof(SecurityIdentifier));
		sSid = m_Config.MachineSID;
		sid2 = new SecurityIdentifier( sSid + "-513");
	
		acct = (NTAccount)sid.Translate(typeof(NTAccount));	
		acct = (NTAccount)sid2.Translate(typeof(NTAccount));
	}
	catch( Exception ex )
	{
		MessageBox.Show( ex.Message );
	}

Open in new window

0
Comment
Question by:snorberg
[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
2 Comments
 

Accepted Solution

by:
snorberg earned 0 total points
ID: 24049461
It seems that the only way this can be done from .net code is by using the win32 api commands through the runtime.interopServices.
http://www.pinvoke.net/default.aspx/advapi32.LookupAccountName
The pinvoke site has a reasonable example.
 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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