get executable version

Machine Code jckj33 at gmail.com
Sat Jun 1 02:02:42 UTC 2019


On Friday, 31 May 2019 at 19:22:03 UTC, rikki cattermole wrote:
> On 01/06/2019 7:07 AM, Machine Code wrote:
>> is there something on std library or package on dub to get a 
>> executable version on windows? just in case I don't need to 
>> dig into winapi.
>> 
>> Like C#'s:
>> 
>> FileVersionInfo.GetVersionInfo(path).FileVersion
>> 
>> https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo.fileversion?view=netframework-4.8
>> 
>
> You'll need to go to WinAPI.
>
> Here is a jumping off point[0] and yes we have bindings for 
> it[1] (although missing the Ex versions).
>
> [0] 
> https://docs.microsoft.com/en-us/windows/desktop/api/winver/nf-winver-getfileversioninfow
> [1] 
> https://github.com/dlang/druntime/blob/8fd52019826259dc92ab712f4b37a3f0ea4d8265/src/core/sys/windows/winver.d

Thanks I wrote this D version, if anyone is interested or would 
like to point out any mistake/improvement:

string getFileVersionString(string filename)
{
	import  core.sys.windows.windows :
		MAX_PATH;
	import core.sys.windows.winbase :
		GetModuleFileName;
	import core.sys.windows.windef :
		DWORD, UINT, LPBYTE, NULL,
		LPSTR, TCHAR;
	import core.sys.windows.winver :
		GetFileVersionInfoSize,
		GetFileVersionInfo,
		VerQueryValue,
		VS_FIXEDFILEINFO;
	import std.windows.syserror : wenforce;
	import std.string : format;
	import std.conv : wtext;
	import std.traits : fullyQualifiedName;

	wchar* szVersionFile = cast(wchar*)filename.wtext.ptr;
	DWORD verHandle = 0;
	UINT size = 0;
	LPBYTE lpBuffer = NULL;
	DWORD verSize =
	wenforce(GetFileVersionInfoSize(szVersionFile,
									&verHandle),
			fullyQualifiedName!GetFileVersionInfoSize ~ " failed");
	wchar[] verData = new wchar[verSize];
	wenforce(GetFileVersionInfo(szVersionFile,
								verHandle,
								verSize,
								verData.ptr),
			 fullyQualifiedName!GetFileVersionInfo ~ " faield");
	if(!VerQueryValue(cast(const(void)*)verData.ptr,
					  cast(const(wchar)*)"\\".ptr,
					  cast(void**)&lpBuffer,
					  &size)) {
		assert(0, fullyQualifiedName!VerQueryValue ~ " failed");
	}
	if(!size) {
		assert(0, "no size");
	}
	auto verInfo = cast(VS_FIXEDFILEINFO*)lpBuffer;
	if (verInfo.dwSignature == 0xfeef04bd) {
	 	return format!"%d.%d.%d.%d"(
	 		   (verInfo.dwFileVersionMS >> 16 ) & 0xffff,
                (verInfo.dwFileVersionMS >>  0 ) & 0xffff,
                (verInfo.dwFileVersionLS >> 16 ) & 0xffff,
                (verInfo.dwFileVersionLS >>  0 ) & 0xffff
	 		);
	}
	return "";
}


More information about the Digitalmars-d-learn mailing list