C# "Assembly.LoadFile" & disable "DllImport" in loaded assembly? - c#

In C#, Is it possible to dynamicaly load a .NET library at runtime with using something like System.Reflection.Assembly.LoadFile & disabling the loaded library from using [DllImport("someCPP.DLL", EntryPoint ="someFunction")] so that you can't call c++ stuff for security reasons??
The reason for this question is i'm thinking about making a generic openSource browser plugin that could run any .NET code & display things like OpenGL or Direct3D or whatever content you want in the browser using .NET. This would need the loaded .NET library to be secure though by disabling "DllImport" & maybe some other things. Is this possible?

You can use an application domain with restricted security permissions.
Here is a general introduction to application domains:
Application Domains (C# and Visual Basic)
And here is the specific permission related to executing unmanaged code:
SecurityPermissionFlag.UnmanagedCode

Related

How to add plugin for Excel Online?

I would like to use Excel functionality from a browser. I currently have an excel based Microsoft office application. It adds an icon to the ribbon and does C# based custom application logic to communicate to a database server.
Since it requires application releases for any changes in the schema I am curious if I can convert this into an web application and still benefit from Excel's built in functionality.
I am expecting this to be possible since Microsoft Excel Online is run inside a browser. My question is, is it possible to add plugin to such online excel document? If yes, any example would be much appreciated.
You absolutely can, and depending on approach you can do so with nothing more than a VSTO project template. This is little more than a ClickOnce application. This can launch a browser within a window to perform the logic.
You may use the Office Interop assemblies to decouple your application from Office apps, yet still communicate easily. Make sure you set Embed Interop Assemblies to true once added as a reference.
For those using Office in the browser, it seems as though Microsoft is shifting into CSOM/JSON/REST, meaning this solution is coded entirely differently than COM. This should help you get started:
https://msdn.microsoft.com/EN-US/library/office/dn268594.aspx.

how use java applet in asp.net without installing java runtime?

i am new in java applet ...
where can i learn more about java applet (related to asp.net) and is it possible to use java applet in asp.net without installing java run time ?
mean is there a library for java on the net that we can refer it ?
(how ?)
for example how can i use below java applet codes without forcing my users to install some thing on their machines?
How to find client computer name in java script/jsp?
mean JĂșlio Santos answer
thanks in advance
The example you mentioned uses ActiveX (built into IE) and JScript (also built into IE). You do not need the Java Virtual Machine to be installed for that.
In general, running a Java Applet in ASP.NET does not make sense because Applets are client side (they run in the browser) while ASP.NET is (mainly) server side. You can use IKVM to compile the Applet into something .NET understands but that basically gives you access to its public methods which may or may not help depending on your use case.
Here's how you can have IE auto install the JVM runtime: http://java.sun.com/developer/technicalArticles/JavaLP/javawebstart/AutoInstall.html
You cannot use Java without having the runtime installed.
The easiest way to do so, is to let the user navigate to java.com.

Embedding a Jar into a C# Form

Ok this situation is a bit difficult. But I am trying to embed a Java Jar into a C# form. Not as a new window or new process.
The Jar will be a game that uses the LWJGL library.
The C# Form will be a "wrapper" for it with tools and more.
Anyone have any ideas? Is this even possible?
According to the IKVM Home Page
IKVM.NET is an implementation of Java
for Mono and the Microsoft .NET
Framework. It includes the following
components:
A Java Virtual Machine implemented in
.NET A .NET implementation of the Java
class libraries Tools that enable Java
and .NET interoperability
One would approach it from using a DLL not a jar. Also this sounds like a bit of a security issue.
They're completely different runtimes; you'll need a process for both the CLR and the JVM; no way around that.
You can try the Java to C# converter provided by MS, and then rebuild: http://www.microsoft.com/downloads/details.aspx?FamilyID=46bea47e-d47f-4349-9b4f-904b0a973174&displaylang=en

C# plugin for Google Chrome

Can I communicate to Google Chrome in C#?
For writing a chrome plugin for example.
<Spoilers>
Quick short answer: No, because Extensions require JSON, JavaScript, and HTML.
</Spoilers>
Hi Tony,
There are multiple templates on the internet that you can download to build a chrome extension project using Visual Studio.
Downloading one of these templates just gives you the files and folders that you need which I call "the setup".
That won't let you build a Google extension using C#.
Andrey mentioned that there are libraries like Bridge.NET that will compile your code into HTML/JavaScript. That will help you write HTML and JavaScript using C#. You still need a manifest.json file.
I don't recommend that. That library is not designed for Chrome Extensions. Also, you will have to compile the code into JavaScript and store that JavaScript code in a JavaScript file. I recommend using HTML/JavaScript with no compilers when building your Chrome Extension.
You need to keep in mind that there are 3 main parts in a chrome extension. These are:
manifest.json
A JavaScript file
HTML file
There are multiple steps and it's not hard to build a google chrome extension. This link will give you the complete tutorial on developing a chrome extension with detailed explanation. This tutorial installs a template so that you can develop it in Visual Studio just like I mentioned before.
What I have done to address is use Simple Message Host, it will trigger an executable on the local machine that you code in c#, sending stdin messages and listening to stdout messages so you can build this host to use as a bridge, but like I said, it needs to be on your local network at least, and you have to do some editing in the windows registry, so it has its limitations.
But for the system I am working with, this solution worked perfectly because I have a controlled environment that I can set up all these prerequisites.
So, just to clarify, what I did here is:
Create a chrome extension with background.js opening up the listener to the website's javascript.
Add a registry in windows registry pointing to the path of the executable.
Create the executable in C# doing all your logic.
Send a response from the executable to the extension and then back to the website.
There are several guides on how to do this, so I won't detail these steps here so I don't replicate it.
But for the moment, it is the best way to do what you want, if you have control of your environment that is.
So, if your plugin (extension or chrome app) will work on a controlled environment, this is the way to go, otherwise, I would consider something related to ClickOnce perhaps or WebAssembly but that's not fully released yet.
Chrome own extension manager supports extensions written in js and html.
that said, to execute c# code within the extension you have two options:
Compile c# code to javascript code which then can be added as a normal javascript extension to chrome (take a look at scriptsharp)
Use c# as a back-end system. just like most of download managers:
for case 2 you need a c# application installed in client device(or in the cloud) listing to a specific port (using httplistener or self hosted webapi (you can use netcore) which do these steps
Listing to incoming requests
parse request data eg. json and do something with it
return the result to javascript extension which can display it to user or do other things with it.
The topic is quite old, but I'd like to share that sample:
https://github.com/Retyped/Demos/tree/master/ChromeDemo
In a few words, the sample is implemented in C#. The Retyped.chrome NuGet package provides bindings (Chrome API) for Bridge.NET projects. So yes, now you can implement your logic in C#, then C# code will be transparently compiled into JavaScript by Bridge.NET compiler.
With that approach you can build your Chrome extension on top of .NET Framework as well as utilize thousands of JavaScript libraries.

C# - Possible to use Subinacl or something else (an api maybe?) from C# code?

I've created a program in C# which creates users and adds them to groups, everything is working fine. But I also want to create a "home folder", which is on another server, and the share will be like this: 81file01/users/username. And of course set the rights of the folder to the newly created AD-user. Now we're using a vb-script to do this, and this part is done with Subinacl, but is there a way to do this through my c# code?
I'm using .net 3.5 by the way :)
You can shell out using System.Diagnostics.Process and just call Subinacl directly as you do now.
Or you roll your sleeves up, and get your hands dirty calling what appear to be mainly a Win32 set of APIs. The friendliest article on that subject I could find from Microsoft talks about using the COM interface via COM interop.
There are some wrappers to these APIs floating around, but how good they are, I don't know.

Categories