How can I create my custom Shell Context Handlers for Windows? - c#

Problem
Language: C# 2.0 or later
I would like to register context handlers to create menues when the user right clicks certain files (in my case *.eic). What is the procedure to register, unregister (clean up) and handle events (clicks) from these menues?
I have a clue it's something to do with the windows registry, but considering how much stuff there is in .net, I wouldn't be surprised if there are handy methods to do this clean and easy.
Code snippets, website references, comments are all good. Please toss them at me.
Update
Obviously there is a slight problem creating context menues in managed languages, as several users have commented. Is there any other preferred way of achieving the same behaviour, or should I spend time looking into these workarounds? I don't mind doing that at all, I'm glad people have put effort into making this possible - but I still want to know if there is a "proper/clean" way of achieving this.

Resist writing Shell Extensions in managed languages - there are a multitude of things that could go bang if you pursue this route.
Have a browse through this thread for more details. It contains links to do it if really want, and sagely advice of why it can be done, but shouldn't.
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/
You're back to unmanaged C/C++ as your only real tools here.

This is not a good idea because of potential dependency issues between different versions of the .NET Framework. Your shell extension could be expecting one version, while a different version may have already been loaded by the application that's currently running.
This thread contains a good summary of the situation.

While others already mentioned that writing shell extensions in pure .NET is a bad idea due to framework conflicts, you should still note that:
There are 3rd party drivers out there (see Eldos or LogicNP) that do the unmanaged side for you, allowing you to write managed code that talks to the native driver, thus preventing shell-related CLR version conflicts.
A recent MSDN article mentioned that Microsoft has solved this problem for the CoreCLR, as used by Silverlight. They've accomplished this by allowing multiple versions of the CLR to run in the same process, thus fixing the problem. The author further stated that this fix in Silverlight will be rolled into future versions of the full CLR. (Meaning, in the future, it will be quite feasible to write shell extensions in managed code.)

I've done them before in C#. It ends up being a hell of a lot harder than it should be. Once you get the boilerplate code down, though, it is easy to roll out new items. I followed this link:
Link To Info

As the prior comments mention, it isn't the best idea to write shell extensions in managed languages, but I thought I'd share an Open Source project that is doing just that :)
ShellGlue is a managed shell extension that is actually quite helpful. The source also might be helpful to you if you're interested in pursuing writing a shell extension in C/C++.

Aside from the caveats that have been mentioned concerning the implementation of shell extensions in managed code, what you'd basically need to do is the following:
First, create a COM component in C# that implements the IShellExtInit IContextMenu interfaces. How to create COM components in C# is described here. How to implement the necessary interfaces is described in this article. While the description is for a C++ implementation, you can apply that knowledge to you C# version.
Your COM component will have GUID called the Class-ID or CLSID. You need to register that ID with your file type as a context-menu shell extension:
HKEY_CLASSES_ROOT\.eic\ShellEx\ContextMenuHandlers\MyShellExt
(Default) -> {YOUR-COMPONENTS-CLSID}
Also make sure that you registered your component correctly as described in the C# COM tutorial. You should find it in the registry under
HKEY_CLASSES_ROOT\CLSID\{YOUR-COMPONENTS-CLSID}
InprocServer32
(Default) -> C:\WINDOWS\system32\mscoree.dll
Class -> YourImplClass
assembly -> YourAssembly, version=..., Culture=neutral, PublicKey=...
...
Good luck...

As others have pointed out, shell extensions are not practical in windows development currently.
I asked a similar question recently which was answered with a link to a guide to do exactly what I wanted to do

Related

How to give user an equivalent Excel-VBA environment for my application in C#

