Click here to Skip to main content
15,900,818 members
Home / Discussions / C#
   

C#

 
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 
Hi Pete,

Good to "see" you again. The sql connection code goes
C#
using (SqlConnection UserInfo = new SqlConnection("User id = xxx; password = yyy; server=aaa\\bbb;" +
"database=mydb;connection timeout=30"))
{
SqlCommand in = new SqlCommand("INSERT INTO dbo.ccc(Tstamp, SysID, LogOnUser, DefUser," + "AltDefUser, UAuthType, UAuth, UGuest, USIDNo, USIDPlain, UToken)" +
"VALUES (@Tstamp, @SysID, @LogOnUser, @DefUser,"
+ "@AltDefUser, @UAuthType, @UAuth, @UGuest, @USIDNo, @USIDPlain, @UToken)", UserInfo);


I can assure you the the SQL part of it works 100% because a) if I use static values (e.g. AuthType = "a") then that values writes to the db and b) if I do
C#
[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 OnTimedEvent(object source, ElapsedEventArgs e)
    {
         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;
                      RegistryKey RegKeyDU = Registry.LocalMachine;
                      //using (sqlstuff as above)
                      in.Parameters.Add("@UAuthType", SqlDbType.NVarChar, 40).Value = AuthType;
                                    UserInfo.Open();
                                    inu.ExecuteNonQuery();
                                    UserInfo.Close();
                                }
                            }
                    finally
                    {
                    CloseHandle(hToken);
                    }
                }
                else
                {
                    string s = String.Format("OpenProcess Failed {0}, privilege not  
                    held", Marshal.GetLastWin32Error());
                    throw new Exception(s);
                }
            } 


Then the data writes properly, i.e. If I incorporate the method into the exising class everything works as it should...
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 
AnswerRe: Can't create Workflow projects? Pin
Richard MacCutchan29-Feb-12 7:14
mveRichard MacCutchan29-Feb-12 7:14 
QuestionGet Namespaces and/or Assemblies In A Solution Pin
Kevin Marois29-Feb-12 6:05
professionalKevin Marois29-Feb-12 6:05 
AnswerRe: Get Namespaces and/or Assemblies In A Solution Pin
Pete O'Hanlon29-Feb-12 6:14
mvePete O'Hanlon29-Feb-12 6:14 
AnswerRe: Get Namespaces and/or Assemblies In A Solution Pin
Ennis Ray Lynch, Jr.29-Feb-12 6:15
Ennis Ray Lynch, Jr.29-Feb-12 6:15 
QuestionCorrection needed in SQL Query Pin
Abdul Anoop29-Feb-12 5:45
Abdul Anoop29-Feb-12 5:45 
AnswerRe: Correction needed in SQL Query Pin
Pete O'Hanlon29-Feb-12 5:50
mvePete O'Hanlon29-Feb-12 5:50 
AnswerRe: Correction needed in SQL Query Pin
Shameel29-Feb-12 8:05
professionalShameel29-Feb-12 8:05 
QuestionEmail button in Datagridview Pin
pmcm29-Feb-12 5:38
pmcm29-Feb-12 5:38 
AnswerRe: Email button in Datagridview Pin
Montasser Ben Ouhida2-Mar-12 4:48
Montasser Ben Ouhida2-Mar-12 4:48 
GeneralRe: Email button in Datagridview Pin
pmcm4-Mar-12 22:23
pmcm4-Mar-12 22:23 
AnswerRe: Email button in Datagridview Pin
satalaj2-Mar-12 5:23
satalaj2-Mar-12 5:23 

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.