|
Thanks ALOT! That's perfect and exactly what I need. Sniff, sniff.. getting emotional. Heh.
|
|
|
|
|
ok, now before i go mad, can someone help me out here. below is what i have to change privileges.
public static bool SetPrivilege(string privilege, bool enabled)
{
if (Environment.OSVersion.Version.Major >= 4)
{
IntPtr tknPtr = IntPtr.Zero;
Process pr = Process.GetCurrentProcess();
OpenProcessToken(pr.Handle, 0, out tknPtr);
TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
LUID luid = new LUID();
StringBuilder privString = new StringBuilder(privilege);
if (!LookupPrivilegeValue(null, privString, out luid ))
return false;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = enabled ? 0x00000002L : 0;
int sz = Marshal.SizeOf(typeof(long))*2;
sz += Marshal.SizeOf(typeof(int))*2;
return AdjustTokenPrivileges(tknPtr, false, tp, sz, null, 0);
}
else
return false;
}
[StructLayout(LayoutKind.Auto)]
private class TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES[] Privileges = new LUID_AND_ATTRIBUTES[1];
}
[StructLayout(LayoutKind.Sequential)]
private struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public long Attributes;
}
[StructLayout(LayoutKind.Sequential)]
private struct LUID
{
public int LowPart;
public long HighPart;
}
and the marshaller complains about param #5:
AdjustTokenPrivileges(tknPtr, false, tp, sz,null, 0);
saying about how it cannot be marshalled no because it has no layout information. btw i get the same error even when i dont use null, and use a TOKEN_PRIVILEGE instead.
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
Nnamdi Onyeyiri wrote:
[StructLayout(LayoutKind.Auto)]private class TOKEN_PRIVILEGES { public int PrivilegeCount; public LUID_AND_ATTRIBUTES[] Privileges = new LUID_AND_ATTRIBUTES[1];}
That MUST be Sequential as well.
BOOL AdjustTokenPrivileges(
HANDLE TokenHandle, // handle to token
BOOL DisableAllPrivileges, // disabling option
PTOKEN_PRIVILEGES NewState, // privilege information
DWORD BufferLength, // size of buffer
PTOKEN_PRIVILEGES PreviousState, // original state buffer
PDWORD ReturnLength // required buffer size
);
Param #5:
PreviousState
[out] Pointer to a buffer that the function fills with a TOKEN_PRIVILEGES structure that contains the previous state of any privileges that the function modifies. This parameter can be NULL.
If you specify a buffer that is too small to receive the complete list of modified privileges, the function fails and does not adjust any privileges. In this case, the function sets the variable pointed to by the ReturnLength parameter to the number of bytes required to hold the complete list of modified privileges.
That is the keyword. Normally these ype of functions need to be called twice. First to find the size of the buffer that is to be allocated (Marshalled in your case) and then to perform the actual function.
I came across these types too in the RASAPI, and I assume they are everywhere
See ms-help://MS.VSCC/MS.MSDNVS/rras/rasclnt_88j7.htm[^] for more info. Have a look at the C++ example and things will become clear.
UPDATE: oi that link dont work, funny links from Visual Studio help works... Just copy shortcut then.
Hope this helps
|
|
|
|
|
|
Try this ,
[StructLayout(LayoutKind.Sequential)]
class TOKEN_PRIVILEGES
{
int privilegeCount;
public LUID_AND_ATTRIBUTES[] Privileges;
public TOKEN_PRIVILEGES(int size)
{
this.privilegeCount = size;
this.Privileges = new LUID_AND_ATTRIBUTES[size];
}
}
or send me what you have...
|
|
|
|
|
Also LUID is a 64-bit value, so no need to create a LUID (which is wrong btw ) just use a long value.
|
|
|
|
|
i am having difficulty implementing this function c# i have implemented it in vb.net i am unable to write the values of the enums in c# as this is the way we write the eums in vb.net
Private Enum FileFlags
FOF_ALLOWUNDO = &H40S
FOF_CONFIRMMOUSE = &H2S
FOF_FILESONLY = &H80S
FOF_MULTIDESTFILES = &H1S
FOF_NOCONFIRMATION = &H10S
FOF_NOCONFIRMMKDIR = &H200S
FOF_RENAMEONCOLLISION = &H8S
FOF_SILENT = &H4S
FOF_SIMPLEPROGRESS = &H100S
FOF_WANTMAPPINGHANDLE = &H20S
End Enum
Private Enum FileFunctions
FO_COPY = &H2S
FO_DELETE = &H3S
FO_MOVE = &H1S
FO_RENAME = &H4S
End Enum
can anyone tell me the alternative of this in c#
thank you!
|
|
|
|
|
Instead of using &H1000S use 0x1000
HTH,
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
public enum SHFOOperation : int
{
Move = 0x1,
Copy = 0x2,
Delete = 0x3,
Rename = 0x4,
}
[Flags]
public enum SHFOFlags : int
{
None = 0x000,
MultiDestFiles = 0x001,
ConfirmMouse = 0x002,
Silent = 0x004,
RenameOnCollision = 0x008,
NoConfirmation = 0x010,
WantMappingHandle = 0x020,
AllowUndo = 0x040,
FilesOnly = 0x080,
SimpleProgress = 0x100,
NoConfirmMkDir = 0x200,
NoErrorUi = 0x400,
NoCopySecurity = 0x800,
}
|
|
|
|
|
I want to know how UploadFile can work?
I use it like this
myWebClient.UploadFile("http://localhost/myuploaddir/uploadfile.txt","c:\\myfile.txt");
but it always throw error 405 Method Not Allowed.
I set the directory to write allowed.but it does not work.
Many people encountered the same thing.
What is the problem?
lost my way
|
|
|
|
|
Hi,
Is there any way for differentiating whether a Forms Constructor is
called at DesignTime (by the FormsDesigner) or at Runtime.
I have some code in a Forms Constructor that should be executed only when the
Application is Run, and not when the Form is opened by the Forms Designer.
Thanks,
Firoz
|
|
|
|
|
If possible, you can add another constructor and those values there. The desingner will ( ) allways use the default constuctor, else you can check the DesignTime property, but that can be a bit over the top for something small.
Hope this helps
|
|
|
|
|
Hi leppie,
Thanks,
leppie wrote:
add another constructor
That is a great Idea. It works.
But, (in my project) this will require code modification in a lot of places.
Actually, I have a base form and around 40 forms derived from this base form.
It would be wonderful, if I could put some code in the Base Form constructor
itself, instead of modifying the constructor of all derived forms.
But I liked your Idea, and I will keep it as a last option.
leppie wrote:
check the DesignTime property
I had tried this before, but this property always returns false.
Can you put some more light into this (how to use the DesignTime property).
Thanks,
Firoz
|
|
|
|
|
so this does not work?
public MyClass()
{
if (DesignMode)
{
}
else
{
}
}
Note you can never check for this yourself...
Hope this helps
|
|
|
|
|
leppie wrote:
so this does not work?
Nope, that doesn't work... Why? Think about it this way; the DesignMode mode property uses the ISite property to do its work. But at that point of your code no properties have been set so ISite is still null which is why DesignMode will always return false.
Shortly after code execution has left the constructor for your object, then ISite will be set and DesignMode will return its proper value.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Thanx James
So the only way to go is an overloaded constructor?
|
|
|
|
|
leppie wrote:
So the only way to go is an overloaded constructor?
I don't really like that tactic because it forces the user's code to be modified from what the form designer spits out.
If at all possible I would try to delay the creation/use of the stuff that can't be done in DesignMode until the Load event or some other time.
Of course this isn't always possible so you are stuck with things like creating a different constructor.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Maybe I'm missing the point here but why not do it in the Form_Load event?
Paul
|
|
|
|
|
I think thats what James is telling us. We should only attempt to get a correct value from DesignMode after the form has been Loaded.
Something like this:
bool loaded = false;
protected override void OnLoad(EventArgs e){
loaded = true;
base.OnLoad(e);
}
Then we use:
public int SomeProperty {
get {
if (loaded & DesignMode) dosomethingelse();
return someint;
}
}
Correct me if I understand this wrong...
|
|
|
|
|
leppie wrote:
I think thats what James is telling us.
Ooops! Missed the change of name part-way down this thread.
leppie wrote:
Something like this:
Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event.
Paul
|
|
|
|
|
Paul Riley wrote:
Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event.
I guess thats to do with your own programming style. To me, its easier to override the function calling the event. You mite rather want to utilize the event, but in that case u need to keep track of it.
|
|
|
|
|
I read a lot about .NET and I've worked with VS .NET for a few days but I still cannot answer for myself the following question:
How will work programs compiled with VS .NET on other platforms? It seems a platform for developing applications for Windows and not similar to Java 2 Platform.
|
|
|
|
|
Zinj wrote:
How will work programs compiled with VS .NET on other platforms?
Someone writes or uses someone elses implementation of the .NET CLI. Of course most of those applications won't run anyway because technologies such as ADO.NET, Windows Forms, and ASP.NET aren't considered a part of the CLI. The Mono Project[^] is working on porting everything, so even those technologies will work.
Microsoft has also released Rotor[^]which is their implementation of the CLI which compiles on Windows XP and FreeBSD.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Rotor is designed for XP and FreeBSD.
Why XP? another .net framework?
and Why FreeBSD? can't port to linux(i think the the source code in FreeBSD can port to linux very quickly)?
|
|
|
|
|
elf wrote:
Why XP? another .net framework?
and Why FreeBSD? can't port to linux(i think the the source code in FreeBSD can port to linux very quickly)?
I have no idea, I was just quoting MSDN
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|