Click here to Skip to main content
15,891,828 members
Home / Discussions / C#
   

C#

 
QuestionExchange Server 2007 - Commands Pin
Sebastian T Xavier29-Feb-12 22:53
Sebastian T Xavier29-Feb-12 22:53 
AnswerRe: Exchange Server 2007 - Commands Pin
Dave Kreskowiak1-Mar-12 3:37
mveDave Kreskowiak1-Mar-12 3:37 
QuestionQuick Constructor Question Pin
Kevin Marois29-Feb-12 12:01
professionalKevin Marois29-Feb-12 12:01 
AnswerRe: Quick Constructor Question Pin
RobCroll29-Feb-12 13:36
RobCroll29-Feb-12 13:36 
GeneralRe: Quick Constructor Question Pin
J4amieC1-Mar-12 5:22
J4amieC1-Mar-12 5:22 
AnswerRe: Quick Constructor Question Pin
GParkings29-Feb-12 22:47
GParkings29-Feb-12 22:47 
AnswerRe: Quick Constructor Question Pin
BobJanova1-Mar-12 5:27
BobJanova1-Mar-12 5:27 
QuestionPassing values between classes Pin
CCodeNewbie29-Feb-12 10:33
CCodeNewbie29-Feb-12 10:33 
Hi,

I think I have become a little lost while trying to pass values from one class to another and, having researched reasons why my code isn't working, am now going in circles (I really need to start documenting attempted methods).

OK, so I have this code which gets some user details using impersonation (thanks to Luc for making me work harder finding an answer)

C#
public class UInfo
    {
    [DllImport("advapi32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute]
    static extern int OpenProcessToken(System.IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);

[DllImport("kernel32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute]
static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

public const int TOKEN_DUPLICATE = 2;
public const int TOKEN_QUERY = 0X00000008;
public const int TOKEN_IMPERSONATE = 0X00000004;

    public void UsInfo(string[] args)
    {
        IntPtr hToken = IntPtr.Zero;
        IntPtr dupeTokenHandle = IntPtr.Zero;
        Process proc = Process.GetProcessById(2568);
        if
        {
           OpenProcessToken(proc.Handle,TOKEN_QUERY|TOKEN_IMPERSONATE|TOKEN_DUPLICATE, ref hToken) != 0)
        {
        WindowsIdentity newId = new WindowsIdentity(hToken);

            try
            {
                const int SecurityImpersonation = 2;
                dupeTokenHandle = DupeToken(hToken,  
                SecurityImpersonation);
                if(IntPtr.Zero == dupeTokenHandle)
                {
                    string s = String.Format("Dup failed {0}, 
                    privilege not held",
                        Marshal.GetLastWin32Error());
                        throw new Exception(s);
                }

                WindowsImpersonationContext impersonatedUser =  
                newId.Impersonate();
                IntPtr accountToken =  
                WindowsIdentity.GetCurrent().Token;
                string Token = accountToken.ToString();
                string WinID = 
                WindowsIdentity.GetCurrent().Name;
                string IsAuthd =  
                newId.IsAuthenticated.ToString();
                string AuthType = newId.AuthenticationType;
                string IsGuest = newId.IsGuest.ToString();
                string SIDNo = newId.Owner.ToString();
                string SIDPlain = 
                newId.Owner.TranslatTAccount)).ToString());
           }
           finally
           {
           CloseHandle(hToken);
           }
        }
        else
        {
            string s = String.Format("OpenProcess Failed {0}, 
            privilege not held", Marshal.GetLastWin32Error());
            throw new Exception(s);
        }
    }
    static IntPtr DupeToken(IntPtr token, int Level)
    {
        IntPtr dupeTokenHandle = IntPtr.Zero;
        bool retVal = DuplicateToken(token, Level, ref 
        dupeTokenHandle);
        return dupeTokenHandle;
    }
}//from Willy Denoyette [MVP] http://bytes.com/topic/c-sharp/answers/463942-using-openprocesstoken


but I need to put the string values retrieved in to my service's code:-
C#
public partial class Service : ServiceBase
{
     public void OnTimedEvent(object source, ElapsedEventArgs e)
     {
      // do some sql connection stuff
      in.Parameters.Add("@UAuthType", SqlDbType.NVarChar,40)   
      .Value = AuthType;// I dont get Uinfo.AuthType as being an available value
      in.Parameters.Add("@UAuth", SqlDbType.NVarChar, 40)
      .Value = IsAuthd;
      // the code doesn't errror, there are just no values (not even null) written to the db.


I have tried using public const string AuthType = ""; but still nothing writes to the db.

Could someone please point me in the right direction on this? I have seen a lot of articles using this kind of thing from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/02d7555b-b600-4b0e-aa76-6b2d976e6c44/

C#
public class Customer
{
    private string _name;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
}


but surely there has to be a more concise way to return multiple values?

Thanks for your time.
AnswerRe: Passing values between classes Pin
Wes Aday29-Feb-12 10:54
professionalWes Aday29-Feb-12 10:54 
GeneralRe: Passing values between classes Pin
CCodeNewbie29-Feb-12 11:11
CCodeNewbie29-Feb-12 11:11 
GeneralRe: Passing values between classes Pin
Wes Aday29-Feb-12 11:24
professionalWes Aday29-Feb-12 11:24 
GeneralRe: Passing values between classes Pin
CCodeNewbie29-Feb-12 11:37
CCodeNewbie29-Feb-12 11:37 
GeneralRe: Passing values between classes Pin
Pete O'Hanlon29-Feb-12 11:45
mvePete O'Hanlon29-Feb-12 11:45 
GeneralRe: Passing values between classes Pin
CCodeNewbie29-Feb-12 11:56
CCodeNewbie29-Feb-12 11:56 
GeneralRe: Passing values between classes Pin
Wes Aday29-Feb-12 12:44
professionalWes Aday29-Feb-12 12:44 
GeneralRe: Passing values between classes Pin
Pete O'Hanlon29-Feb-12 22:14
mvePete O'Hanlon29-Feb-12 22:14 
GeneralRe: Passing values between classes Pin
CCodeNewbie5-Mar-12 6:07
CCodeNewbie5-Mar-12 6:07 
SuggestionRe: Passing values between classes Pin
Pete O'Hanlon29-Feb-12 11:28
mvePete O'Hanlon29-Feb-12 11:28 
GeneralRe: Passing values between classes Pin
CCodeNewbie29-Feb-12 11:53
CCodeNewbie29-Feb-12 11:53 
AnswerRe: Passing values between classes Pin
CCodeNewbie29-Feb-12 12:11
CCodeNewbie29-Feb-12 12:11 
AnswerRe: Passing values between classes Pin
BobJanova1-Mar-12 5:23
BobJanova1-Mar-12 5:23 
GeneralRe: Passing values between classes Pin
DaveyM691-Mar-12 9:47
professionalDaveyM691-Mar-12 9:47 
GeneralRe: Passing values between classes Pin
Pete O'Hanlon1-Mar-12 11:29
mvePete O'Hanlon1-Mar-12 11:29 
GeneralRe: Passing values between classes Pin
BobJanova1-Mar-12 23:31
BobJanova1-Mar-12 23:31 
QuestionCan't create Workflow projects? Pin
SledgeHammer0129-Feb-12 6:39
SledgeHammer0129-Feb-12 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.