C# - generate DLL linked in console App - c#

I have two C# Applications
TestApp is a console app which call a method decrypt included in AppCrypt
AppCrypt contains this method and needs to log (write on a file)
So far I can modify TestApp and I saw the result on my console but my goal is to edit the code of AppCrypt
I am using VS for both.
Question 1) I cannot find where is written the place where I link the DLL of AppCrypt
Question 2) I can build AppCrypt but I cannot generate a new DLL because I am getting this message
I found on SO that I should add a project Console App or a Winform but I just need to use that function without having a proper app so I dont know if it is my case.
I have built the DLL but it does not seems to be linked in my console app which is TestApp, how can I verify that it is linked?

You are getting that error cause most probably you have set your class library project as Startup project in your solution. Whereas you should set the Console app as your start up.
How to Set a project as StartUp: Right click on your project name and from context menu select Set As Startup Project

Related

Where can i run my code in visual studio?

So, i am trying to write my first C# "Hello World" Program in visual studio, however when i want to see the output of the code (or simply just run it), i can't because i can find the run option, i have tried ctrl + f5, and many other options, however it didn't worked any suggestions? Here's a picture of my visual studio.
Check this guide Tutorial: Create a simple C# console app in Visual Studio
Open Visual Studio 2019.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
In the Configure your new project window, type or enter Calculator in the Project name box. Then, choose Create.
And then to run:
Choose the green Start button next to Calculator to build and run your program, or press F5.
This is because you created empty solution. You don't have any output, i.e. process of compilation of your solution does not produce any executable (exe) or library (dll).
In order to run any code is to produce one of the above. In order to do that you have to have project created.
To do so, you have to (in your case) right click solution and choose "Add -> New Project" option. This will show you pop up allowing you to choose which project you want to add, choose just basic type of project, which is "Console app". There you will have class with static method Main which is the entry point for your app.
Code inside Main method will be executed.
You haven't created a project. You've got no executable and no ability to build one. I don't know if you've worked with other languages before or not (perhaps ones which are interpreted and/or have no specific structure to them), but C#/.NET programs require a particular structure in order to build and execute. You can't just create a random C# file on its own and execute it.
Instead, create a new Console Application project (from the File -> New Project menu) and give it a name.
That will auto-generate a Program.cs file, which contains the Main method which is the entry point of your program when it's executed. From there you can write code in there, create other methods in that class if you need to, and you can also add other files containing classes etc. into your project.
(There are, as you'll see, also quite a lot of other project types you can create depending on the kind of application you want - and also some types which are just for code libraries and not full applications.)
How to: Run a C# program in Visual Studio
The project should contain a C# file with a Main method, and its output should be an executable (EXE), you have created an empty solution with no such files.

Generate Winforms Project .exe programmatically from within a Winforms Application

I have a requirement to build a container C# WinForm Application which will spit out .exe files of another WinForm Application on a button click event.
For eg: I have one Winform App named ProjectA which accepts one startup argument. Now I have a container WinForm App named ProjectB. I want to generate ProjectA.exe programmatically within ProjectB by passing the required parameter to ProjectA app on the button click.
Could not find anything relevant about this on Google. Can anyone throw in some light to achieve this.
Please note that both the Winforms Application must be written in C#.
I have one very vague thought of using MSBuild Command to build ProjectA which in turn will generate its .exe
However I am not too clear on this.
It is not necessary to mess with MSBuild for this. You may generate your program's code as a string and then generate your executables on the fly:
https://support.microsoft.com/en-us/help/304655/how-to-programmatically-compile-code-using-c-compiler
As an example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln");
Another Example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln /t:Rebuild /p:Configuration=Debug;TargetFrameworkVersion=v4.0");
put the above code on your button click.

Debugging Topshelf Service

I am using Topshelf to create a Windows service which uses the chrome Selenium driver to manipulate and parse various webpages.
While the service code runs fine from a normal (NetCore2) console test application, there's a problem when it runs under the Win32 console app that Topshelf requires.
Unfortunately, I haven't been able to figure out how to debug into the underlying service code. The VS2017 debugger appears to be attached to the Topshelf console app, but won't let me step into the underlying service code, which is in a separate NetStandard2 class library. Specifically, when I come to this line in the Topshelf console app:
var scanEntry = _scanner.Run();
where _scanner is an instance of the object that does the actual scanning, I can't step into the code (i.e., F11 just steps over the line).
I tried inserting the following line into the class library scanning code:
System.Diagnostics.Debugger.Launch();
but it doesn't do anything; the code just keeps merrily executing along.
I suspect this is related to the fact that the scanning code is running in a separate process that Topshelf launches. But I haven't been able to figure out how to identify it through Visual Studio's Attach to Process mechanism.
Pointers on how to debug the underlying service code when it's running under Topshelf would be much appreciated.
Additional Info
When I tried stopping on that _scanner.Run() line, and using the context menu to single step into the class library code (rather than using F11), I got prompted to turn off Just My Code, which I did. The VS debugger then tried to step into the class library code, but complained that it couldn't find the library's symbol file (*.pdb).
Which is really weird, because a symbol file with that name exists in the bin folder for the Topshelf console app.
I tried manually opening the pdb file from the bin folder, but got an error message that "A matching symbol file was not found in this folder".
Is this related to trying to debug a NetStandard2 class library from within a plain old Net console app?
Okay, turns out the problem was related to debugging a mixed NetStandard and NetOriginal code base...
NetStandard class libraries, by default, do not generate debug symbol information usable by NetOriginal apps. You have to go into Project Properties -> Build -> Advanced and change the type of debug information being generated from Portable to Full.
Once I made this change I was able to step into the class library code as per usual.
I found this at a matching symbol file was not found in this folder

What runs my C# application?

When developing a C# project in Visual Studio, I have three options for output type. Console Application, Windows Application, and Class Library. AFAIK, the only difference between a DLL and an EXE, is the EXE should have an entry point defined, and that is called when the file is double clicked. However, when I launch an EXE built from a Console Application, a console window is created. So obviously something is happening other than the CLR getting launched and then calling my Main method. What launches the console window? When I launch an EXE built from a Windows Application, is some other code run also, or is it just my main method?
Your portable executable file(exe) will contain the information about what kind of application it is.
Subsystem flag of the IMAGE_OPTIONAL_HEADER defines what kind of user interface the application is built with.
IMAGE_SUBSYSTEM_WINDOWS_CUI defines the console application, IMAGE_SUBSYSTEM_WINDOWS_GUI defines the windows application and so on.
For more information Peering Inside the PE: A Tour of the Win32 Portable Executable File Format
The output type is a configuration parameter for your project which tells Visual Studio what to do when you compiled. If set to Console Application, it will generate an exe file with the code to launch the console window.
The different between a dll and an exe is more than the main method. Visual Studio generated additional codes in the exe file that creates the console and invoke the main method. For details of how the exe file performs this, refer to http://en.wikipedia.org/wiki/Portable_Executable.
In this link the inquisitor added some notes which mentioned the blog post (2nd link).
Is it possible to build a Console app that does not display a console Window when double-clicked?
http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx
The same content as Siram's answer https://stackoverflow.com/a/30084790/2005526 but to assist you with future searches these were the keywords used on google to locate the mentioned resources. "double click exe launches console"

Merging two related projects to one executable? (c sharp)

I'm currently working on a solution with two different project. One project is a windows form (main project) and the other one is a console application. Currently I am running the console application from the main project with:
System.Diagnostics.Process.Start(Environment.CurrentDirectory + "\\console.exe");
When I debug, everything works fine as a wish. However, C sharp creates two different executables, one with the console and another with the main project. My question: is there a simple way to merge those two to only one executable? Note: I am a beginner, and I tried really hard to get this working :(
Thank you!
To merge multiple assemblys into one you can use Fody/Costura. It allows you to merge multiple assemblys(exe's) into 1 project(exe). In your main project you add it (can be done via nuget). Then just in weavers.xml (file that will be automaticly generated add these text:
<Costura>
<IncludeAssemblies>
MyProject.MyConsoleApp
</IncludeAssemblies>
</Costura>
(it should be the same name as the reference, so add the console app to your windows form as reference,and remember don't add the .exe suffix)
PS: read the tutorial on the link I gave you.

Categories