none
Execute C# from a desktop script RRS feed

  • Question

  • I need to execute C# from a desktop script; what I mean by desktop is without a server.

    I need to do this for an IE context menu item. I know how to install the script according to Adding Entries to the Standard Context Menu. I know I could execute a separate application as described in how to add my own menu to IE context menu. What I need (I think) is something more like what is described in wcf from com class library not find, so that I have access to the page's document (the DOM).

    I don't need details, but details will be appreciated. If I just get enough of a hint so I know what to read then that will sure help.

    My question is quite fundamental and perhaps I just need to do more learning, but it is difficult to find the documentation to read. I tried creating a C# Class Library and executing the object using ActiveXObject in JavaScript (JScript?) but the ActiveXObject cannot create the object. If I need to use a service reference instead then I will read about that; is that what I need to do?
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 6:31 PM

Answers

  • Thank you for your interest.

    First, I don't trust that paxScript.NET thing. One problem is that I don't see installation instructions for it. Another problem is that the paxScript.NET forum does not work. Also, I was hoping for a solution that did not require additinal software.

    Microsoft provides a relatively low-technical solution for installing scripts into the IE context menu yet they do not provide a way to use .Net for that. It surprises me. I could do a BHO but I am surprised that the only solutions are so relatively technical. The remark from webJose "I'd do it in unmanaged C++" is exactly what I was not looking for. The alternative to build a COM Object in C# is worth knowing but it also is more technical than I was hoping for. It is not that I cannot do it, it is that I was asking for something less technical. Some people would argue that a COM object is an answer, so I will just be stubborn about that.

    Instead of getting more technical by using a BHO or a COM object, I realized I could use a WebBrowser control for the web site and do everything I need to do. I have developed a C# Windows Forms application and I use the menu in the form to do what I originally intended to do from the IE context menu. It is easy enough, and I don't have to do anything about security that I seem to have to do if I were executing in an IE security context.

    I am not developing this for anyone else, but when I do develop something for a client, I hope they appreciate getting the least technical solution. Technical developers should be competent enough to know when to be technical and when to not be technical, and customers will usually appreciate that.


    Sam Hobbs; see my SimpleSamples.Info
    • Marked as answer by Simple Samples Thursday, October 15, 2009 3:18 AM
    Thursday, October 15, 2009 3:16 AM

