Get FileDescription (ProgramName) from .exe - c#

I am developing an activity tracker for my development work in Eclipse (with Java). I now have the program's path and the duration i spent in it.
e.g.: 7min C:\Program Files (x86)\Internet Explorer\iexplore.exe and
10min C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE
I now want to know the name of the program. In C# I get it like this:
System.Diagnostics.FileVersionInfo versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(file);
return (versionInfo.FileDescription == string.Empty) ? null : versionInfo.FileDescription;
Unfortunately, I found no such way in Java and i am not sure if this is even possible?
Thank you!
Edit: I also read this post and the accepted answer but was unable to get the FileDescription...

I am not aware of such API in Java. However, what you can do is as follows:
Create a C# project with the required method that returns program name.
Export this project to DLL.
In your java code you can use JNI to call the native code from you DLL project. You can check these examples: SUN JNI Example , Making Native Windows API calls from within a Java Application
(I would suggest you to consider whether you absolutely need this description.)
Good luck!

It took me forever to find this, but I finally was able to piece together this inline script:
(get-command *.exe).fileversioninfo.filedescription
Now I used *.exe and .filedescription because I was attempting to return the prettified file names for every exe in a directory, but you can use any of the available variables.

Related

How to create a program that converts 1 dll file into another dll file?

So I want to make a mod installer that doesn't contain any game code but still installs the full modded dll. Everyone with the official game always starts with the same dll (written in C# and unity).
My initial idea was to decompile using ILspy (as we only have access to .dlls) and recompile but that doesn’t work (build errors). I have thought about using something like winMerge to save changes between .dlls but not 100% sure how to use it and my final idea is to convert the whole binary from storred file into a normal number and find the difference between the 2 files.
Anyone have any idead of whats the best way to go about this?
It been a while but i want to get back to how i solved this. I ended up making an installer that compares the different in hash (binary) between 2 binary files and creates a patch. Now this patch can then be applied in the installer itself to make the output dll! Here is the github if anyone is interested - https://github.com/dan771/Snowtopia-Mod-Installer
My best luck has come from ildasm / patch / ilasm. You can do essentially the same thing with Mono.Cecil. It's quite automatable even in the face of an obfuscated dll provided that the original dll has no native code.

Get the name of a running Java program from C# (like the process name for .NET apps)

I need to get the name of the Java program thats being executed by Java, from a C# program. I have no access to the Java program (aka I'm not the one who wrote it, so I can't make it communicate with my C# program.) Is it possible to do this?
There is a command line tool that comes with the JDK called 'jps'. jps -v shows all the arguments you have passed to java.
You can call jps from c# and then read the output.
Hope this helps.
I don't know for sure, but I don't believe so and let me explain why:
on your root machine, if you use a process explorer you will just see the JVM (java virtual machine) and not what is happening inside it. the herarchy would actually be Root > Java VM > Java VM process. hope that logic helps you find your solution if there is one.

need unmanaged dll version that is being imported with DllImport

I am working on an application that imports an unmanaged dll into C#. It has a wrapper class that loads the methods so it can be called. The methods work fine in the program from the dll. I want to add saving the version of the dll that is being used. I found that I need to use FileVersionInfo.GetVersionInfo("my.dll") thanks to C# getting version of unmanaged dll. However, when running this function it exceptions saying it can't find "my.dll". The dll is in a folder off the root of the c:. This folder is in the PATH and according to http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx#search_order_for_desktop_applications it should find it.
Knowing that the my.dll file is loaded and working why can't I also call GetVersionInfo() inside the same wrapper class and find my.dll so I can get the same file's version number? Thanks for the help as I have been looking for a couple of days.
It requires the full path, it won't search for the DLL. That's too risky, the Windows search rules for a DLL are intricate and subject to configuration. If you need to do this before pinvoking any function then the best way is by pinvoking SearchPath(). Which uses the same algorithm as LoadLibrary uses if you set the first argument to null.
If you need to do this after pinvoking a function then the best way is by using the loaded DLL. Iterate Process.GetCurrentProcess().Modules to retrieve the ProcessModule.FileName.
From MSDN, the parameter for GetVersionInfo is:
The fully qualified path and name of the file to retrieve the version
information for.
So it's pretty clear. You need to pass the full path, as it seems this function relies on it. Otherwise it will most likely look for the file in the current directory (so your app's dir).
As a side note, keeping the native DLL in C:\ is bad practice.
You should store it in your application's folder. Then this function would work and your app would be more self contained. No files spread around the disk.
Of course, this is true unless you have a good reason for storing it in the root of your C drive.

