Debugging C# application that is already built - c#

I have an application I made that I lost the source code for, but the application has worked on my Win 2K computer. Now when moving to a Windows 7 computer it won't start. Probably some dependencies that needs to be solved, but I don't know which. The only error I got is a System.InvalidOperation without further description
Is there any tools(free) that I can use to get more specific information on what goes on

I am not sure how/if you can debug the application, but here is another idea.
There are tool out there called "reflectors", which allow you to look inside the .NET dll or executable.
They basically do the reverse of the compiler convert the dll/exe to source code (Now it won't be exactly the code you wrote, for example some local variables might have changed their names and some things might have been optimized, but the end result is the same, since this reflected code came from the same dll/exe.)
Now you can take the output of this and use it to rebuild and then debug your application.
Here are some ofthese tool:
dotPeek - free
.NET Reflector - not free
I know dotPeek doesn't allow you to debug inside of it, so you have to rebuild your application. The other one seems to be able to do that with its most expensive licence. Good luck!

You can also use Microsoft Debugging Tools for Windows (WinDbg). Here is an article that can help you.

http://www.dependencywalker.com/ will tell you dependencies and missing ones.
Also, you can attach visual studio, or get visual studio to launch a exe in debug mode without the source code.

my first try would be dependency walker just to make sure your not
missing some piece of native code
then I would look to just attempting to attach the program to visual
studio.
My next step would be to use something like reflector to get some
version of source code.

The pro version of regate's reflector let's you debug an existing dll without the sourcecode, costs $ 138 tho.
Ilspy will let you see the source for free, maybe you can figure it out from that.
Rgds Gert-JAn

Related

I have a managed assembly and need to debug it but can't

I have an .exe which is a portable program. It is assembled with Microsoft Visual C# / Basic .NET
I managed to find out that in order OllyDBG to be able to open it, it should be running and also JIT compiler producing some code. I am a beginner and not at all understand the full process. When it's running, it doesn't create any other files near it ( though when laucnhing the installable version it creates a temporary .dat file aside it). But the portable version is the same size so I am working with it.
While it is running, anyway the debugger can't open it ( nor the installed version )... So my question is - what am I missing in order to make it readable. I just need to make a small change in the .exe but I cannot get into it. Please help me with few more details to make it happen.
If it really is a managed application built with a .NET compiler, then the assembly program is actually composed of IL code, which can more or less be reverted back into source code for you to modify. In order to do this, you can use a tool such as ILSpy or .NET Reflector.
There is a possibility that the assembly may have some kind of obfuscation to discourage decompilation, and in that case, you will need either look to the developer of the software for permission, or to a specialized deobfuscation program like de4dot.
Keep in mind, the terms of service of the application may forbid you from modifying the assembly, but this isn't a legal advice forum.

.NET C# App running good on Development PC but doesn't run on Production computer

So it's basically as stated in the title. I've created a WPF app in Visual Studio 2013 using some external libraries.
Application works flawlessly on my Development machine (Windows 8.1 x64 + Visual Studio 2013) but doesn't run at all on Production device (Tablet with Windows 8 (NOT 8.1)). App is developed under .NET 4.5, doesn't matter if I try to run Debug or Release version. The proccess just hangs a while and then closes without any errors or messages.
If anyone would know what to do or how to fix this I would be very happy.
Thanks in advance :).
The first thing too look up in this situation, would be the Windows event log. When a .NET application crashes badly, the .NET Runtime will log an event there.
Most of the time, these events will log the stack trace which lead to the crash. This will give you a hint as to what happened.
Such logs are found in the "Application" category, and the source name you need to look for is ".NET Runtime". Usually, there will be another entry with the source name "Application Error", but this one is much less likely to help you.
Another useful technique, is to add a Console.WriteLine call at the start of your program, in order to see if that line gets to run at all.
Depending on what you find using these techniques, you may also want to use tools such as Dependency Walker or ILSpy, as suggested by Mike Dinescu.
Most likely a binding error occurs at start-up but the production machine you're testing on is configured to silently report these errors and it doesn't display the typical dialog box that would inform you that the process crashed.
Don't fret though, that message would probably not help you too much anyway.
What you need to do is inspect the main executable with a tool that can generate a dependency tree and figure out which DLLs are missing. Most likely these will be native binaries, not managed. The usual suspects are the VC runtime, or MFC or ATL libraries but there could be others too. That's why you need to use a tool such as DependencyWalker or RedGate's Reflector to find all dependencies for the main executable.
It is because your external libraries are not compiled into final .exe file. So you either need to include them in the same folder or merge them into exe but this process can be quite tricky. Look for ILMerge. However it is way easier to just copy them with your exe file.

