Dynamically create a dll file from a string or cs file - c#

The task is that the contents of the dll file becomes known only during the execution of the main program, in which I get the necessary data and form a line or cs file. Then I need to create a dll from the code I wrote in the line (let's say it's correct and compiles without errors). Then I will dynamically connect this dll to the main program and use the function from there.
The main problem is in the dynamic creation of the dll file, I did not find a working option. I am working in WPF .NET 5.0, I tried CodeDOM but there was an error "not supported on this version" so I will be very grateful for any information on how to do it!

Related

During runtime: write, compile and use C# code?

Hi I am trying to find a way of executing code generated at runtime, without restarting the program.
My current workflow:
User uses my program to create some diagrams
User presses compile and C# code is generated from the users diagrams
The C# code is placed in a code file which exists in my project's hierarchy. The program is then restarted in order to use the newly generated code.
What I'm after though is changing Step 3, so I don't have to restart the program. Something like:
The C# code is run through the C# compiler which generates a DLL.
any errors are shown to the user
The DLL is then loaded into the program
Setup some links to the codes methods, so I can pass data to it and get output data
if this would work?
Look at the CSharpCodeProvider. It is intended for compiling a code in memory.

Visual Studio 2012 C# - Importing a DLL

I am actually trying to use a DLL on a C# project. The problem is, whenever I try to import it in my project by adding it as a reference, I get this error message :
A reference to "C:\FilePath\LnkEMP.dll" could not be added.
Check that the file is an assembly or a valid COM component
The library is "LnkEMP.dll", used for a program called Expert/M Plus. I need this library to interract with this program.
I think that this DLL is made in C++, which could be one of the reason that my C# project can't load it.
I tried to make a C++ Library and importing it, but this time I had another error message :
A reference to "C:\FilePath\LnkEMP.dll" could not be added,
because it is not an assembly .NET, or not a registered ActiveX control
Do you have any idea of what should I do to get it work ?
You can fall back on dynamic loading of dll using Win32 api calls. There should be lots of examples of dynamical loading/calling of external dll. Hopefully your dll comes with doc on how to use it.

Can a Windows .exe GUI application be converted to DLL, and then run the GUI?

How can I convert a Visual-C++ GUI project from app to DLL, and then have a C# app call the DLL and run the GUI?
When I try, the " dlg.DoModal();" causes "Debug Assertion Failed!".
I've got a visual studio 2010 C++ project with Config Props set for:
"Config Type is app (.exe)
Use MFC in a Shared DLL
Not Using ATL"
When I change it to DLL, select Common Lang Run Support (/clr), I can successfully call its individual functions from my C# solution.
I've tried calling a test function in the DLL that calls
"theApp.InitInstance();"
but this generates "Debug Assertion Failed!".
Looks like you tried to convert the Win32 application into .NET one with just one switch (/clr), and this won't work (well it can work but in very simple cases and not in yours).
You still can convert the EXE into Win32 unmanaged DLL (this requires writing a couple of extra lines in .def file and in the main code), then use P/Invoke to invoke that function.
Create an empty MFC DLL project and copy all the essential code from Exe into it. Just changing he project type in Visual Studio Settings won't correctly change all the other code that Wizard generates when you create DLL app from scratch.

Calling C# from ColdFusion

I've written a .dll in C# to change the permissions on a folder. I also wrote an .exe to test the .dll and it successfully changes the permissions. Now I'm trying to call the .dll from ColdFusion, but I'm getting an error about System/Security/IPermission not being found.
I'm assuming this is an interface in C# that ColdFusion can't find in any of the available assemblies on my system. I've added the System.Security assembly to my References in the C# project. Is there something else I need to do to make sure ColdFusion can find the interface?
Here's how I'm using the .dll:
<cfobject type="dotnet" name="permObj" assembly="#pathToDLLs#CoursePortal.dll" class="CoursePortal.Permissions">
<cfset permObj.revokePermissions(dir, username)>
I never could get it to work. I switched the DLL to an EXE and used <cfexecute> to call it. It's working fine now. The .NET code is called so infrequently it doesn't make much difference that it's a separate app.

How can I show an error message when trying to run an exe file without its dlls?

I have a C# application which has a dll added by reference.
When I try to run the exe file from Windows (after compilation) and the dll is renamed I want the application to show an error message.
How can I do it in code?
If the exe can't find any required dlls it should already display an error. Something along the lines of:
The program can't start because example.dll is missing from your computer. Try reinstalling the program to fix this problem.
The fact that you're not getting this implies one of several things:
a) The dll isn't actually referenced by your program.
b) You've linked the dll into the exe.
c) The DLL has been found elsewhere by the CLR (thanks Pondidum)
In a .NET app you won't get an error until the first time you try to use a class in the missing DLL.
A couple of possibilities:
Write code that runs on program startup and calls dummy methods on one class in each DLL
Recursively call Assembly.GetReferencedAssemblies() and build a list of missing DLLs
Also it would be possible that your exe finds the needed assembly somewhere else. To check where it search for a needed assembly check out this article on MSDN.
Another possibility would be to use the AssemblyLoad or AssemblyResolve events to get more informations about which assemblies are (not) loaded.

Categories