Pre-commit hooks in C# with SharpSVN

I'm new to SharpSVN (and frankly--pretty new to C# as well). I've been trying get a simple pre-commit hook working which checks for a comment. (i.e. the commit fails in the absence of a comment)
There are several posts (like this one) which are related and helpful, but I have a few fundamental questions that are keeping me from getting further:
1) How do I get code like the link above running in C#? (i.e. which C# context would I use-- console application? csharp class?)
2) In a Windows Server context, how do I call my compiled C# program?
I've tried this answer's methodology with no luck.
Thanks in advance.
If you are creating a pre-commit hook you should call it pre-commit.exe. (Subversion accepts hook with the extensions .exe, .cmd, .bat and .wsf.)
Hooks communicate via stdout, stderr and in some cases stdin, so you should compile your application as a console application.
To get the hook working you must place the .exe (and the required DLLs) in the hooks directory of the repository.
See How to access file information in a pre-commit hook using SharpSVN for some examplecode.
Compile your "hook" as a console application, and then write a batch file that calls your console application. The batch file needs to be named correctly and placed in the "hooks" folder of your Subversion repository.
For your specific case, the batch file should be called pre-commit.bat (or pre-commit.cmd).
I had to keep users from commiting to the wrong branch by mistake. So I wrote a pre-commit hook that would check the comment for a key value. If the comment doesn't start with the right key the commit is aborted.
Here is the project:
http://sourceforge.net/projects/csvnprecommit/
Feel free to use it as a base for your own hook or use it as is. If you find a bug submit it to the project.

Embedded a *.exe into a dll

does somebody know how can I embedd an exe file into a dll ?
I have a tool which is an exe file that I call from c# code.
The thing is that I want to have 1 dll containing this tool (exe file) and the dll containg my c# code.
Is it possible to embedd this exe file within the resources?
Thx in advance
Sure it is. You can add any file as RC_DATA in application as resource. But I believe you will need to extract it to disk first before calling it!
Which IDE/Language you are using?
[EDIT]
Sorry! you did mention that you are using C#.
Add a resource file to you application (right click application in IDE and select "Add new item".
Use the toolbar in resource editor to add an existing file.
Then extract the exe whenever required by calling code something like:
System.IO.File.WriteAllBytes (#"C:\MyEXE\", Resource1.MyEXE);
It's worth baring in mind that your uses may not be too happy about you doing this. Embedding an executable that they've got no control over into a DLL that you'll extract and run will probably make people worry about the running a Trojan on their machine.
It's better to leave the .EXE in the filesystem and be transparent about what your application is doing.
You can load an Assembly from a byte[]. This can be obtained via the ManifestResourceStream of an embedded resource.
An alternative may be to not embed the .exe itself, but rather include its functionality in the dll, and use rundll32[1] to execute it.
On a side note, remember that when you pull a file from your resources to disk and then execute code on it, you may trigger Windows Data Execution Prevention - basically, Windows tries to automatically detect if something is supposed to be code or data, and if it looks like data (which a resource would), then it will prevent that data from being executed as code.
This becomes a particularly sticky issue if your .NET assembly is going to be used over a network instead of from a local drive - there are all sorts of .NET security configurations that might prevent this from working correctly.
Another option, and not knowing the details of your project, take this with a grain of salt: add a .exe.readme file to your install that describes to any curious users or IT people why there is an executable they weren't expecting in the installation directory :)

Categories

Resources