Check whether executable is managed
So, another interesting question: “How to check whether the executable file managed or not by code”. Answering – ask framework about it version.
[DllImport("mscoree.dll")]private static extern int GetFileVersion([MarshalAs(UnmanagedType.LPWStr)] string szFilename,[MarshalAs(UnmanagedType.LPWStr)] StringBuilder szBuffer,int cchBuffer,out int dwLength);
But any, even very unmanaged file has file version – you ask. Any managed file has managed signature, so if the file is not managed, the method return COR_E_ASSEMBLYEXPECTED, else it return main core framework version.
private string GetRuntimeVersion(string filename){const int CCH = 0x400;StringBuilder sb = new StringBuilder(CCH);int len;int hr = GetFileVersion(filename, sb, sb.Capacity, out len);if (hr == unchecked((int)0x8007000B))return string.Empty;else if (hr < 0)throw new Win32Exception(hr);elsereturn sb.ToString(0, len - 1);}
Pretty simple, isn’t is?
You may also be interested with:
- Video encoder and metadata reading by using Windows Media Foundation
- RSA private key import from PEM format in C#
April 12th, 2007 · Comments (0)
No comments yet
Leave a Reply
Discover other tags
.NET 3.5
Accessibility
blogging general
blogging tools
C#
CodeProject
demos
DEV
DevAcademy2
download
events
fun
Hardware
help
Interop
jobs
LINQ
Microsoft
Mobile
My tools
Performance
promo
Silverlight
soft
source
teched
TechedIsrael2008
thoughts
Tips and Tricks
tutorial
Vista
Vista Battery Saver
Visual Studio
VSTS
WCF
Web
Windows 7
Windows Gadgets
Windows Live
Work process
WPF
WPF/E
WPF crossbow
XNA
XPS
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