Visual Studio 2013 C# Interactive Window

Is there C# interactive window for VS 2013, or any other similar way, without installing VS 2012.
I tried C# Repel, C# Snippet Compiler and Mono Command Prompt, but, all of them don't provide the features of C# interactive window packed with Roslyn for VS 2012.
UPDATE: Found a total better alternative!!! Try CShell, it is free and open source! "A simple, yet powerful, C# scripting IDE" as they promote it.
Check my tweet which was also favorited by Scott Hanselman :)
My old recommendation was: LINQPad
Best way is to install VS 2012 and use C# interactive window provided with its Roslyn. While doing that, wait Microsoft to release C# interactive to VS 2013.
This is taken from one of the links above:
http://social.msdn.microsoft.com/forums/vstudio/en-US/0da45fe7-fbe2-4074-b52f-dc8d7c4b2ba3/c-interactive-in-vs-2013
It worked for me, so hopefully this will help the next person :)
Hi SpencerGR,
I just got upgraded to VS 2013 and wanted C# Interactive, and this is
the first hit when I searched for it, so I figured I would place my
answer here...
Didn't make sense to me that the extension from VS 2012 wouldn't work;
so I hunted down the Roslyn keys in my registry for VS 2012 and was
able to kinda figure out what was going on. After a bit of futzing
around, I finally got it working, so here's the instructions for
whoever might care to try [be warned, I'm sure this isn't supported by
MS, I take no responsibility if something happens to mess up your
computer, and so forth ;)].
Basically it was a matter of
copying some registry keys with slight modification copying some files
and this one is annoying but oh, well] there's a file you have to
change the datestamp on (I did it by opening the file in Notepad++ and
saving it out.) If you don't already have VS 2012 or you don't have
the Roslyn CTP installed you'll need to get the files somehow, and I'm
not sure if there are other registry settings needed or not, but I
imagine if you just manage to get the files out of the CTP distro it
would be enough... No guarantees of course.
Not-Instructions-Just-An-Explanation-Of-What-I-Did-I-Am-A-Trained-Professional-...-Or-Something
;)
copying some registry keys with slight modification; I replaced '11.0' with '12.0' in the keys and values, you might need to change
the paths too if you didn't install Visual Studio in it's default
location. (also posted to pastebin.com/XeP5ai8F )
Save this as a .reg file and open it to import the new keys easily:
Windows Registry Editor Version 5.00.
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\InteractiveWindows\0] "ContentType"="Roslyn C#"
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\InstalledProducts\Roslyn] ""="Microsoft Roslyn CTP" "ProductDetails"="Microsoft Roslyn CTP"
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\Packages\{c5edd1ee-c43b-4360-9ce4-6b993ca12897}] "Class"="Roslyn.VisualStudio.CSharp.Repl.CSharpReplPackage" "CodeBase"="C:\\Program Files (x86)\\Microsoft Visual Studio
12.0\\Common7\\IDE\\Extensions\\Microsoft\\Roslyn C# Interactive Window\\1.2.20906.1\\Roslyn.VisualStudio.CSharp.Repl.dll"
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\Packages\{f5199a4e-6a60-4f79-82e9-fc92a41c4610}] "Class"="Roslyn.VisualStudio.InteractiveWindow.InteractiveWindowPackage" "CodeBase"="C:\\Program Files (x86)\\Microsoft Visual Studio
12.0\\Common7\\IDE\\Extensions\\Microsoft\\Roslyn Components\\1.2.20906.1\\Roslyn.VisualStudio.InteractiveWindow.dll"
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\ToolWindows\{2d0a56aa-9527-4b78-b6e6-ebe6e05da749}] "Name"="Roslyn.VisualStudio.InteractiveWindow.VsInteractiveWindow"
copying some files; basically just need to copy the files mentioned above to the locations mentioned above; I just copied the
'Common7\IDE\Extensions\Microsoft\Roslyn C# Interactive Window' and
'Common7\IDE\Extensions\Microsoft\Roslyn Components' folders from my
11.0 install to my 12.0 install. easy peasy.
and yeah... there's a file in the aforementioned 'Common7\IDE\Extensions' directory called
'extensions.configurationchanged'; I couldn't get things to take, then
I noticed that file. so, as mentioned, I just opened in Notepad++ and
saved back out; I think I had to make a change to the file, so I added
a space. kinda felt kludgy, but I guess it beats VS taking longer to
load due to having to scan the Extensions every time you open it up.
So, that's it! Worked for me, I hope someone else may find this
useful, as it probably took longer to post this than it did to
actually get it working in the first place. ;) It's possible I missed
a step in here somewhere, since I had made a few other attempts before
I found the 'changed' file, so I could have possibly twiddled another
bit somewhere that was necessary that I don't remember. Should be a
good start tho.
I haven't tested it out fully by any means but it shows up in the
'View > Other Windows' list, it opens up just find, and it looks like
Intellisense is working too. Good luck and great code to all! :)
--- Chelle L.
You could also use LinqPad. You can use it to test C# code. It has built in support for loading DLLs and Nuget Packages. Its what I use personally and I love it.
It has a free version, but the paid version has intellisense and debugger
C# interactive finally arrived to VS 2015... You may need to install vs2015.1 to get it..
http://blogs.msdn.com/b/visualstudio/archive/2015/10/08/visual-studio-2015-update-1-ctp.aspx
Good luck... and happy interactive coding!
IWIH describes how to install it and which prerequisites you need.
Let me give you some
Additional information:
After you have installed VS 2015 and its updates (update 3 KB3165756 patch has just arrived) you can bring C# interactive to your screen as follows:
Either via the menu
View -> Other Windows -> C# Interactive
Or press Ctrl + Q and type into the quick launch box:
C# interactive
which allows you to click on View -> Other Windows -> C# Interactive with the mouse.
Afterwards, you have the C# Interactive beneath the error list. Here is an example how you can use it:
Quickstart:
To get help in this window, type #help and press enter.
To execute a statement, simply press ENTER. To type in multiple lines, press SHIFT+ENTER for each line, and for the last line press ENTER to execute the entire statement.
To print the values of a variable, type its name and press enter (in the screen shot, I have done this for variable q)

