Injecting a DLL into a Target Process VB.NET - c#

I recently wrote a quick VB.NET app that injects a DLL into a running process. To test it I was creating my own vb.net Class Library project which simply spawns a "Hello World" message box in hopes of it showing up in the target process once I injected my HelloWorld.DLL.
My problem is that the message box never shows up after I inject the HelloWorld.DLL. I'm pretty sure the reason for this is because once my HelloWorld.DLL is injected (since it's a VB.NET DLL) it does not have a DllMain and hence has no idea what to execute and nothing happens.
Do I have to make my injection DLL in C++ so it has a DllMain? Is there anything I can do as a work around? Or am I completely wrong about everything.
Any insight would be greatly appreciated. Thanks.

While the .NET DLL is technically an extension of the PE format it is that extension that makes it intrinsically different to a DLL that contains pure compiled, native code. In order for the .NET code (managed code) to be run is will need to be executed by the .NET interpreter and withing the context of an AppDomain.
Essentially there is a load of stuff that .NET will do to get that code up and running.
Microsoft (bless 'em!) have written and article outlining what you need to do here http://support.microsoft.com/kb/828736
Another option is to not write pure C++ code, but instead to create a managed C++ project which will be much easier in getting the two to play nicely together. BTW having a managed C++ project doesn't mean all the code has to be managed either AFAIK

Related

How to inject a .Net DLL into an unmanaged process?

After our senior developer left last week, I've been assigned to a task in which I need to inject a dll written in C# and invoke a method into another process which is not made using .Net, it's written in C++.
I'm not a C/C++ guy and honestly I do not understand half of what I read till now.
Searching for "How to inject a .Net DLL into an unmanaged processes?" and queries like this only brings one practical article on code project which I have difficulty to follow due to so many errors.
How can I do this without thinking about suicide?

Creating dll library from generated c code

I have a control problem which i solved by using Model predictive control (MPC). I have stated the problem in MATLAB and used FORCES http://forces.ethz.ch/ to solve it. FORCES is a web service from ETH Zürich which generates library-free ANSI-C code. For testing the code they provide a script to compile the c-code to a MATLAB .mex file. This all works great.
I now want to use this function in my C# Windows Forms programm. To use C-code in C# I have to compile the c-code to a dll and then use P/Invoke? Or is there an other way? How can I compile it? Can I just use the code as it is or do I have to edit it (adding __declspec or something like that)? Is it possible to compile the dll directly out of MATLAB?
The .c and .h files can be found on my homepage: http://n.ethz.ch/~rehofman/download/.
I run 64bit Windows 7, MATLAB 2013b, Visual Studio 2012.
I have MinGW already installed if that is needed.
EDIT:
Ok here is a more specific question: I have a function(myMPC_solve) written in c-code which I want to call from c#. What is the best way to do so? The arguments and datatypes are all in the .h file.
You could do any one of the following:
Compile the C code to a DLL, and include it using p/invoke.
Compile the C code into a C++/CLI mixed mode assembly, wrap the functionality in a ref class, and consume that from C#
Wrap the C code with a COM server and consume that from C#.
Leave the code in MATLAB and consume that from C#. There are multiple ways in which MATLAB code can be consumed by C# code.
And I'm sure there are other options beyond the above.
Now, if you are hoping for a detailed step-by-step instruction of what to do next, I'm sorry to disappoint you. You asked a broad question and I've given you a correspondingly broad answer. What you should do next is to pick one of the options above, and implement it. When you get stuck, do feel free to ask a more specific question.

Read C# functions in C++ project

Good day,
I want to take an existing C# project and wrap functions into a C++/CLI DLL. I need to be able to read this DLL from VB 6. I choose this route because I won't have to register a .net DLL in order to use it with VB 6. Frankly I have no experience with this kind of thing so I would greatly appreciate a good example. I know there are plenty of similar questions like this but I haven't been able to find anything simple enough for me to understand.
This is a fairly unwise route to pursue, making C# code [ComVisible] is pretty trivial and Regasm.exe should not scare you. The usual mistake with Regasm is to forget to use its /codebase option on your dev machine.
If you insist on not taking advantage of this then you'll need to find a way to get the CLR loaded yourself so it can execute your C++/CLI and C# code. There are three basic ways to do so. You've written off COM interop and hosting the CLR yourself isn't very practical if the host app is VB6. You however can take advantage of the C++/CLI compiler's ability to generate unmanaged DLL entrypoint stubs that load the CLR for you and switches to managed code execution. Do so by writing a static function that you decorate with __declspec(dllexport). The technique is shown in this answer. Beware that it isn't particularly performant and you'll have to live with the restrictions imposed by the VB6 Declare statement.
Also check this post-processing tool that can inject these stubs directly into a C# assembly. Not sure how reliable it is, I haven't used it myself.
Be a man and make that DLL under a couple of different compilation/linking/function-call-protocol configurations. Then go ahead and try a couple of different referencing configurations in your VB6 project. Post what works. Contribute, go forth!
Take a look at Export Managed Code as Unmanaged
and C# Project Template for Unmanaged Exports

How to handle C# - C++ project interaction the right way?

I´m really in the need of getting a deeper understanding of how to set up things right to get an elegant interaction between my C++ and C# code bases.
What I want to achieve is an in-game editor written in C# for my game engine (C++/DX). For doing so I let VS build my engine as a C++ dll with some additional functions (unmanaged code) to access the required functionality of my engine from the C# editor code base. So far so good.
The first thing which is bugging me is that I´ve to build the dll with CLR support. Otherwise C# does not accept the dll for some reason. It doesn´t even allow me to add it to the resources ("A reference to 'C:\Users...\frame_work\Test\frame_workd.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.").
And when I build the dll with CLR support and add it to the references in C# ,re-build without CLR support, start my editor and make a function call from the dll then I get an Exception HRESULT: 0x8007007E. I searched for it but the only thing I found had to do with dependencies but that doesn´t fit to the alert I get when adding the dll to the resources.
The other point is that I always have to switch the configuration type between application (.exe) and dll. in VS C++ depending on whether I want to run my engine directly or from the editor and every time the complete project is build completely new.
So, could someone explain to me how to organize this the right way? And what could be a possible reason why C# wants the dll to be compiled with CLR support?
Thank you guys/girls.
There are two ways to deal with this.
Either you make your C++ code provide an API which has a fully compliant COM object. If the object is COM then C# can directly interop with it. (This is why you can't add it as a reference directly)
However I think what you are really wanting to do will involve a P/Invoke (calling C/C++ native code from C#). This is entirely possible but it's not always easy. You need to deal with conversions between your C++ API and your C#, pointers and you need to be very careful to pin any references that your C++ code writes to in the C# app.
C# code is managed code (runs in the CLR), and can only directly* reference managed assemblies. So of course you're getting an error when you build against a managed assembly, and then sneak in and replace that managed DLL with an (incompatible) unmanaged DLL. You're basically trying to lie to the compiler, and that generally doesn't end well.
If you want your C++ DLL to be accessible from C#, the simplest way to do it is to build it as a managed assembly (i.e., CLR support). Which you're already doing. Just take out the extra step where you replace the working managed DLL with a non-working unmanaged one.
Also:
C++ dll with some additional functions (unmanaged code) to access the required functionality of my engine from the C# editor
That won't help you, because C# can't directly* call unmanaged code. The simplest way to make this work will be to make additional managed classes and methods in your C++ DLL. Then your C# assembly will be able to directly use those managed classes.
* As Spence noted, you can use -indirect- means (P/Invoke and COM) to access unmanaged code from C#. But that will make your life much more complicated than it is now, not to mention how it will complicate your build and deployment. You're already really close to something that should work -- don't add all that extra complexity.
When calling functions with P/Invoke, you don't add the DLL to the C# project resources (or what you probably meant, references, either).
You will add it to the file list in your MSI project, of course.

System.AccessViolationException when calling C++ from C#

I don't know why but today myOpenID doesn't seem to work. Anyway ... I have this problem: I have a unmanaged C++ library (DLL) which I have to embed in an existing C# project. Now ... I have created a mini-wrapper (DLL) in managed C++ which calls the library so that I can load it from the C# code and, when I try it from a command-line C# project, it perfectly works, right results, right behavior ecc.
Now, when I load it in the real project, it starts giving me strange System.AccessViolationException coming from the mini-wrapper DLL. I am not experienced in C#, nor in general manged/unmanaged C++ development under Windows, and I just can't understand why should this work from a C# project, and not work from another.
More information: the original library uses OGRE3D rendering engine to do calculations, and the project in which I have to use this library uses OGRE under the hood, could this cause problems?
Any suggestions?
Here are some ideas for you to try sir...
It's hard to know whats going on exactly but the first thing I would try to do is remove this managed c++ dll from the mix. It might be confusing things. Somewhere here this feels like data is not being marshalled correctly between the managed and unmanaged world. Also, just because it doesn't crash from the console, doesn't necesarilly mean the code is working correctly, it could still be breaking, just not in a way thats triggering an access violation. The first thing I would look at is using p/invoke to call your unmanaged dll directly, if it breaks, you should know pretty quickly:
Calling Win32 DLLs in C# with P/Invoke
It could be that somewhere in the mix, this pointer is being moved to a different address space where that pointer makes no sense. Are there any process boundaries here?

Categories

Resources