I'd like to give user the opportunity to do its own report based on my class hierarchy (object in memory). A scripting language like VBA: Visual Basic for Application. Something that would be evaluated at runtime.
I'd like the user to be able to dynamically create its own report based on some available restricted objects marked with specific attributes only. Give user possibility to create macro where he have similar model as in VBA-Excel : Application/Workbook/WorkSheet/Range/... but with my specific selected objects instead.
Then, he would have been able to use intellisense and add its own functionality to its application.
Update: There is something very important that appears to me. The editor should be preferably part of the application itself in order for the script to know all the environment (namespace, objects, ...) it should interact with. Otherwise, having a scripting engine/editor in a separate process would force the application to be either a "single-instance" app or to create a tricky way to hook the script to a specific running application process.
Does it exists and how to plug it to a .net app?
4 solutions have been proposed:
IronRuby
CsScript
WinWrap
Windows Scripting Host (WSH)
I also found:
ScriptCs
RoslynPad
Code Project - Anoop Madhusudanan article: C# as a Scripting Language in Your .NET Applications Using Roslyn
But I wonder which one could fits every needs (or most of them):
Editor with Intellisense
User have access the currently running process object model (Application as global and all its related objects accessed through application properties). Optional but very nice to have: not necessarily every public properties, only marked ones with specific attributes.
Interactive environment enabling step by step, debugging, breakpoints, ...
Editor/debugger Licensed for free redistribution
Not required, but preferably use C# language
Actual testing:
I tried to evaluate #2 but there is no VS project sample with all modules working together (which seems a little bit complex to assemble).
I currently evaluate #6 which seems to work fine (I corrected a minor bug). It seems to fits most points but #3. But it seems to be the closest to my needs yet. It also uses Roslyn which appears to me a positive aspect because it is the Microsoft compiler which should always be up to date.
I'm still waiting to see any feedback from anybody with either other better solutions or any advise to help me take a better decision.
After some thought, trials and errors. My needs become clearer and my understanding of to current state about Roslyn development was also better.
I realized that Oleg Shilo (Author of CS-Script) was right about the fact that Roslyn is not ready (2014-10-21) to help me for intellisense. But I think it should happen relatively soon. I think it should worth the wait. But it can compile and run code into the same running app I would like to run tested in.
Although any other approach could have probably work better right now. I think that a solution like RoslynPad would be closer to the ideal solution for my needs. Most other proposed solution where nice for general scripting but not as good as RoslynPad for inside app own scripting... at least according to my opinion.
I then fixed some little issues in RoslynPad and hook it to the newest AvalonEdit which also fixes some other issues with auto-completion (but still with few bugs).
I now get needs filled up as this:
#1 Partial (ready for full when Roslyn will supported and documented it)
#2 Full
#3 No (ready for full when Roslyn will supported and documented it)
#4 Full
#5 Full
Thanks a lots to all people giving me ideas, links and very nice advise.

Building An App With Plug-in Support

I'm starting a new project which would greatly benefit from program add-ons. The program in its most basic form reads data from a serial port and parses it into database records. Examples of add-ons that could be written would be an auto-archive add-on, an add-on to filter records, etc. I'm writing both the program and the add-ons, but some customers need custom solutions, so instead of branching off and making a completely separate program, add-ons would be great. The simplest add-on would probably be a form who's constructor takes an object reference, manipulates the object in some way, then closes.
Unfortunately, I have absolutely no idea where to start coding, and almost as little idea where to search. Everything I search for turns up browser add-ons. From what I have gathered, I need to look into dynamic loading DLLs. Besides that, I'm clueless. Does anyone have any good resources or examples I that they know of?
I'm happy to provide more details, but this project is in its inception, so I don't have a ton of specific details (specifics kind of defeats the point of add-ons, too.)
You should seriously consider using the Managed Extensibility Framework (MEF) to handle your plugin architecture. It requires thinking about things a little differently, but it is well worth the mind-stretch.
This is a simple example to illustrate the basic technique.
codeproject.com - Plugin Architecture using C#
This article demonstrates to you how
to incorporate ... as a
plugin for another application or use
it as a standalone application.
in .NET 4 you now have the Managed Extensibility Framework (MEF) to do much of the plumbing.
In .NET 3.5 you had the System.AddIn but it was deemed by many to be far too complex.
codeproject.com - AddIn Enabled Applications with System.AddIn
AddIns (sometimes called Plugins) are
seperately compiled components that an
application can locate, load and make
use of at runtime (dynamically). An
application that has been designed to
use AddIns can be enhanced (by
developing more AddIns) without the
need for the orginal application to be
modified or recompiled and tested
You really need to look at Managed Extensibility Framework (MEF). This is specifically designed to help support add-ons and other extensibility.
A very basic description (basically, your plugins must implement a special interface):
http://martinfowler.com/eaaCatalog/plugin.html
Much better article, in C#:
http://www.drdobbs.com/184403942;jsessionid=TVLM2PGYFZZB1QE1GHPCKHWATMY32JVN
I think Reflection will play a major role.
I expirimented with an app that had a plugin folder. A filesystem watcher would watch the folder, and when a new DLL was placed in it, it would use reflection to determine which types of plugins it included, loaded them, and added them to the list of available classes, etc.
Try using the term 'add-in' or 'plug-in' for your research instead of 'add-on'. That should help some.
If you're using .Net 4, there's an add-in namespace in the framework that will get you partway there.
Writing plug-in support for an app is no simple task. You'll have to maintain pretty strict separation-of-concerns across your interfaces, you'll need to provide an interop library that defines ALL of the supported plug-in types, and you'll want to do some research into dependency injection & inversion of control, in addition to the previously-suggested reflection research.
It sounds like you might have a busy weekend doing research.

linker for .net application

