|
I managed to do this by adding this class to my application:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace Gauge
{
public static class mySecurity
{
#region Constants
const UInt32 TOKEN_QUERY = 8;
const int INT_SIZE = 4;
#endregion
#region Enumerations
private enum TOKEN_ELEVATION_TYPE
{
TokenElevationTypeDefault = 1,
TokenElevationTypeFull,
TokenElevationTypeLimited
}
public enum TOKEN_INFO_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin,
TokenElevationType,
TokenLinkedToken,
TokenElevation,
TokenHasRestrictions,
TokenAccessInformation,
TokenVirtualizationAllowed,
TokenVirtualizationEnabled,
TokenIntegrityLevel,
TokenUIAccess,
TokenMandatoryPolicy,
TokenLogonSid,
MaxTokenInfoClass
// MaxTokenInfoClass should always be the last enum
}
#endregion
#region WIN API FUNCTIONS
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError=true)]
public static extern Boolean OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError=true)]
public static extern Boolean GetTokenInformation(IntPtr TokenHandle, TOKEN_INFO_CLASS TokenInformationClass,
IntPtr TokenInformation, int TokenInformationLength, out uint ReturnLength);
#endregion
#region Public Methods
///
/// Returns True when the current user is a member of the
/// Administrators group and is also running the process
/// elevated as an Administrator, otherwise returns false.
///
/// <returns>
/// <c>true if user is running the process elevated as an Administrator; otherwise, <c>false.
///
public static Boolean IsRunningAsAdmin(WindowsPrincipal pWindowsPrincipal)
{
return pWindowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
///
/// Checks whether the user can elevate to Administrator rights.
///
/// <returns><c>true if user can elevate to Administrator rights; <c>false otherwise.
public static Boolean CanElevateToAdmin(WindowsPrincipal pWindowsPrincipal)
{
Boolean returnVal = false;
// Determine if the current user is already running with Administrator rights.
Boolean isAdmin = IsRunningAsAdmin(pWindowsPrincipal);
// Return True if the user is an Administrator (user must have Administration rights).
if (isAdmin)
return true;
OperatingSystem os = Environment.OSVersion;
Version vs = os.Version;
// If Vista or Higher, check for split token.
if (vs.Major > 5)
{
try
{
IntPtr myToken;
TOKEN_ELEVATION_TYPE elevationType;
uint dwSize;
IntPtr pElevationType = Marshal.AllocHGlobal(INT_SIZE);
// Get a token reference for the user running this process.
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, out myToken);
// Get the elevation information for this token.
GetTokenInformation(myToken, TOKEN_INFO_CLASS.TokenElevationType, pElevationType,
INT_SIZE, out dwSize);
// Cast the result to ENUM_TYPE.
elevationType = (TOKEN_ELEVATION_TYPE)(Marshal.ReadInt32(pElevationType));
// Free allocated unmanaged memory.
Marshal.FreeHGlobal(pElevationType);
// Determine result of the elevation check.
// ==============================================
// TokenElevationTypeFull - User has a split token,
// and the process is running elevated
// TokenElevationTypeDefault - User is not using a split token
// ==============================================
returnVal = (elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited);
returnVal = returnVal || (elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull);
}
catch
{
returnVal = false;
}
}
else
{
// Prior to Vista, only check needed is if user is in Administrators group.
returnVal = isAdmin;
}
return returnVal;
}
///
/// Checks if user is running as a standared user, but can elevate to Administrator rights.
///
/// <returns><c>true if user can elevate with their own credentials; <c>false otherwise.
|
|
|
|
|
Hi everybody,
how can i use TAPI 2 into C#?
I made my project with TAPI 3, but the 3rd Version is buggy, the DISCONNECTED Event doesn't fire
each time if the call is over.
So i like to step to Version 2 of TAPI
Big thanks for help
|
|
|
|
|
Hi
i have the following code which insetrs a value into the database, however it says the value is not in the correct format.
any help?
thanks
Session["Qty_New"] = tb_Qty_New.Text.;
cmd.Parameters.Add(new SqlParameter("@qty_new", SqlDbType.Int));
cmd.Parameters["@qty_new"].Value = Convert.ToInt32(Session["Qty_New"]);
|
|
|
|
|
Jamie1985 wrote: wever it says the value is not in the correct format.
any help?
Yes, obviously ensure Session["Qty_New"] has data, and that that data can be converted to an int before converting it.
Check out int.TryParse rather than Convert.ToInt32
|
|
|
|
|
Two things - you don't actually need to convert the session to an int to assign it to the parameter. Also, you should check to see if Session["Qty_New"] is null or an empty string before using it.
|
|
|
|
|
Hi
are you sure i dont need to convert to int, i took it out and left it like this
string Qty_New
Qty_New = tb_Qty_New.Text;
cmd.Parameters.Add(new SqlParameter("@qty_new", SqlDbType.Int));
cmd.Parameters["@qty_new"].Value = Qty_New;
however this doesnt work
there is a value in Qty_New
cheers
|
|
|
|
|
Any leading/trailing space character in that?
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
no just a one digit number in the text box value
|
|
|
|
|
fixed it
the textbox value was set on page_load instead of the onlclick
thanks
|
|
|
|
|
Hi all,
I want to sending an XML string using webservice installed at server to the client using IP(internet IP) and then further want to save it in the from of XML file.
can anybody tell me how to do it???
Thanks in advance.
Ankur
|
|
|
|
|
can we hide focus from grid cell
amit
|
|
|
|
|
|
how do i redirect to anther page when i only know the path of the libery?
|
|
|
|
|
Your post doesn't make much sense. Redirect what?
wrote: when i only know the path of the libery?
Is not clear at all.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
how to deactivate grid cell if it is active.
amit
|
|
|
|
|
I want to know what is happening in these lines of code:
a)
public class cAppBarDisplay:AppBar
{
cMessageProcessing CMS;
public cAppBarDisplay(cMessageProcessing cMP)
{
CMS = cMP;
}
}
->cMessageProcessing CMS;
By this mean are we creating a refrence?
->and what the constructer is actually doing?
CMS & cMP both are refrence of same class
Thanks in advance,
plz describe in a bit detail.
|
|
|
|
|
humdumof wrote: ->cMessageProcessing CMS;
By this mean are we creating a refrence?
You created a field called CMS, which you can set to reference an instance of cMessageProcessing. This happens when you do CMS = cMP for example.
humdumof wrote: CMS = cMP;//exactly what is happening here
You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
|
|
|
|
|
AlwiNus wrote: You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
Not a refrence to the class itself?
By this what u mean?
also CMS=cMP;
Does it mean we can access all the methods of cMessageProcessing thru CMS?
& what is use of doing all this in constructor?
|
|
|
|
|
humdumof wrote: Not a refrence to the class itself?
By this what u mean?
When you use a class you create an instance of it. Kinda like if i want a bowl of cereal, i have to get myself a bowl and some cereal, and probably some milk. Then my bowl of cereal that i can eat, would be an instance of a bowl of cereal... if you follow me.
When you pass the instance of your class to the constructor your passing a reference (pointer) to it.
humdumof wrote: also CMS=cMP;
Does it mean we can access all the methods of cMessageProcessing thru CMS?
& what is use of doing all this in constructor?
Yes, you can access all the methods of the class through CMS.
The use of this, is that you can make use of the same instance of a class, in as many different classes as you like. So you can make 1 instance of your class, and pass it to 2 other classes for example. Then both classes will work on the same instance.
EDIT: As for why you should actually do this in the constructor: If the new class depends on having an instance of cMessageProcessing (ie it uses it) and it does not get passed a reference or create its own instance, then it will probably crash. By passing it to the constructor, you can guarantee that the new class will always have an instance of cMessageProcessing, it would be impossible to instatiate the class (ie myClass = new MyClass()) without passing the correct parameters.
My current favourite word is: Bauble!
-SK Genius
|
|
|
|
|
buddy, U have a typical class code with u.
Just look in the code its just a constructor with one parameter which is initiallising the value passed by the parameter to the local class variable.
|
|
|
|
|
I am writing a program using System.Timers.Timer to trigger the webcam to capture an image and saved in a file.
The image saving function is workable when it works standalone (by clicking a button). However, when I call the image saving function in the OnTimedEvent (event-triggering function) it fails. It will pop up the following error message:
"System.NullReferenceException: Object reference not set to an instance of an object...."
I lately found that the Data could not be retrieved from the clipboard when the clipboard is being used by another process...
Following is the Caputure function of the webCam device... (in Device.cs)
public Image Capture()<br />
{<br />
try{<br />
if (deviceHandle != 0)<br />
{<br />
SendMessage(deviceHandle, WM_CAP_EDIT_COPY, 0, (IntPtr)0);<br />
IDataObject ido = Clipboard.GetDataObject();<br />
if (ido.GetDataPresent(DataFormats.Bitmap))<br />
{<br />
return ((Bitmap)ido.GetData(DataFormats.Bitmap));<br />
}<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);<br />
}<br />
return null;<br />
<br />
}
Following is the timer triggering function...
private void OnTimedEvent(object source, ElapsedEventArgs e)<br />
{<br />
<br />
SavingOnePic(Directory.GetCurrentDirectory(), txtSavingName.Text.ToString());<br />
<br />
}
Following is the saving Function, it works when it is called from a button click event. however, does not work in the above OnTimedEvent function.
private void SavingOnePic(string savingPath, string fileName)<br />
{<br />
<br />
try<br />
{<br />
<br />
if (!Directory.Exists(@savingPath+@"\capturedImage")) <br />
{<br />
Directory.CreateDirectory(@savingPath + @"\capturedImage");<br />
fileIndex = 0;<br />
}else{<br />
string[] fileList = Directory.GetFiles(@savingPath + @"\capturedImage",<br />
fileName + "*.png");<br />
<br />
fileIndex = fileList.Length;<br />
}<br />
<br />
Image image = selectedDevice.Capture();<br />
<br />
image.Save(@savingPath + @"\capturedImage\" + fileName + <br />
"_000" + fileIndex.ToString() + ".png", ImageFormat.Png);<br />
<br />
fileIndex++;<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);<br />
}<br />
}
|
|
|
|
|
And the question is ...
SkyWalker
|
|
|
|
|
Sorry that I didn't mention it clearly.
I want to know why the SavingOnePic function in OnTimedEvent cannot run.
I found that the clipboard does not have any data if the SavingOnePic function in OnTimedEvent. But I don't know how to solve it
Thanks
|
|
|
|
|
You can try to use a System.Windows.Forms.Timer instead, which is tread-safe. If i remember it correctly the System.Timers.Timer isn't (not sure though).
Also, when you mention that you get an exception, it is more helpful if you post the full exception, including stack trace etc..
HTH
|
|
|
|
|
As you already said, there is no data of that type in your clipboard.
You cannot retrieve what does not exist
Therefore, you should avoid "calling" methods of non-existing methods:
IDataObject ido = Clipboard.GetDataObject();
if (ido != null)
{
}
else
{
return;
}
SkyWalker
|
|
|
|
|