Have a context menu entry pass parameters to a windows service while it is running - wcf

I have a simple windows service called lets say "MyService", built using WCF that on start adds a registry entry to "HKEY_CLASSES_ROOT\Folder\Shell\{ContextMenuEntryName}" thus allowing for the right click menu anywhere in windows to have an entry named : {ContextMenuEntryName}. The target executed on clicking this entry is another program e.g. "{Path}\serviceclient.exe %1". The "%1" gives me the path of the windows folder that that context menu has been invoked on. This program then takes that value and passes it to my service by creating a proxy of the service and invoking its method.
The WindowsService code for its OnStart method is as follows:
protected override void OnStart(string[] args)
{
if (_serviceHost != null)
{
_serviceHost.Close();
}
// Add registry entry for context menu option System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
string contextMenuCommandPath =
Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("MyService")) +
"serviceclient\\bin\\Debug\\serviceclient.exe %1";
_contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "Folder");
_contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "*");
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
_serviceHost = new ServiceHost(typeof(MyService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
_serviceHost.Open();
}
The serviceclient.exe has the following code after adding a service reference named "MyService" to it:
// Instantiate service proxy
var myServiceClient = new MyService.MyServiceClient();
// Execute monitor target based on path specified (or default).
myServiceClient .Monitor(args.Length > 0 ? args[0] : string.Empty);
I would like to directly pass arguments to the Windows service itself when I click on the context menu entry instead of invoking another separate program that does the same thing.
Would it be possible to have the context menu entry directly pass information to a Windows Service in WCF while it is running?

It doesn't look like it on first glance, but it's a complicated subject. See Creating Shortcut Menu Handlers.

If you implemented a named pipe server inside your Windows service maybe you could send the file path data directly from the context menu command with something like:
echo %1 > \\.\pipe\{pipe-name}
However, you can't use the standard WCF NetNamedPipeBinding to implement the pipe server, because of all the .NET-specific protocol requirements baked into that binding, so you would need to do it either using the System.IO.Pipes classes, or write a custom WCF transport which just receives raw messages from the pipe.

Related

Eclipse Editor: automatically switch context when parts get active/inactive

I am developing my own eclipse editor and need to switch between different contexts for key binding. Currently I am doing the context activation/deactivation manually upon part activation.
This page
https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fworkbench_advext_contexts.htm says:
If you are activating a more specific Context within your part (either
View or Editor) you can use the part site service locator to active
your Context. The part's IContextService will take care of activating
and deactivating the Context as your part is activated or deactivated.
It will also dispose the Context when the part is disposed.
It seems like that is just what I want. But the page did not say how. Can anyone give me a hint what a 'part site service locator' mentioned in the text is and how to use it?
I would interpret the text so that you should use the service locator of the site that corresponds to your (editor) part. In the following example, part references your editor. By obtaining the context service from the part's site, you get a child context service for that particular part in wich you can activate a specialized editor context.
IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );
After digging through Eclipse code, here's my answer to my own question.
First thing first, it is JUST enough to invoke
IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );
anywhere after init(where you get PartSite), just as #RĂ¼diger Herrmann mentioned in his answer.
AND(here's my finding) NOTHING ELSE need to be done.
Eclipse will automatically activate/deactivate the context when part is activated /deactivated, just as described in the text I refer to. In addition, when the part site is disposed, all context will be disposed.
If you are interested in how, here's more digging.
Activate/Deactivate
When we invoke getSite().getService(IContextService.class), what we get is an instance of SlaveContextService.
When we call activateContext(String contextId) on it, our request will be automatically translate to a request with a default expression ActivePartExpression.
From it's name we can easily guess that this expression will check whether a part is active and do some changes. The changes it does can be seen at ContextService.UpdateExpression.changed. Here's the code(ContextService:124-128)
if (result != EvaluationResult.FALSE) {
runExternalCode(() -> contextService.activateContext(contextId));
} else if (cached != null) {
runExternalCode(() -> contextService.deactivateContext(contextId));
}
Whenever Eclipse context changes(activate/deactivate part will trigger context change), UpdateExpression.changed will be invoked and check whether the target part is still active, then activate/deactivate the context accordingly.
Dispose
In SlaveContextService.dispose, all context registered through it will be disposed upon the service's dispose.

Access the EPartService after an RCP application has initialized

After my application initialized, I'm trying to automatically create a part within a part stack. I need the EPartService for this but I can't think of any way to properly get a hold of this service.
I've tried using the LifeCycle management to get the current IEclipseContext. However, whenever I try to access the service using the context, it's not found.
Any idea how I can do this?
You should be able to inject the EPartService in any of the defined methods in your life cycle class. However you won't be able to show a part until the application startup is complete. So use the App Startup Complete event, by adding a method like this to the life cycle class:
#Optional
#Inject
public void appStartupComplete(#UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event,
EPartService partService)
{
....
}

How to Debug NinjectWebCommon.cs?

We are trying to debug through ninjectwebcommon.cs to find the binding for a respository.
In VS 2012, I am putting a Debugpoint on the kernel.bind but it doesnot hit anytime. Can someone sugggest me how to debug this?
I have NInject Version v4.0.30319
The not-so-simple and (obviously) temporary solution for me was to create a background thread in NinjectWebCommon.RegisterServices with the configuration I was debugging:
var thread = new System.Threading.Thread(() =>
{
while (!System.Diagnostics.Debugger.IsAttached)
{
System.Threading.Thread.Sleep(100);
}
// custom code, including kernel.Bind<>() calls here
});
thread.IsBackground = true;
thread.Start();
The idea here is to keep the thread you've created from executing the debuggable code until the debugger is actually attached. Setting the IsBackground property is important, because this allows the rest of the RegisterServices method to complete, which in turn allows the application to start and allows the debugger to attach.
Ninject is open source. You can download the entire project from their Github page at https://github.com/ninject. From there you can point Visual Studio to those projects instead of using the compiled assemblies.
Another option would be to use http://symbolsource.org as a Symbol Server. But it looks like they only have Ninject 3.

Is there a CommandParameter in XAML/WinRT?

Can a CommandParameter be passed to a Command in WinRT? How?
Actually, I may have misunderstood your question entirely. If you are talking about UI commands (commands that implement the ICommand interface) you can pass parameters when you call Execute. You can also test if the command and parameters are valid before executing the command by calling CanExecute.
As for passing a parameter as part of a Button binding, set the Command property equal to the command you want the button to execute and set the CommandParameter property equal to the parameter you want to pass.
Yes and no. WinRT applications can receive parameters through the Application.OnLaunched override.
The override receives an instance of type LaunchActivatedEventArgs which includes the arguments.
So it is possible to receive arguments, the question is more about how they can be passed.
Windows Store (WinRT) applications cannot be started from the command line. If a WinRT application is associated with a file type, it can be launched by calling ShellExecute on a file. Other than that, the application cannot be started directly.
It is possible to write C++ that launches a WinRT application using the IAplicationActivationManager interface and this interface can pass parameters to the launched application. So you could create a C++ launcher executable that could be called from the command line.
For more information on how to launch an application using this interface, see the following forum post:
http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/a4d2fca1-4034-4cc7-a86a-6242ce1a8b16

Launching RDP (mstsc.exe) with a supplied IP without autoconnecting

I am creating a program (in C#, .net 4) that allows the user to highlight an IP address from a textbox and click a button that launches RDP (MSTSC.EXE) supplied with the highlighted IP. What I'd like to do is NOT have RDP try to auto-connect to the IP (which is what happens if you provide the -v: argument). Rather, bring up the usual RDP dialog that will allow the user to either edit the IP or click "Connect" to go ahead and connect.
Is this possible? There doesn't appear to be any command line switch that prevents autoconnecting. The only thing I can think of is to create an .RDP file with the IP and then use the -edit switch. Although I'm wondering if it's possible to launch mstsc.exe and then do some kind of Clipboard paste to paste the IP into mstsc.exe?
Found the answer after rewording my question.
SendKeys.SendWait("^V") did the trick.
To avoid reconnection see this link
For the rest use
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
static void Main()
{
// Get the path that stores favorite links.
Process.Start("mstsc.exe", "/v:10.58.45.24"); //Or whatever
}
}
}