|
I'd be VERY worried about dragging a file from explorer into a text box, you are hoping explorer behaves the same for both XP and weven. One is probably dropping the filename into the text box and the other is tyring to drop the file object into the text box.
Inspect the content of what is dropped to determine what has arrived.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi.
I'd like to have access to the bytecode that is currently running or about to run in order to detect certain instructions and take specific actions (depending the instructions). In short, I'd like to monitor the bytecode in order to add safety control (see DETAILS below for explanation).
Is this possible? I know there are some AOP frameworks that notify you of specific events, like an access to a field or the invocation of a method, but I'd like to skip that extra layer and just look at all the bytecode myself, throughout the entire execution of the application.
I've already looked at several AOP frameworks (although not in great detail, since they don't seem to do quite what I need) and I'm familiar with Mono.Cecil.
I appreciate alternative suggestions, but I don't want to introduce the overhead of an AOP framework when what I actually need is access to the bytecode, without all the stuff they add on top to make it more user-friendly (... admittedly very useful stuff when you don't want to go low-level).
Thanks
DETAILS:
Basically, I have a C# application and I need to monitor the instructions it wants to run in order to detect read or write operations to fields (operations Ldfld and Stfld) and insert some instructions before the read/write takes place: I may need to acquire locks, or if that fails abort the operation. Also, I may need to update a read log (in case of a read) or write log (in case of a write).
In fact, what I'd really like to do is to replace the read/write instruction with my own custom code, but it that fails I think I could manage just inserting some instructions before and after.
|
|
|
|
|
Would a simple property do what you want?
Maybe even write a class to hold the affected fields.
|
|
|
|
|
I'm afraid not: I need to be able to detect accesses to any data, including fields of sealed classes which I cannot redefine as properties.
As for your second suggestion, if the field is of a sealed class I won't be able to write a wrapper that extends it. For example if I have:
class SomeClass {
SomeSealedClass fieldIWantToProtect;
}
sealed class SomeSealedClass {
...
}
I cannot write a class:
class SomeSealedClassWrapper : SomeSealedClass {
}
Thanks for your answer.
|
|
|
|
|
I was thinking more along the lines of:
private class ProtectiveLayer
{
private SomeSealedClass afield ;
public SomeSealedClass Afield { ... } ;
}
public class SomeClass
{
private ProtectiveLayer blah ;
public SomeSealedClass Afield
{
get { return ( blah.Afield ) ; }
set { blah.Afield = value ; }
}
}
But, I think you should explain more about why you want to do this; it doesn't sound worthwhile.
|
|
|
|
|
Again, I cannot do that, because SomeClass may be sealed and I won't be able to override it in order to introduce the property.
PIEBALDconsult wrote: But, I think you should explain more about why you want to do this; it doesn't sound worthwhile.
I explained it in the DETAILS section of my first post. I don't know what else to say; if you have any specific question I'd be happy to answer it.
The key here is that I don't just want to control access to specific fields of a specific class, but every field of every class, including library classes. This is because I need to log those changes in order to be able to undo them if necessary.
I know this seems pretty brutal but it's what my system is all about: protecting the data.
|
|
|
|
|
blackblizzard wrote: including library classes
Ah, classes you didn't write, I see.
blackblizzard wrote: undo them if necessary.
That's probably not a good idea, but maybe someone can figure it out.
|
|
|
|
|
PIEBALDconsult wrote: Ah, classes you didn't write, I see.
Yep.
PIEBALDconsult wrote: That's probably not a good idea, but maybe someone can figure it out.
What isn't a good idea? If you mean the undo, it's a very important requirement of the system, and it's completely necessary. There are transactions running in the system, and if they fail I need to roll them back (undo what they've done). It's pretty standard, and I've seen it done at a higher level in Java, but the way they do it isn't possible in the .NET framework.
EDIT: I should clarify something about the "undo": it's not that you let the transaction write whatever it wants to memory and then with the undo change it back to how it was. Rather, when the transaction wants to write to a field I intercept the write and write to the transaction's write log instead (write, write, write). If the transaction commits (i.e., finishes successfully) I dump the contents of the write log to memory (actually perform the write it requested). Otherwise, if it rolls back, I simply discard the contents of the write log and they're never written to shared memory.
In any case I need to be able to monitor all accesses in order to register them and redirect them to the write log, if necessary.
|
|
|
|
|
I would use Mono.Cecil to modify the binaries on disk.
Ideally you'd add a reference to a helper assembly, and use Cecil only to replace the ldfld/stfld instructions with calls to a method in your helper assembly (passing the field by-ref and/or as metadata token).
However, there's another option for bytecode modification that's not well-known: the .NET profiler API.
Using the profiler API, you can register a hook that will be called by the .NET framework to modify the IL bytecode immediately before it's JIT-compiled.
This way you can also instrument code that's dynamically loaded (Reflection.Emit). But it's much harder to use than Cecil because any changes to metadata (e.g. adding a reference to one of your methods) have to be registered with the running .NET instance, and there are some limitations on what metadata you're allowed to change at run-time.
Are you trying to implement software transactional memory for .NET? Even using the powerful profiler API, you'll likely run into limitations (e.g. when .NET calls into native code), not to mention performance problems.
|
|
|
|
|
Uhm... How did you get into my head? (or maybe they're the only two good options for this )
I was currently thinking about doing a combination of the two things you mention: implementing the profiler API and modifying the bytecode with Cecil whenever a class is loaded. My current idea (not very pretty right now, until I find something better) is:
1. Get notified whenever a class is loaded. The profiler API has a callback method for this.
2. Weave (modify) the class, if I haven't already modified it.
3. Unload the old class.
4. Load the new class. (I think that in order to load it as if it were the old class I need to write it to the .dll and then reload it; I don't need to tell you, this is pretty ugly)
And then forget about monitoring the code because I have already injected the control code into the class.
If could replace the class in a more straightforward way, like you do in Java by implementing a ClassFileTransformer, that would be great. I'd rather not monitor the code and just rewrite classes as they are loaded, but I currently don't know how to do this. Any input on this front?
Daniel Grunwald wrote: there are some limitations on what metadata you're allowed to change at run-time.
I don't need to change much, just replace the stfld or ldfld instruction with a call to a method that takes care of resolving any possible conflicts, logging, etc.
Daniel Grunwald wrote:
Are you trying to implement software transactional memory for .NET?
Yep.
Daniel Grunwald wrote: Even using the powerful profiler API, you'll likely run into limitations (e.g. when .NET calls into native code), not to mention performance problems.
Tell me about it, I keep bumping into walls. I wish I had an alternative, but this is a project I was given and I can't really change it. It's an STM in C#. In fact, it's a pre-existing STM, so I don't get much choice on how it works, just on how (how?!?! ) I manage to get it to work.
Thanks for your answer. If it wasn't exactly what I was thinking about it would've been really helpful . (Where were you yesterday before I thought this up? )
EDIT: I've just found the profiling API callback method JITCompilationStarted, which looks way more appropriate for my purpose than the ClassLoadStarted/ClassLoadFinished ones that I planned to use. According to this resource[^] it will allow me to view and modify the IL before it's JITed, which is exactly what I need. I don't know how I had managed to overlook this method until now. (Granted, I only found out about the profiling API yesterday, but still)
modified on Wednesday, April 14, 2010 1:22 PM
|
|
|
|
|
Hi all,
I am running the aspnet_regiis command from my application (as code example shows below). Is there any way to read the output that is displayed on the command window?
Process start = null;
start = Process.Start(new ProcessStartInfo()
{
FileName = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe",
Arguments = string.Format("-pef connectionStrings {0}", webConfiguraitonFilePath),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
});
start.Start();
Many thanks in advance.
Kind regards,
|
|
|
|
|
A quick bit of googling revealed this article: How to redirect Standard Input/Output of an application[^]
It looks like you'll need to amend your code in a similare way to below:
Process start = null;
start = Process.Start(new ProcessStartInfo()
{
FileName = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe",
Arguments = string.Format("-pef connectionStrings {0}", webConfiguraitonFilePath),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardError = true,
RedirectStandardOutput = true
});
start.Start();
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
|
|
|
|
|
Hi Martin,
Many thanks for the reply, I will definitely try it out.
Kind regards,
|
|
|
|
|
Just remeber to set the UseShellExecute property to false.
Process start = null;
start = Process.Start(new ProcessStartInfo()
{
FileName = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe",
Arguments = string.Format("-pef connectionStrings {0}", webConfiguraitonFilePath),
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardError = true,
RedirectStandardOutput = true
});
start.Start();
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
Thanks again.
Kind regards,
|
|
|
|
|
|
Hi,
How to draw semi transparent boxes(rectangles) in richtextbox control
Any help would be greatful.
|
|
|
|
|
Hello, Hope this is the right forum.
I'm working with web application (C# and asp.net) and I was wonder what are the disadvantages of start working with winforms (clickonce) instead of web applications?
I didn't found any new discusses about that.
Are there any things I can do with web forums that I can't do with winforms?
Thanks in advance,
Roy.
(I u think there is a beeter form for this - please tall me).
|
|
|
|
|
|
Hi,
I saw this article but it seems kind of old (internet explorer 6?!).
I thought that may be there are new differences with the advanced ajax and clickonce today.
Thanks,
Roy.
|
|
|
|
|
It depends on what the app does and/or should do.
Also if you want the app to be publicy available the go for web ASP/SileverLight/whatever suits your
needs and or style.
If the main reson of the app is to introduce data and some small reports than I would recomend
winforms/WPF/localapps/desktop apps simply because they are significantly more responsive.
Another option to consider is:
- is the app for internal enterprise/plant/firm usage?
- should it be available outside usage? If yes the whole app or just some small reports for management?
There's no absolute answer.
|
|
|
|
|
Thanks for the reply!
I will take it into consideration.
Roy.
|
|
|
|
|
There are several elements you have to consider when regarding web vs. winform.
First what does the application do? Do you require rich content with complex process (like a language processor) then you need to deploy a winform application.
If you don't have need for a rich application and can live with simpler controls, then web is the way to go.
If you need a rich experience with a choice between web and out-of-web experiences then you should consider Silverlight. SilverLight presents itself as a web application but can also be built to be an out-of-browser application with a development process closer to that of a winform application.
For myself, I've always been a backend...service based...winform developer. SilverLight gives me the development experience closer to a winform application which is much more comfortable for me than web is. (But stay aware of the fact that you must make service calls asynchronously which adds quite a depth of complexity in the design process.
|
|
|
|
|
hi,
i want to host a control in a datagrid cell(single cell) not the entire column.is there any easy way for doing this?
|
|
|
|
|
Check out this msdn link[^].
|
|
|
|
|
this is for entire column i think i need to add a control to only one cell i.e first cell of grid.
|
|
|
|
|