Can I debug an Exe

I need to compare few function calls and signature between my application and an working application. Here I don't mean any way to reverse engineer or access the source code of the other application , but truly need to know what are the methods , Interfaces used by the working application.
I tried attaching my application to Visual Studio and then , Start>Debug , but this doesn't provide any useful information. Any help.
Reflector Pro Visual Studio plug in can debug not only exe you write, but any other assembly ;)
you can debug code, but once its compiled, its machine language and a debugger in Visual Studio is not going to do anything at all.
In order to get an idea of what a compiled executable is doing, you can use a program like this: http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
Runtime Flow (developed by me) can show all method calls in the working .NET application without need for source code.
Couple of solutions that I can think of:
Attach the exe with VS IDE Debugger, and use PDB files to debug the exe
Use Reflector to point towards your exe, and check the source. See example.

How to convert C# to DLL to hide the source code?

How can I convert my C# code to DLL file in a way that the user of DLL can’t view my source code?
When I make DLL in the way I always do by making a class library project, importing my classes and compiling it, the source code can still be viewed.
I believe you are looking for an obfuscator. This is a tool that will take a compiled DLL and rewrite the code with the intent of it not being meaningfully decompiled by another user. Visual Studio comes with a free Dotfuscator
Note, this will not actually prevent people from looking at your code. They will instead be looking at a very weird translation of your code. There is no way to prevent people from looking at decompiled versions of your code in C# or any other .Net language for that matter.
This is not something that is unique to C#. It is fact a flaw of every language in existence. It's perfectly possible to decompile C code. The difference though is it's much easier to maintain a lot of the original code structure when decompiling managed languages (.Net and Java for instance) because the metadata maintains the original structure.
obfuscation is what you want to search for.
There is a free one (that is limited) in visual studio called Dotfuscator.
Uses fancy methods to rename your code and alter flowpaths to obscure it.
Consider using an obfuscator.
If you are developing desktop applications converting your code to Dll will not hide the it( there are many tools to decompile the dll or exe files).
but if you are using Asp.Net, then you can compile your site to Dll, and the code will not be visible in the aspx pages, it will be compiled to Dll, you can do that by right click on your project on solution explorer, then choose Publish website
But in all cases .Net Exe files and DLL will be easy to decompile and extract the source code again, unless you use tool to obfuscator your code.
If you mean, the end-user can view your source code by decompiling it, you can protect yourself using an obfuscator.
There is standard obfuscator build in into Visual Studio. In the menu choose Tools / Dotfuscator community edition.
I think my reply to a similar question about JavaScript obfuscation applies here as well: in short, why bother? Your question has already been answered here ("use an obfuscator"), but I thought it wouldn't hurt to find out what your motivations are. Generally, code that you give to people is "in the hands of the enemy" -- if somebody wants to use it/figure out how it works badly enough, they will.

Categories

Resources