Trouble running a .pkg program as an admin - c#

At my work's sites, we have update packages that need to be run locally and synched with a small printer. These updates are of a .pkg format and when double-clicked should open a sync tool. Our sites are blocked to run any executables like this so a UNC prompt comes up and we need to enter our passwords and I'm looking for another way so we don't have to intervene everytime.
I've started a program that will open this file and run it under a domain admin account but the method process.start gives me the error "the specified executable is not a valid application for this os platform". To get around that, I've seen that I need to change the UseShellExecute property to True but doing this doesn't allow me to run it as a user so I'm back to the original problem.
Does anyone have any suggestions or alternative methods to achieve this?
Thanks in advance for any help!

Related

ClrMD on another user's process

I have a .Net application that's reporting hangs in the Event Log. The events say "Application Hang" with no usable details to go by.
So I whipped up a C# console app that uses the ClrMD library. But the caveat is this application is being published as a RemoteApp. When I try to attach ClrMD to the users' process I get this error:
Microsoft.Diagnostics.Runtime.ClrDiagnosticsException: Could not attach to process. Error 0.
This is not surprising since I'm logged into the server as Administrator and they are in their own RDP session with a different user.
So then I decided to impersonate the user they are logged in as thinking if I run my ClrMD app as them, it will have access to that process. This gave me a different error:
Microsoft.Diagnostics.Runtime.ClrDiagnosticsException: Could not attach to process. Error 1008.
I can't seem to find any clues as to what 1008 means to know where to go from here. I realize I'm probably trying to do something that's not possible.
Has anyone gotten ClrMD to attach to another user's process?
If not, are there are debug tools out there that can attach to other users' processes?
If not, have people used ClrMD in within production and how would I know the app is hung to know when to trigger a ClrMD dump? Just monitor the UI thread in another background thread?
Had / have same issue. Running elevated as admin on a server via a console app didn't work. But it does work if run through PowerShell or Visual Studio itself with elevated permissions. The debugger requires Admin privs to connect live. Adding a manifest, etc didn't seem to help when running as a console.

Start WPF application on computer startup

