|
Hi Wes, nice to meet you.
I am not sure I understand your question (but then I am almost a complete newbie at c#).
Do you where I am creating or invoking the the instance? Sorry if its a dumb question.
If I may add, (as I can be fairly idiotic sometimes) the saying goes "Never argue with an idiot. They will drag you down to their level and beat you on experience"
|
|
|
|
|
CCodeNewbie wrote: where I am creating
Right. I do not see anywhere in your code block where you instantiate your class (Uinfo ui = new Uinfo() ) Such as:
public partial class Service : ServiceBase
{
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
Uinfo ui = new Uinfo();
in.Parameters.Add("@UAuthType", SqlDbType.NVarChar,40)
.Value = ui.AuthType;
in.Parameters.Add("@UAuth", SqlDbType.NVarChar, 40)
.Value = IsAuthd;
Or am I missing something? Of course this method relies on modifying Uinfo to use properties.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
I had tried exactly that (well, close enough I had used Uinfo userinfo) but when I do
in.Parameters.Add("@UAuthType", SqlDbType.NVarChar,40).Value = I only get the options of
DuplicateToken
Equals
ReferenceEquals
TOKEN_DUPLICATE
TOKEN_IMPERSONATE
TOKEN_QUERY
???
|
|
|
|
|
I see. Well the problem is that in your UsInfo method, you are assigning the results to local variables. You need to make them properties or fields so that you can retrieve them externally.
|
|
|
|
|
Whoosh, right over my head.
I think know what you mean though thus I had tried declaring public const strings and contemplating get;set;.
What would you suggest?
|
|
|
|
|
CCodeNewbie wrote: What would you suggest?
First thing is study up on scope.
Then in your Uinfo class use properties. Like:
public string AuthType {get;set;}
Then assign it inside your Uinfo class
AuthType = "some string"
To use it for your database then you need to instantiate your class and then you can use the property
string dbparameter = ui.AuthType
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
CCodeNewbie wrote: What would you suggest?
Pretty much what Wes has to say. I'm not sure you need to create properties for it - as you aren't manipulating anything there, just leaving it as a field would be enough.
|
|
|
|
|
Hi Pete,
Please pardon my ignorance/incompetence but I'm still not getting it. If I follow suggestions and use
public string LogonUser { get; set;}
I get the compile errors
'Uinfo.LogonUser.get' must declare a body because it is not marked abstract or extern
'Uinfo.LogonUser.set' must declare a body because it is not marked abstract or extern
OK, so I go
public string LogonUser { get { return LogonUser; } set { LogonUser = value; } }
and get 'An unhandled exception of type 'System.StackOverflowException' occurred in whatever.exe'. I think this may be because LogonUser is Null.
If I do this
public string LogonUser;
then in Main() I get
"from main" with no value for ui.LogonUser.
I am probably missing something really simple but can't seem to grasp what it is.
Code follows:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Text;
namespace Something
{
public class whatever
{
public static void Main()
{
UInfo ui = new UInfo();
Console.WriteLine("from main" + ui.LogonUser);
Console.ReadLine();
}
}
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 string LogonUser;
public static void Uinfo()
{
IntPtr hToken = IntPtr.Zero;
IntPtr dupeTokenHandle = IntPtr.Zero;
int p = Process.GetProcessesByName("explorer")[0].Id;
Process r = Process.GetProcessById(p);
if (OpenProcessToken(r.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 LogonUser = WindowsIdentity.GetCurrent().Name;
}
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;
}
}
Could you tell me what I missed in the tutorials please?
|
|
|
|
|
You seem to be missing a lot of code here. I would expect to see code to actually write to the database. Merely setting parameters doesn't do anything. I also don't see any use of your UInfo class in there. Could you post the missing bits please? Without them, we can't really offer more help.
|
|
|
|
|
Hi Pete,
Good to "see" you again. The sql connection code goes
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
[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;
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...
|
|
|
|
|
Sorry, got to go to bed . Been up 18 hrs now -eyes tired, brain tired. Look forward to talking more tomorrow.
Thanks for you input so far...
|
|
|
|
|
When you want to return multiple values, create a data class, with either properties or fields in it. I tend to just make them fields because properties don't gain you anything in this case (the purpose of the class is to expose all its data anyway). In this case you want to put all these things
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());
... in a data class (possibly UInfo itself) as public fields.
|
|
|
|
|
BobJanova wrote: I tend to just make them fields because properties don't gain you anything in
this case
Personally, I never expose fields, only properties. In many cases there is no gain, but it provides the flexibility to perform validation or make read only etc in the future. There is very little work required to create the properties and is much more maintainable IMHO.
|
|
|
|
|
I don't see why you need to make them properties if you might upgrade them in the future. If you need to make them readonly, for instance, it's a simple matter to convert them at that point.
|
|
|
|
|
The reason I do it is to make a clear distinction between data classes and 'active' classes. Validation is something which is generally part of logic and shouldn't be done by a pure data class. If I have a class that exposes fields, it's the only thing (apart from a constructor or some non-state-changing lookup functions that are trivial combinations of the fields) that it will expose.
Maintainability for a field is no worse than an auto-property, as they're source code equivalent in usage.
|
|
|
|
|
If I create a stock ActivityDesigner project, don't change anything, I can open the xaml file and see the designer.
If I create a stock ActiveLibrary project and double click on the Activity1.xaml file, I get: Index was out of range. ???
Same problem if I create a WCF workflow service and try double clicking on the xamlx file. Am I missing some component or something?
Never worked with WF before, just messing around with it.
|
|
|
|
|
Wouldn't this be better asked in the WCF and WF[^] forum?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
How would I go about getting a list of namespaces and/or assemblies referenced in a solution?
Everything makes sense in someone's mind
|
|
|
|
|
You'd normally try to do this through a Visual Studio extension. Get a reference to the DTE and that gives you access to all sorts of handy functionality. Alternatively, you could try the Object Browser if you just want an easy way to view this info.
|
|
|
|
|
This might work:
typeof(SomeClassName).Assembly.GetReferencedAssemblies
|
|
|
|
|
Guys,
Am trying to access the data from SQL SERVER 2008 via C#. I am getting an Exception. I am sure that I have problem with the query. please check this and reply...
The Code is:
private SqlDataReader GetInContainers(DateTime tFromDate,DateTime tToDate)
{
SqlConnection mycon = new SqlConnection(@"server=XYZ ; database = ABC; Trusted_Connection=yes");
mycon.Open();
string sql="Select CntNo, iso_code, InDateTime from gatein_containers where (InDateTime between #" + tFromDate.ToString("MM/dd/yyyy HH:mm:ss") + "# and #" + tToDate.ToString("MM/dd/yyyy HH:mm:ss") + "#) " +
" and (SHIPPING_LINE_CODE='1' or SHIPPING_LINE_CODE='MSL') order by InDateTime";
MessageBox.Show("Going to Execute");
SqlCommand myCommand = new SqlCommand(sql, mycon);
myReader = myCommand.ExecuteReader();
mycon.Close();
mycon.Dispose();
MessageBox.Show("Executed");
return myReader;
}
I am able to get the result when I use simple querty like:
sql="select * from daily;
|
|
|
|
|
A few things to note - you should use parameters for your queries, rather than using the text directly - you leave yourself wide open to SQL injection attacks here. When you say you get an exception, you should really tell us what the exception is, we can't read your mind. It's generally not a good idea to format your dates to anything other than the ISO standard, internally it will be stored as ISO - so you'll get a problem when you attempt to do this. AFAIK, SQL Server doesn't use # to delimit dates in queries, that's an Access format.
|
|
|
|
|
The # is the date delimiter in MS Access and SQL Server does not recognize that. Use the single quote to delimit dates. The default date format is YYYY/MM/DD. You can specify date in this format directly or use the convert function if you wish to specify date in other formats.
|
|
|
|
|
hi everyone, i've a form with a datagridview containing Name, and email address field. I've added another button column to this grid and on the click event of this button I'd like to concantenate the mailto: process and the email address field. Something like this:
if (this.dgvPresenters.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
{
Process.Start("mailto + '"(this.dgvPresenters[e.ColumnIndex,e.RowIndex].Value.ToString())"'+"); }
the email field in the DB is saved as name@company.com - hope this makes sense.
Thank you
|
|
|
|
|
Hi,
To start writing mail, the correct instruction is:
Process.Start("mailto:name@company.com");
then you must change you code as:
Process.Start("mailto:" + this.dgvPresenters[e.ColumnIndex,e.RowIndex].Value.ToString());
|
|
|
|
|