My .NET application is 32-bit because I use TWAIN inside it. But I need to write 64-bit library or app and call its methods from my program.
I must create a 64-bit library to interact with WMI SMO, and its functions don't work properly if it compiled as 32-bit dll.
How can I resolve this issue? Is it possible to use a 64-bit library from a 32-bit application?
You cannot reference a 32-bit library from a 64-bit executable and viceversa. This is a technical limitation.
However, depending on your other constraints, you may decide to employ a service-based architecture to avoid issues like yours and be more agile in the future.
In fact, you could write "micro" services specialized to solve one single purpose (scanning, interacting with WMI, etc...) and then call them from your application.
This way, you are completely free to choose whichever technology/architecture you require to achieve every single task your application requires. Isolating your "features" in services, allows you to have a different evolutionary path per service, as each service is loosely coupled with each others.
This solution requires writing more code than using a library. First you need to create a windows service (msdn) to
wrap your code, then you need to implement the communication channel. Have a look at WCF and WebApi2 to have an idea of what you can achieve.
Related
I am trying to follow this tutorial:
https://codingvision.net/security/c-inject-a-dll-into-a-process-w-createremotethread
but kernel32.dll and its functions can only be used on windows.
What can I use instead to inject dlls on mac?
If you are injecting kernel32 then it means that you are actually injecting a native library designed for Windows. There is no 1-to-1 alternative apart from possibly ones within libraries like WINE, but avoid such hacks.
Instead consider finding an alternative in the API of the actual system. You should find the respective method in the API of the system which you are currently running and conditionally execute different calls.
Yet be sure to know that the best approach would bo to AVOID using direct system calls and operate only within .NET, especially that if you find a way to execute required things only using .NET libraries then there is a high chance of migration to .NET Core which is designed to work on all three major systems without a problem (especially for web and console applications).
So to sum up:
there is no kernel32.dll for MacOS
you need to find a respective function in the API of MacOS which will do the same as the method which you have called from Kernel32
the best thing is to avoid usage of Kernel32 and try to find a respective call within .NET libraries
Once upon a time you could simply use the Mach call task_for_pid() but that stopped working years ago when Apple first started paying attention to security. Then for a few years you could still force the dynamic linker to load a .dylib into an executable when it launched by setting some environment variables, but then Apple put a stop to that too, as they continued to crack down on security holes.
For the most part you can't do this anymore, or at least not easily. Especially with things like System Integrity Protection enabled. (I mean you could still create a kernel extension and do it there, except Apple now requires that all kernel extensions be signed with a special entitlement and they're pretty much not giving out that entitlement anymore.)
I recently learned that AppDomain is not fully supported in .NET Core, and they have no plans so far of implementing full support.
What I am trying to do is to make a program that can run a plugin, but I don't want that plugin to be able to access certain assemblies or namespaces (for instance System.IO).
The way I used to solve this problem prior to .NET Core will no longer work due to the lack of support.
Is there any other way I can achieve the same in .NET Core?
More concrete example
Let's say I load an assembly using Assembly.LoadFrom from the file system, which contains a plugin method that I then invoke. But I don't want plugins to be able to erase files, etc. In fact, I only want the plugin to be able to call functions from a specific assembly.
Basically they want you to use the platform boundaries for the environment you are developing for.
Sandboxing
Why was it discontinued?
Sandboxing, i.e. relying on the runtime or the framework to constrain which resources a managed application can access, is considered a non-goal for .NET Core. Sandboxing applications and components is also really hard to get right, which is why generally recommend customers not to rely on it. It also makes the implementation more complicated and often negatively affects performance of applications that don’t use sandboxing. Hence, we do not offer sandboxing features in .NET Core.
What should I use instead?
Use operating system provided security boundaries, such as user accounts for running processes with the least set of privileges.
https://blogs.msdn.microsoft.com/dotnet/2016/02/10/porting-to-net-core/
I would like to know how to allow my C# application to be used from others' own applications.
Is making the relevant classes public enough for this purpose? Shall they be able to put a reference in their projects to my .exe and use my public classes freely? Shall they only be able to use it from .NET applications?
What else should I take into account? Any security issues maybe?
Your .Net dlls can be used by other .Net applications. You can separate the logic part of your code from the interface, and put the logic in "Library" projects that will be compiled as Dll files that can be used in a .Net application by adding references to them.
If you want to allow non .Net apps to used you can use COM Interop:
Wikipedia - COM Interop
COM Interop C# Tutorials
You can also use WCF services as CSharpVJ says.
Create a wrapper WCF service and expose your exe application thru' those WCF services, and it will be available to almost all clients based on even other platforms like Java, Python, Ruby and more depending on which settings you use inside your WCF Service..
WCF provides a service based model to communicate with other applications based on .Net and other platforms.
WCF is explained at this article in MSDN
You could always just make the sourcecode public on https://github.com/ for example.
That would other people allow to view and edit your sourcecode compeltly, which might be even better than a WCF service
There is a Java library running on windows machines that needs to log information about the OS like CPU load, memory occupied by the JVM etc. which I'm quite sure Java itself cannot obtain, because it is OS specific.
This information is needed in the logs of this library in order to point out to clients that certain operations failed because the library could not obtain enough resources.
It is not possible to choose the JVM, i.e. we cannot demand that our clients should use a specific JVM that does implement windows OS specific functionality.
Is there a windows library (DLL) or API that could be used via JNI?
We could also implement a DLL in C++ or C# ourselves, where would I need to look to see how this could be done most effectively?
Edit: I need access to data about the process of the JVM itself, which I can get through the native Windows API only, I guess. So I think I'll have to implement a small C program and link this as native DLL via JNI. Any tips on that?
For the memory part, look at several memory related methods of the Runtime.
To get the occupied memory, try the following:
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.totalMemory() - runtime.freeMemory());
I have not tried this yet, but for the CPU load take a look at the following.
The getSystemLoadAverage() method should do it.
Hope this helps!
If your needs aren't met by java.lang.Runtime, you could go the JNI route and design a Java API that provides exactly what you need.
I don't know of any out of the box DLLs that you could wrap (with JNI) but the data you're after is in the Registry as the so-called "performance counters". On linux you could read the data from /proc.
See the MSDN site for more info on Performance Counters.
Back in the day, I wrote an out-of-process (i.e. EXE) COM server using VC++ 7.x (Visual Studio 2003 and .Net 1.1) using the ATL library. The server used the Multi Threaded Apartment (MTA) model and is activated by DCOM calls (VB6 clients). Multiple client requests are satisfied by a single process -- which is activated by the first request (automatically by the COM infrastructure).
It was quite a complex piece of code and I now have to make some changes to it. Unfortunately, the machine I built it on is long gone and I'm having problems even compiling the project on a new machine with VS 2003. A few years ago, I toyed with moving this project to VS 2005, but there were a lot of breaking changes to the ATL library and I gave up on that effort.
Rather than tackle with my current problems building this old VS 2003 C++ project, I'm toying with rewriting the thing in .NET. This will make it much more maintainable at my small shop (where we don't do C++ anymore). The ATL macros and C++ idioms are long since gone from my memory, and I'd like to keep them there. :-)
So I'm exploring how feasible it is to do this rewite in C#/VS2008. I know you can expose .NET classes to COM clients. Indeed I've done so with some simple stuff in the past. However, this is much more complex. I'm not sure of a number of things:
The interfaces are all defined in a type library that is consumed by the clients (that must remain unmodifed). May I assume that I can build a .NET server that implements these interfaces based on an existing type library?
The server implements a number of interfaces that inherit from IDispatch and are marked with "dual" and "oleautomation". As an example:
[odl, uid(...), dual, oleautomation]
interface IKDFTSearchManager : IDispatch {
HRESULT Method1(...);
HResult Method2(...);
}
I don't believe that any of the clients use the IDispatch methods, but I would assume that the VTable generated by interop must match up. How would one expose this from a .NET server?
What type of project should house the components? A console application?
DCOM would presumably activate the EXE if it is appropriately registered with one of the interop tools. Is that the case? Would the infrastructure behave as it did with the C++/ATL server -- i.e. cause a single EXE to be activated that serviced multiple client requests?
Related to (4), would this server be using the Multi Threaded Apartment model?
To begin with I would recommend reading this (Ever done a total rewrite of a large C++ application in C#?)
You should look into ServicedComponent which is the official way of implementing a DCOM server in .NET.
The project will be a class library that will hosted by the COM+ you can host the project as service also again you don't need to write the host COM+ will take care for that.
You can reference you old tlb (but I would recommend writing p/invoke interface inside your project) and implement the interfaces.
You will need to use ProgIdAttribute to keep the old one.
I come from the similar background, and I can image there will be significant efforts to do this especially if you don't want to touch the client at all.
There is .Net sdk tool called Tlbexp.exe which will export the type library for you on .Net assembly. I assume you need use this tool to run against your new c# assembly (dll) to generate the same type library as your old COM server.