I have a WPF application that I would like to be launched anytime the computer starts NOT when a user logs in. I know I can place the .exe in the startup folder, but I don't want that since a user has to login for that to be launched. I cannot use a Windows service, it's not an option for me.
Is there a way to start up an application once the PC starts up(or reboots), even before a user logs in? Any help is much appreciated.
You can't run an application without a user login. The only thing you can run is a Service, which I'm guessing from your question being about WPF that this is not what you want.
How about using the Task Scheduler? In Windows 7, there's a "Security options" section where you can set an option for a task to run whether a user is logged on or not. You can specify an executable file, and it'll start when Windows starts up.
(This won't scale though, if you're looking for an option when a user installs your software. For that, a service is the correct way to go... you may need to elaborate in your question why exactly you can't use a service.)

Counting all instances of a .exe running on a shared folder

I have a c# application that is on a shared folder in which I have 3 or 4 people who all run the same .exe file. I need to be able to count all instances of the running process.
So far I've tried mutex and:
Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;
those work if I'm trying to compute the number of instances of the .exe file I have running on my machine, but this will be running on multiple machines all pointing back to the same shared executable.
I don't want to create a file that increments and decrements depending on if a user opens or closes it for that would be unreliable.
--------edit--------------
Does anybody know how Excel does it? If the 2nd person opens the file it says that it is in use and tells you who is using it
Thank you for your help!!!
There isn't any way other than to implement some form of a licensing service or if you have privileges to the running machines.
You can tell who has a share open:
open computer management (right click my computer and select manage)
then from the tree view on the left select:
system tools> shared folders> sessions
this lists who is accessing files through shares.
there is an SO answer about sdoing this programatically:
https://stackoverflow.com/a/2418657/359135
If you really wanted to take it further and you have the admin rights you could use this info to interrogate running processes on connected machines:
tasklist.exe /S SYSTEM /U USERNAME /P PASSWORD
http://www.watchingthenet.com/how-to-view-and-kill-processes-on-remote-windows-computers.html
I am not suggesting you do any of this.. more to highlight what would be required. I would run a server somewhere that received a open, close and ping notification from your app.
the ping notification would let you identify instances that had crashed, lost connectivity or for any other reason not sent a close notification to the server app.
I have seen this done just by having a table in a DB of open session that gets written to every minute or so. I don't know how this would work for millions of users but i have seen it working well for tens of users (up to about a hundred).
EDIT:
more on pinging..
if you have server side code you can actively look for missing pings, however I would be tempted to just use a DB table and add a where clause to your select when reading open sessions:
Select
*
FROM
Session
WHERE
LastPing > DATEADD(second,-60,Now())
and I would check for an old record on insert, so that you don't get lots of old rows hanging around.
EDIT: just to be clear if you used the crazy techniques listed at the start of this answer you would have no way of knowing that someone had copied the file to their machine locally and run it. If you make the program insist that it has a connection to a particular DB or server then you have much more control.
A process on one computer cannot detect processes running on a different computer unless you set up some kind of communication between them.
You could set up a shared file as you suggest, but yes it will be unreliable.
You could run a service on some central computer and each instance has to report back to the server, but of course that will have similar reliability issues. (If one instance dies without notifying the server, the server won't realize it's still running.)
When each instance runs, it could listen at a port, record its IP and port number in a central file, and when you need a count you try to connect to each of those ports to confirm that instance is still running.
There are lots of ways you can do it, but the work is up to you. No help from the OS like you get when they're all on the same machine.

Console Application stopped working

Is it possible for code to work perfectly on the Administrator profile and not work at all while logged in as another user?
I am building certain applications at work, and while developing, coded and tested while logged on as Administrator. Every application working normally.
Now before deployment, when I execute the same application, but now logged in with a specific user account, I just get the error "Console Application stopped working" etc etc. I have handled all possible exceptions with my own Message Box, but this error is something I did not expect?!
In the exe's, I tried changing setting to "Run as Administrator", no use!
Any help would be much appreciated. Thanks!
PS - There are certain FTP methods I reuse from another class. And by handling all possible exceptions, I mean, I would have a try-catch block for the FTP method, so in case of error encountered after deployment, affected user may call helpdesk and notify the exact error.
Well, its not the right way to go, but right click and run as Admin, works butter smooth. Should I go ahead and modify all executables to - Run as Administrator under Properties->Compatibility??
You should firstly investigate if your application does need Admin rights indeed. A few scenarios that I can think of where an App needs admin rights are writing to protected locations of the system, writing to registry (excluding the areas that the user has rights on). If you think that admin access is not needed, you could always seek other ways to make it work. For example, instead of writing to a protected location, create a directory for you application in the user's application data folder and write to that location.
When you launch the application, if you're running on I believe Windows Vista or higher(7 works this way for sure)
You can Right click on the application and click on Run as Administrator
And yes, Sometimes the application won't work if you're not running as an administrator. For instance, If it's trying to change files in a protected location.
There are other variances that can cause your program to not work, For me, "Console Application stopped working" normally means that you don't have the right version of the .net framework installed, but it can mean any number of things.

Getting program to run at start up from c# code. Setting Registry Run to 'true' doesn't work as well

I found this nice snippet of code online:
rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Which runs great but alas on windows 7 and vista I suspect, it crashes cause it doesn't have permission to write there.
So then I research (on stackoverflow of course) how to avoid this, quickest method:
rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Simple enough! Though there is two issues remaining. One is with both methods (on a XP Box that is), the program thinks its relative path is somewhere in the C:\windows folder now...so I.e. it won't find my path relative help files etc. The second issue is, on windows 7, my program won't startup until I log in. I want it to start up in the background if possible before anyone logs in.
Its a simple .exe that hangs out in the systray when its running. I didn't want to create this monstrosity of an installer to get around these admin and pathing issues.
I Think I would have to create it as a service (no clue how to do that) to get it to start up when the machine reboots before anyone logs in. Secondly to do that I am sure I have to figure out the admin privileges, and since I don't want to have to approve the program to run every time it starts up it sounds like I would have to figure out its admin privileges during install time, but alas no installer.
So just curious what routes I might take to get this to work. I can even suffer it coming up only after when the user logs in, but my current methods that work this way really screw up the pathing of my program since it tries to write stuff out to a new directory (not the one I originally started the EXE from). Etc...and I have no clue how to go about fixing that pathing issue.
It sounds like you need two programs here.
You can't have an application run in the system tray and run prior to login. The system tray doesn't "exist" until the user logs in and has a valid desktop.
The normal way to handle this is to make two programs. First, create a windows service that does the bulk of your work. This will run on startup, and be independent of any user logins.
Then, make a user mode application which uses IPC to communicate with the service. This can run on login, and "talk" to the service remotely, thereby providing your system tray requirements.
If you want your program to start as a service before anyone logs on, then it's going to need to be installed and run as an admin user. There's not getting round this fact.
There's a Microsoft Knowledge Base article on creating a service which should get you started.
There's a project template for a Windows Service installed by default in Visual Studio 2008:
"File > New > Project > Visual C# > Windows > Windows Service"

Categories