|
I have some code that checks a remote machines service to ensure it is all good. It installs as a service which I have tested as local system and network service and I get the
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
error. if I manually add a user to the application it works fine here is the part of code I am using to try to pass the alternate credentials any help would be appreciated
ConnectionOptions op = new ConnectionOptions();
op.Username = "xxxxxxx";
op.Password = "xxxxxx";
op.Impersonation = ImpersonationLevel.Impersonate;
op.EnablePrivileges = true;
ManagementScope scope = new ManagementScope(@"\\192.168.1.103\root\cimv2", op);
scope.Options = op;
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Service WHERE Name =" + servicename);
using (ManagementObjectSearcher ser = new ManagementObjectSearcher(scope, query))
{
ManagementObjectCollection services = ser.Get();
foreach (ManagementObject service in services)
{
}
}
|
|
|
|
|
Andre O Rahming wrote: if I manually add a user to the application it works fine here is the part of
code I am using to try to pass the alternate credentials any help would be
appreciated
sorry, I dont get that bit
Are you using Active Directory at your site ? - 'in loose terms', I'd create an AD profile witht he appropriate permissions, that can be used on each machine, and the service uses it and your service checker gets 'run as' the 'service profile' (that would have to be a safer way, rather than requiring hard-coded accounts/credentials in code)
'g'
|
|
|
|
|
Have a look at the user account under which you are running the service.
This account may not have access to WMI.
|
|
|
|
|
Also check that the account has access to network resources. I am not sure, Local System may lack them, try Local Network Service.
|
|
|
|
|
Hi,
I'm very new in c# code. I would like to have a list a windows that open in current desktop in window z order.I have go through the internet and found this code
public partial class Form1 : Form
{
public const int GW_HWNDNEXT = 2;
public const int GW_HWNDPREV = 3;
[DllImport("user32.dll")]
static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "GetWindow", SetLastError = true)]
public static extern IntPtr GetNextWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.U4)] int wFlag);
public Form1()
{
InitializeComponent();
}
public static Form GetTopMostWindow(IntPtr hWnd_mainFrm)
{
Form frm = null;
IntPtr hwnd = GetTopWindow((IntPtr)null);
if (hwnd != IntPtr.Zero)
{
while ((!IsWindowVisible(hwnd) || frm == null) && hwnd != hWnd_mainFrm)
{
hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
try
{
frm = (Form)Form.FromHandle(hwnd);
}
catch
{
}
}
}
return frm;
}
}
My question how to get the GetTopMostWindow() return value from form1() class?
Thank you
|
|
|
|
|
bunge-bunge wrote: My question how to get the GetTopMostWindow() return value from form1() class?
Form1.GetTopMostWindow(Application.MainForm.Handle);
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I'm at a loss for what this is supposed to be? Ever seen this pattern before?
[Serializable]
public class MyCollection : ArrayList {
public enum MyUpdateFields {
MyGroupName
}
}
So, that's basically an ArrayList that defines a useless enum? Why even create this in the first place? I would not even know this exists if Code Analysis hadn't complained about it.
|
|
|
|
|
Jasmine2501 wrote: So, that's basically an ArrayList Basically, yes. The author should be complimented for writing a specific collection-class, as opposed to using generics throughout the app for each collection. It makes changes easier, as one can easily change the collection-class, without having to check each method that uses the collection.
Jasmine2501 wrote: that defines a useless enum? I'm not sure whether it's "useless"; it's merely defined in the collection-class, implying that it's used there, or in conjunction with that class. If it's not used (place the cursor there an hit F12 to find all references) than it'd be better to remove it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't see the point in having an Enum that only defines one value. What possible purpose could that have? I can make it private and the app still builds - nobody is using it, and I posted all the code for the class, it's not using it. My concern is not about the unused code though, it's about and Enum with only one value. How do you think that might be used? It can only ever have one value, and it can't be null. I don't see the point. I was kind of wondering if it's done for some reason I've never heard of - a lot of this codebase is copied straight from the MSDN.
I agree that sometimes a named class that doesn't extend the "system" class makes sense. However, I don't have any issues with using ArrayList, if that's what you need. It is a waste of code and a useless increase in complexity - I don't need my own special ArrayList class, the system one is fine. I agree with what you're saying, but I don't think it's enough of an advantage in most cases. "Don't use the generic class" is often used to increase complexity for no reason at all. When the code is not very well documented, it puts me in the position of "OK, I see this class doesn't do anything extra from ArrayList, so why is it here?" and I can go and look at places where it's used and it's doing nothing special, and then I think "certainly the guy before me didn't write extra code just because someone told him not to use the generic classes" - sends me down a rabbit hole of trying to understand reasoning I didn't participate in.
In this question, I'm looking for someone to say, "oh yeah, that's the Lipshitz design pattern, here's a link"
|
|
|
|
|
Jasmine2501 wrote: What possible purpose could that have? Was it in one of their examples? Isn't it just there as a proxy for "you could hypothetically have a enum here.."?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't know where they got it from. I only had access to the previous developer for 'not long enough' and he showed me only the things he thought were important. He would be happy to admit that he has zero experience with ASP.Net - the site was converted from PHP, so it has some weird patterns and an overall odd style.
|
|
|
|
|
It's not a useless enum! The enum is simply scoped to the class MyCollection (a terrible name, if that's the actual name). So you can do stuff like this (for example). It's assumed that Foo has a member of type MyCollection.MyUpdateFields .
MyCollection coll = new MyCollection();
Foo f = new Foo();
foo.UpdateField = MyCollection.MyGroupName;
coll.Add (foo);
It's too bad the author hasn't commented their code. (This wouldn't even be accepted for a code review where I work).
/ravi
|
|
|
|
|
I can see in your sample code that you have created a variable of the enum type, and set the value to be the only possible value for that enum - that is all fine and good, but what are you going to use that value for? Remember, setting values that don't get used is a waste of memory - the compiler even warns you about it sometimes.
And yeah, I've changed the names to protect the innocent.
|
|
|
|
|
It's an example to demonstrate enum scoping.
/ravi
|
|
|
|
|
Could you explain what you mean by that? Or point me to a link, Google isn't finding anything with that term.
|
|
|
|
|
By scoped enum, I simply mean an enum that's scoped to a class. See the answer to this[^] SO question.
/ravi
|
|
|
|
|
I'd rather think the code stayed incomplete for some reason.
When overriding the Add() method of the collection (which he hasn't done yet), he could e.g. check that the item to be added has the correct value in its MyUpdateFields property and otherwise throw an exception.
Of course, Generics are a better way to solve it, but they were not available in e.g. .Net 1.1. And since the application was originally written in PHP, some odd ways of doing things might have their origin there.
|
|
|
|
|
Jasmine2501 wrote: Why even create this in the first place? Why even bother to try and figure out what this little-code-monster is, when you can get busy, and write better code than this dinosaur coprolite ?
Probably, the author intended, at some point, to extend the enumeration, for some purpose: who cares ?
'ArrayList is deprecated, for good reasons.
Suggest you review the section titled "Performance Considerations" here: [^].
yours, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
BillWoodruff wrote: Why even bother to try and figure out what this little-code-monster is, when you can get busy, and write better code
Because I'm in a testing phase right now and can't write additional code until that's done. The best I can do at the moment is try to understand what's here already. That is pretty important when taking over maintenance of undocumented apps.
|
|
|
|
|
Hello All,
I have an application that frequently uses the general List object with an bitmap type". I have two questions concerning these.
1: What is the best way to copy the bitmaps from one list to another while ensuring the Bitmaps in the original list are unchanged if changes are made to the new list. Currently i'm doing it in a forloop such as:
List<Bitmap> aviList1 = new List<Bitmap>(); aviList1.Clear();
for (int i=0; I originalList.Count; i++)
aviList1.Add(new Bitmap(originalList[i]));
2: Is calling originalList.Clear sufficient to free up the memory or would it be necessary to loop through the list and dispose of the Bitmaps individually?
Thanks in advance for the always great support here at CodeProject.
Bryan
|
|
|
|
|
Bitmaps are a scarce resource: they implement IDisposable, so you should clean up behind you, and Dispose all bitmaps you create. If you don't you may well get "Out of Memory" problems long before the actual RAM is exhausted. Additionally, depending on how you initially constructed the bitmaps, you may leave files or streams in use until the bitmap.Dispose method is called by your code, or by the garbage collector.
You need to construct a new image for each that you add to the other list, if you want the original unmodified when the aviList1[index] bitmap is changed.
However, it would look tidier with a foreach loop:
foreach (Bitmap b in originalList)
aviList1.Add(new Bitmap(b)); Or you could do it with Linq:
aviList.AddRange(originalList.Select(bm => new Bitmap(bm))); (But that just hides the loop)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Hi OG,
I am inferring from your answer (upvoted), that, in this case, it would be best for the OP to loop through his List<bitmap> and specifically dispose of them: that he should not rely on just clearing the List<bitmap>: is that correct ?
thanks, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
Yes - clearing the list does not dispose of any of the items on it (thank goodness!) so the bitmaps will be left hanging until the garbage collector gets round to disposing of them for you.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
If I may be so bold as to humbly point you to a little article[^] that I wrote a while back on memory managing images, you might find that it helps a lot. I use this a lot.
|
|
|
|
|
Sorry chaps, I couldn't find an appropriate forum so I'll take a chance I won't get flamed for using this one. I have a regex question. Given an input string like this:
Some text containing % complete and not much else
If I use a regex of \bcomplete\b I can find the whole word complete just fine. What I want to do is find % complete. I've tried \b% complete\b and \b\% complete\b and \b\x25 complete\b and other variations I can think of but I can never get it to select % complete. Does anyone know if it's possible to do it and how? I can find nothing anywhere that says % cannot participate in an expression.
I've tried two different apps, such as RegexBuilder and RegexBuddy but I can't get it working in either. Does anyone have any ideas? Thanks.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|