|
hi everybody,
i want to make a little programm that detect (and logging) all the new launched process on my computer.
i do not know which event is fire when a new process is opened :/
if someone could help thx a lot
|
|
|
|
|
Well, there is no event fired that signals a new process has been launched. At least not a normal one.
In theory, what you could do is use the System.Management classes to subscribe to a WMI generated event, that you have to setup, that fires when the Process list changes.
...in theory...
I've never done it, so I couldn't give you all the sticky details of the WMI part of the project.
RageInTheMachine9532
|
|
|
|
|
You could also implement a system hook (see Using Hooks from C#[^]) using the WH_CBT message to no when a window is being created, but this doesn't necessarily mean a process is being started. You can P/Invoke GetWindowModuleFileName to pass the HWND (as an IntPtr ) and get the path to the module that loaded it. If you keep these in a lookup table (like a Hashtable ), then you could quickly see if the process was already loaded. If not, add it and run your "event" handler.
Otherwise Dave's idea should work, but as he said it's untested. I've used WMI events for other things, so it should work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thx for your replys !
i found a possible WMI event :
Win32_ProcessStartTrace
The Win32_ProcessStartTrace event WMI class indicates that a new process has started.
but i don't know how to use WMI event, if you have an example ?
thx a lot
|
|
|
|
|
You're on the right track with that. The only problem I can see is that Win32_ProcessStartTrace requires Windows XP. Is this going to be a problem?
As for an example, I'd need some time to come up with one. I'm just starting to cook dinner now...
RageInTheMachine9532
|
|
|
|
|
yep the fact that it requires XP is a problem
so i'll try what ian propose =)
thx all
|
|
|
|
|
For the most basic monitoring, you can write a simple class to monitor processes:
using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;
public delegate void ProcessStartedEventHandler(object sender, Process proc);
public delegate void ProcessExitedEventHandler(object sender, Process proc);
public class ProcessWatcher
{
#region Constants
private const int SCAN_INTERVAL = 250;
#endregion Constants
#region Instance Fields
private Mutex _myLock = new Mutex(false);
private Hashtable _procList = new Hashtable();
private Timer _scanTimer = null;
#endregion Instance Fields
#region Construction / Destruction
public ProcessWatcher() { }
~ ProcessWatcher()
{
Stop();
}
#endregion Construction / Destruction
#region Methods
public void Start()
{
_myLock.WaitOne();
if (_scanTimer != null)
{
_myLock.ReleaseMutex();
return;
}
_procList.Clear();
_scanTimer = new Timer(new TimerCallback(_Scan), null,
SCAN_INTERVAL, Timeout.Infinite);
_myLock.ReleaseMutex();
}
public void Stop()
{
_myLock.WaitOne();
if (null == _scanTimer)
{
_scanMutex.ReleaseMutex();
return;
}
_scanTimer.Dispose();
_scanTimer = null;
_procList.Clear();
_myLock.ReleaseMutex();
}
#endregion Methods
#region Implementation
private void _Scan(object unused)
{
_myLock.WaitOne();
_scanTimer.Change(Timeout.Infinite, Timeout.Infinite);
ProcessStartedEventHandler startListeners = ProcessStarted;
ProcessExitedEventHandler exitListeners = ProcessExited;
ArrayList list = new ArrayList();
Process cur = null;
foreach (Process proc in _procList)
{
if (!proc.HasExited) continue;
list.Add(proc.Id);
}
foreach (int id in list)
{
if (exitListeners != null)
exitListeners(this, (Process)_procList[id]));
_procList.Remove(id);
}
list.Clear();
Process[] running = Process.GetProcesses();
foreach (Process proc in running)
{
if (_procList.Contains(proc.Id)) continue;
list.Add(proc);
}
foreach (Process proc in list)
{
_procList.Add(proc.Id, proc);
if (startListeners != null)
startListeners(this, proc);
}
_scanTimer.Change(SCAN_INTERVAL, Timeout.Infinite);
_myLock.ReleaseMutex();
}
#endregion Implementation
#region Events
public event ProcessStartedEventHandler ProcessStarted;
public event ProcessExitedEventHandler ProcessExited;
#endregion Events
}
With the above, you can add handlers to the ProcessStarted and ProcessExited events to do whatever you wish.
The class is very basic and just off the top of my head. It may have unforseen issues so be cautious! It should be fairly thread-safe, with the _myLock.WaitOne and _myLock.ReleaseMutex calls to lock and unlock access to the important bits, but you may want to look for deadlock conditions.
You may need to adjust the SCAN_INTERVAL to better suit your needs, though I don't recommend really small intervals.
All you need to do is write your particular ProcessStarted and ProcessExited handlers; create an instance field of the class; add the hanlers to the appropriate events; and call Start to begin monitoring. The destructor will automatically call Stop for you.
Enjoy!
|
|
|
|
|
Look at PsSetCreateProcessNotifyRoutine()...
.............................
There's nothing like the sound of incoming rifle and mortar rounds to cure the blues. No matter how down you are, you take an active and immediate interest in life.
Fiat justitia, et ruat cælum
|
|
|
|
|
Does anyone know how to get Graphics.MeasureString to take into account whitespace (spaces and tabs namely) at the ends of strings? I tried feeding it a StringFormat object with the Trimming property set to StringTrimming.None but no dice. What am I missing?
|
|
|
|
|
If you read the documentation for the StringFormat.Trimming property, you'll see that it controls how strings are trimmed if they lay outside of the clipping region. It has nothing to do with trimming the whitespace on strings.
See the StringFormat.SetMeasurableCharacterRanges to specify that whitespace characters should be included when measuring.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.FitBlackBox
top secret
|
|
|
|
|
Thanks ya'll! Works like a charm!
|
|
|
|
|
I have a situation where I wanted to expose the properties of a constituent control(TextBox in this case) of my user control in the property grid. I found this to be very simple by just making a public property for my TextBox and works fine. The problem is that it doesnt write the changes of the TextBox state to the code file. If I expose they properties of the TextBox directly off the user control it does save them to the code file. I guess my question is would be: Do I have to expose every property of the constituent control on the user control to get it to generate the code or is there a work around. Someone please point me in the right direction
|
|
|
|
|
The code is generated to get/set properties on your class - the container class in this case. It would set your property to whatever text you use. When the code is executed, your property is set at runtime which sets the property on the TextBox . If you're looking for a way to control the code generation so that setting your property actually serializes the code so that the child TextBox is set, you're not going to find an easy one and I'm not sure it's even possible since VS.NET (or any designer, like SharpDeveloper) is what provides the code serialization implementation.
As far as exposing all the properties of a child control, you need to decide what you want to expose to the user. The whole idea of a complex container control is to encapsulate other controls as you require. If you end up exposing all the properties of a child control, then what's the point of encapsulating it in the first place?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If you want them to be able to design your control in such a way, you'll need to extend ControlDesigner and provide full designer support for your control. As far as the properties go, though, my original reply still stands. The form/control designer is designer your control, not the child controls. When a property is set on your control, it will serialize the code by setting the property on your control, not the child controls which it knows nothing about. It still won't really make a difference, though, since your properties are basically reflecting the values back on the child controls' properties.
Getting a designer to do this will still be tricky, though. You can extend your designer by providing this type of functionality but getting it to serialize the code appropriate is another matter. Read through the documentation for the ControlDesigner and related documentation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have an application, that needs to have a settings dialog. I want the settings to be available to the rest of the application all the time, even when the settings dialog is closed. In other words, this dialog is merely a utility for the user to modify these settings and save them.
I'm thinking, I should have the settings struct, so that it's a value type, which doesn't need instantiation to be used, and be available all through the application lifetime. Now the challenges are:
1- What's the best method to load/save this struct? Can XML serialization work here? If so, how?
2- Can I bind the struct's fields/properties to controls on the settings dialog, so that any modification will automatically reflect in the struct, which, in turn is the one responsible for actually loading/saving all the settings?
Thank you.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
A singleton pattern works well for this. We use a PropertyGrid for the time being in our application. I create a singleton that uses a collection of classes implementing the ICustomTypeDescriptor and gets everything from a database. The application allows the changing of these values while callers throughout our application have a simple means to get these values. Just put the classes in an assembly that is shared and used by all the assemblies in your application. I put ours, for example, in our toolkit assembly.
XML Serialization would be a good way to read and save your preferences. Just see the System.Xml.Serialization namespace for all the attributes you can use, as well as the XmlSerializer that does include a sample of how to use it. It's really very simple.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
singleton pattern
I'm sorry, but I don't know what that is
Heath Stewart wrote:
Just put the classes in an assembly...
So you do suggest classes instead of structs? I read in an article on CodeProject before by Nishant that whenever you find that you're using a class merely to store values, it's better to use a struct. Comments?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
A singleton is a single instance of an object. You can either use a simple approach like:
public sealed class MySingleton
{
private static MySingleton instance;
private int i;
private MySingleton()
{
i = 1;
}
public static MySingleton Instance
{
get
{
if (instance == null)
lock (typeof(MySingleton))
if (instance == null)
instance = new MySingleton();
return instance;
}
}
public static int SomeMethod()
{
return Instance.i;
}
} Or you could use a ContextBoundObject with its own RealProxy derivative that returns the same instance when someone tries to instantiate your object.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
please some one tell i have a barcode scan device and i wanna to write a code using c# to get the barcode from the barecode device
i will make that by making a thread in my code to detect any input from the barecode then:
_how to make that connection between my code and the barecode device so that user can input the serial of any product from the keyboard (by manualy focusing at text box) or from the barecode device (without focusing at any thing)
please some one can help me in that senaro i wanna just an overview idea about how to make that way of code
::::tonaxxl:::::
|
|
|
|
|
You already asked this question and got the answer to it.
It all depends on how you barcode scanner works. If it injects data into the keyboard stream, then all you have to do is make sure that the focus is on the textbox. If it sends data to the PC through another means, like the Serial Port, then you'll have to write a communication class to handle that kind of input.
It all depends on the barcode device and how it sends data to the PC...
RageInTheMachine9532
|
|
|
|
|
If it sends data to the PC through another means, like the Serial Port, then you'll have to write a communication class to handle that kind of input.
:::::then how to make that communication class that listen to the serial port ?
i wanna to find the way to to connect to that serialport
i dont know how to make that
|
|
|
|
|
First thing's first...
The make and model of the scanner would be helpful.
Do you have all the docs on the scanner? Do you know the communication requirements of the scanner? Do you know what format the data comes in? Do you have the commands the scanner understands? The formats of those commands?
If not, your wasting your time...
RageInTheMachine9532
|
|
|
|
|
no i have nothing from what u say but i wanna the whole story of using the barecode if have a breif idea about the way to handle the barecode numder from the any kind of serial barecode scanner i dont know any thing about that stuff and i ask any one to help me by him self or by sending me any url to help me in that stuff
and i search and found all the code write in that is for the way to print the barecode but i wanna to input it and how to handle it
thank u and i hope i make my self more clearar
|
|
|
|
|
If you don't have any of that information, you're going to have VERY difficult time doing this.
Basically, all you've told use is that you have a barcode scanner and that you want to write a program. You haven't given us ANY information to work with, yet you want help with writing code.
All I can tell you is to search the web for code samples, search for the manufacturers website and search for the technical docs on the scanner.
You can start with the code in the previous post for Serial Comm in VB.NET and you can also look at the VB6 code here for a barcode scanner example. Don't know if it's going to work for you because you haven't been able to tell us what barcode formats the scanner can read.
http://www.vbexplorer.com/vb/scripts/ShowCode.asp?txtCodeId=47696&lngWId=-1[^]
RageInTheMachine9532
|
|
|
|