I have a desktop(winforms) application, and I'm looking for .net linker that links the assembly to assembler level(lower than IL) in order to prevent reverse engineering. another solution might be acceptable as well.
does anyone know of such a linker?
What you're looking for is an obfuscator. It jumbles up the compiled code so that it still does what it is supposed to do, but if you decompile it, it is incomprehensible to most.
Note that any attempt to make it 100% safe is guaranteed to fail, all you can strive for is making it as hard as possible.
There's many solutions that will do this in various ways:
Dotfuscator
Remotesoft Salamander Protector
Spoon Studio
Note, the last one isn't an obfuscator, it virtualizes out your application. Some of the benefits of that is that it is a bit harder to get to the underlying code, but it is primarily a product that solves different problems, namely the need to separate out the application from the rest of the applications installed (ie. no need for .NET to be installed, no conflicting registry settings, etc.)
You may want to have a look at the The Mono AOT (Ahead of Time) Compiler :
Ahead of Time Compilation
Mono Ahead Of Time Compiler
There are some limitations though: you obviously can't compile an assembly that uses CLR dynamic features, reflection, etc.
.Net Reactor is what you are looking for I guess (it is hackable - but way harder than classic obfuscation).
XenoCode has a cool tool that will help you do that:
http://www.xenocode.com
Checkout Code Projection by Xeno, its effective.
Obfuscation is may be good for your needs. But it still hackable.
I don't know any programs and utilities that you asking for but I have an advice for you.
If you need to protect not all application code but only critical sections, you can implement this sections in C++/C and use Platform Invoke to interop with unmanaged code.
Jeffrey Richter recommended this approach.

Is there a source code control system that understands/records c#/vb.net factorings and how to merge them?

MolhadoRef is a refactoring-aware SCM
tool that is capable of capturing and
versioning of the semantics of Java
program entities and refactoring
operations that were performed on
those entities.
Does anyone know of such a system in the .NET universe?
(Why is it that .NET always seems to be way behind java with the type of tool support?)
Does anyone know of such a system in the .NET universe? (Why is it that .NET always seems to be way behind java with the type of tool support?)
First, your question is a little misleading. This isn't a "Java tool", per se. It's a tool that happens to understand Java, but works as an SCM for any blob of text; you just won't get the refactoring support if it's not Java code. So one could could easily ask instead (and perhaps more accurately), "why doesn't MolhadoRef support C#?"
Second, the reason you don't see many tools like this is because this is an academic research project, not a commercial product. It's also a prototype, not a finished work. Notice that the last update was around 2007.
Finally, the design seems awful. They had to design an entirely separate SCM (Molhado) solely for the sake of implementing these ideas, when it sounds like many of them could have been done in the context of an existing SCM. I won't second-guess the researchers since that smacks too much of armchair quarterbacking, but modern SCM tools like git or svn could very easily have third-party plugins that would do exactly the same thing MolhadoRef does, without requiring you to switch to an entirely new SCM ecosystem and wreck your toolchain.

Can you run C# Code from c++?

Can you run C# code from c++?
and How?
If you're C++ code is "managed" C++ that's built on the .NET common language run-time (CLR), then it's easy to reference a C# assembly and invoke public classes and methods. If, however, your C++ code is "native" (not built on the CLR), then you'll want to register your C# assembly for COM interop and invoke the COM object from your C++ code. There's an MSDN article that covers all the gory details:
http://msdn.microsoft.com/en-us/library/w29wacsy(VS.80).aspx
There's also a good article on CodeProject by Nick Parker called "Exposing .NET Components to COM" that you may find useful.
You can use unmanaged C++ to run a .NET application, but how difficult it will be will depend on which version of .NET you are using.
When I did this with .NET 2.0 it took me two solid weeks to get it working.
The answer in this page gives guidance as to which programs are needed to do this.
http://www.pcreview.co.uk/forums/thread-1225474.php
The other option that you have, depending on what you're trying to do, is to host the CLR in your application which allows you to more tightly integrate the C# code than is possible by going through COM. You can read an overview of doing this in this MSDN Magazine article.
With C++/CLI, yes (formerly Managed C++ -- which had different extensions to the language).
yes, you can use COM to call .NET
You can call .NET code without COM by using the mscoree.dll ClrCreateManagedInstance Function. You need to supply the assembly qualified name of the type name you want to create, in the pTypeName parameter.
From memory, in some cases you may need to add the ComVisible attribute to the interfaces or classes you wish to access using ClrCreateManagedInstance(). However this does not require you to also register your class or any of the other messy deployment issues that go with COM.
I'm guessing the answer that would really be useful to Chad is:
Yes, it is most definitely technically possible to run C# code from C++. However, if the other answers on this page sound like they are going over your head, then you are nowhere near experienced enough to actually do this. It is pretty difficult to pull off, all things considered, so unless you really need to be running C# code from C++, it might be best to just rewrite the C# code as C++.

Categories

Resources