All replies

  • Afaik you can not load your C# Class Libary in a javascript as an ActiveXObject.
    It needs to be a COM library.
    Maybe if you use a SilverLight application (cause that would be able to execute C# code clientside) instead of JavaScript.
    But since i'm a Silverlight noob i can't tell you how to do that :)


    Regards,

    Ruud van Falier

    ---
    Owner @ dotTech
    Senior Software Engineer @ Colours, The Internet Company
    Monday, October 5, 2009 6:56 PM
  • As it has been pointed out already, you need to create a COM class library.  But furthermore, you'll probably have to mark the COM class as safe for scripting.  Otherwise IE will stop the script on the spot.

    But maybe what you want can be accomplished in a more elegant manner by creating a Browser Helper Object?
    MCP
    Monday, October 5, 2009 7:12 PM
  • Maybe if you use a SilverLight application (cause that would be able to execute C# code clientside) instead of JavaScript.

    Unfortunately the documentation of IE context menu items is vague. It seems to only support HTML files and the HTML files consist entirely of script code. It is not clear what other types of files are supported, if any. So without documentation, I assume I can only use script code such as JavaScript and VBScript. As far as I know, C# cannot be used as a script language in a desktop HTML file; C# requires use of ASP. If that is not correct then it would be great to put the C# code int he HTML file.
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 7:28 PM
  • As it has been pointed out already, you need to create a COM class library.  But furthermore, you'll probably have to mark the COM class as safe for scripting.  Otherwise IE will stop the script on the spot.

    But maybe what you want can be accomplished in a more elegant manner by creating a Browser Helper Object?

    Is a COM class library the same as a C# class library? If so, then if I mark the COM class as safe for scripting then could it be created (used) using ActiveXObject?

    Perhaps a BHO would be easier, but I want to know how to do it this way if possible. If there is any way other than a BHO to execute C# (CLR) code in a web page without a server (without ASP) then that is worth knowing.
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 7:41 PM
  • A COM class library is different from a C# class library.  Since you are not familiar with COM, this route might be a little hard for you, but again, a BHO is also a COM object, so I leave it to you.

    Basically you create a regular class library and mark it to be COM-visible.  Then you create your objects, but they must be based on interfaces.  Furthermore, each interface requires a unique ID, and each method and property will require a unique ID number.

    After that, you must give your COM-visible classes a unique ID similar to the interface ID.

    Finally, to make it work, you need to deploy the DLL in the target PC and register (typically using regsvr32.exe), but there's a catch in .Net:  The DLL must be installed in the GAC and therefore be digitally signed, or must reside in the same directory than the application that uses it.

    I found two links for you:

    MSDN Online
    CSharpHelp.com
    MCP
    Monday, October 5, 2009 8:01 PM
  • I think the CSharpHelp.com link goes to the same MSDN page as the MSDN link.

    Thank you for the links and such. It seems that the CLR cannot be used in a script without a compiler; in other words, the C# language cannot be executed by a translator/interpreter such as cscript.exe in the manner that VBScript can be.
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 8:16 PM
  • A COM class library is different from a C# class library.  Since you are not familiar with COM, this route might be a little hard for you, but again, a BHO is also a COM object, so I leave it to you.

    Basically you create a regular class library and mark it to be COM-visible.  Then you create your objects, but they must be based on interfaces.  Furthermore, each interface requires a unique ID, and each method and property will require a unique ID number.

    After that, you must give your COM-visible classes a unique ID similar to the interface ID.

    Finally, to make it work, you need to deploy the DLL in the target PC and register (typically using regsvr32.exe), but there's a catch in .Net:  The DLL must be installed in the GAC and therefore be digitally signed, or must reside in the same directory than the application that uses it.


    You are assuming I am not familiar with COM. I understand it as far as the material I have in my COM And ActiveX.
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 8:21 PM
  • Somehow the COM And ActiveX link go messed up and I cannot edit my post, so the following is the address that should be there:
    http://simplesamples.info/Windows/COMandActiveX/index.php
    Sam Hobbs; see my SimpleSamples.Info
    Monday, October 5, 2009 8:23 PM
  • Sorry for the repeated link.  The missing link is:  http://www.csharphelp.com/archives/archive281.html.

    And if I were to take the BHO or ActiveX paths, I'd do it in unmanaged C++.
    MCP
    Monday, October 5, 2009 8:26 PM
  • And you got me thinking with one of your replies, so I search the net and found:  http://eco148-88394.innterhost.net/paxscriptnet/.  It is a C# interpreter.  You can also find another interpreter in CodeProject.com.
    MCP
    Monday, October 5, 2009 8:32 PM
  • Hi Sam,
    Are webJose's links helpful for you? any feedback is appreciated:)

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Thursday, October 15, 2009 2:20 AM
  • Thank you for your interest.

    First, I don't trust that paxScript.NET thing. One problem is that I don't see installation instructions for it. Another problem is that the paxScript.NET forum does not work. Also, I was hoping for a solution that did not require additinal software.

    Microsoft provides a relatively low-technical solution for installing scripts into the IE context menu yet they do not provide a way to use .Net for that. It surprises me. I could do a BHO but I am surprised that the only solutions are so relatively technical. The remark from webJose "I'd do it in unmanaged C++" is exactly what I was not looking for. The alternative to build a COM Object in C# is worth knowing but it also is more technical than I was hoping for. It is not that I cannot do it, it is that I was asking for something less technical. Some people would argue that a COM object is an answer, so I will just be stubborn about that.

    Instead of getting more technical by using a BHO or a COM object, I realized I could use a WebBrowser control for the web site and do everything I need to do. I have developed a C# Windows Forms application and I use the menu in the form to do what I originally intended to do from the IE context menu. It is easy enough, and I don't have to do anything about security that I seem to have to do if I were executing in an IE security context.

    I am not developing this for anyone else, but when I do develop something for a client, I hope they appreciate getting the least technical solution. Technical developers should be competent enough to know when to be technical and when to not be technical, and customers will usually appreciate that.


    Sam Hobbs; see my SimpleSamples.Info
    • Marked as answer by Simple Samples Thursday, October 15, 2009 3:18 AM
    Thursday, October 15, 2009 3:16 AM