Wednesday, December 12, 2018

Get file desscription, product name. company name etc

http://www.cplusplus.com/forum/windows/181615/

GetFileVersionInfo

VerQueryValue

#include "stdafx.h"
#include <tchar.h>

#pragma comment (lib, "version.lib") 


CFileVersionInfo::CFileVersionInfo()
{
 Reset();
}


CFileVersionInfo::~CFileVersionInfo()
{}

BOOL CFileVersionInfo::GetTranslationId(LPVOID lpData, UINT unBlockSize, WORD wLangId, DWORD &dwId, BOOL bPrimaryEnough/*= FALSE*/)
{
 LPWORD lpwData;
 for (lpwData = (LPWORD)lpData; (LPBYTE)lpwData < ((LPBYTE)lpData) + unBlockSize; lpwData += 2)
 {
  if (*lpwData == wLangId)
  {
   dwId = *((DWORD*)lpwData);
   return TRUE;
  }
 }

 if (!bPrimaryEnough)
  return FALSE;

 for (lpwData = (LPWORD)lpData; (LPBYTE)lpwData < ((LPBYTE)lpData) + unBlockSize; lpwData += 2)
 {
  if (((*lpwData) & 0x00FF) == (wLangId & 0x00FF))
  {
   dwId = *((DWORD*)lpwData);
   return TRUE;
  }
 }

 return FALSE;
}

WORD CFileVersionInfo::GetFileVersion(int nIndex) const
{
 if (nIndex == 0)
  return (WORD)(m_FileInfo.dwFileVersionLS & 0x0000FFFF);
 else if (nIndex == 1)
  return (WORD)((m_FileInfo.dwFileVersionLS & 0xFFFF0000) >> 16);
 else if (nIndex == 2)
  return (WORD)(m_FileInfo.dwFileVersionMS & 0x0000FFFF);
 else if (nIndex == 3)
  return (WORD)((m_FileInfo.dwFileVersionMS & 0xFFFF0000) >> 16);
 else
  return 0;
}


WORD CFileVersionInfo::GetProductVersion(int nIndex) const
{
 if (nIndex == 0)
  return (WORD)(m_FileInfo.dwProductVersionLS & 0x0000FFFF);
 else if (nIndex == 1)
  return (WORD)((m_FileInfo.dwProductVersionLS & 0xFFFF0000) >> 16);
 else if (nIndex == 2)
  return (WORD)(m_FileInfo.dwProductVersionMS & 0x0000FFFF);
 else if (nIndex == 3)
  return (WORD)((m_FileInfo.dwProductVersionMS & 0xFFFF0000) >> 16);
 else
  return 0;
}


DWORD CFileVersionInfo::GetFileFlagsMask() const
{
 return m_FileInfo.dwFileFlagsMask;
}


DWORD CFileVersionInfo::GetFileFlags() const
{
 return m_FileInfo.dwFileFlags;
}


DWORD CFileVersionInfo::GetFileOs() const
{
 return m_FileInfo.dwFileOS;
}


DWORD CFileVersionInfo::GetFileType() const
{
 return m_FileInfo.dwFileType;
}


DWORD CFileVersionInfo::GetFileSubtype() const
{
 return m_FileInfo.dwFileSubtype;
}

stlString CFileVersionInfo::GetCompanyName() const
{
 return m_strCompanyName;
}


stlString CFileVersionInfo::GetFileDescription() const
{
 return m_strFileDescription;
}


stlString CFileVersionInfo::GetFileVersion() const
{
 return m_strFileVersion;
}


stlString CFileVersionInfo::GetInternalName() const
{
 return m_strInternalName;
}


stlString CFileVersionInfo::GetLegalCopyright() const
{
 return m_strLegalCopyright;
}


stlString CFileVersionInfo::GetOriginalFileName() const
{
 return m_strOriginalFileName;
}


stlString CFileVersionInfo::GetProductName() const
{
 return m_strProductName;
}


stlString CFileVersionInfo::GetProductVersion() const
{
 return m_strProductVersion;
}


stlString CFileVersionInfo::GetComments() const
{
 return m_strComments;
}


stlString CFileVersionInfo::GetLegalTrademarks() const
{
 return m_strLegalTrademarks;
}


stlString CFileVersionInfo::GetPrivateBuild() const
{
 return m_strPrivateBuild;
}


stlString CFileVersionInfo::GetSpecialBuild() const
{
 return m_strSpecialBuild;
}


void CFileVersionInfo::Reset()
{
 ZeroMemory(&m_FileInfo, sizeof(m_FileInfo));
 m_strCompanyName.clear();
 m_strFileDescription.clear();
 m_strFileVersion.clear();
 m_strInternalName.clear();
 m_strLegalCopyright.clear();
 m_strOriginalFileName.clear();
 m_strProductName.clear();
 m_strProductVersion.clear();
 m_strComments.clear();
 m_strLegalTrademarks.clear();
 m_strPrivateBuild.clear();
 m_strSpecialBuild.clear();
}

BOOL CFileVersionInfo::Create(LPCTSTR lpszFileName)
{
 Reset();

 DWORD dwHandle;
 DWORD dwFileVersionInfoSize = GetFileVersionInfoSize((LPTSTR)lpszFileName, &dwHandle);
 if (!dwFileVersionInfoSize)
  return FALSE;

 LPVOID lpData = (LPVOID)new BYTE[dwFileVersionInfoSize];
 if (!lpData)
  return FALSE;

 try
 {
  if (!GetFileVersionInfo((LPTSTR)lpszFileName, dwHandle, dwFileVersionInfoSize, lpData))
   throw FALSE;

  // catch default information
  LPVOID lpInfo;
  UINT  unInfoLen;
  if (VerQueryValue(lpData, _T("\\"), &lpInfo, &unInfoLen))
  {
   //ASSERT(unInfoLen == sizeof(m_FileInfo));
   if (unInfoLen == sizeof(m_FileInfo))
    memcpy(&m_FileInfo, lpInfo, unInfoLen);
  }

  // find best matching language and codepage
  VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &lpInfo, &unInfoLen);

  DWORD dwLangCode = 0;
  if (!GetTranslationId(lpInfo, unInfoLen, GetUserDefaultLangID(), dwLangCode, FALSE))
  {
   if (!GetTranslationId(lpInfo, unInfoLen, GetUserDefaultLangID(), dwLangCode, TRUE))
   {
    if (!GetTranslationId(lpInfo, unInfoLen, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), dwLangCode, TRUE))
    {
     if (!GetTranslationId(lpInfo, unInfoLen, MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL), dwLangCode, TRUE))
      // use the first one we can get
      dwLangCode = *((DWORD*)lpInfo);
    }
   }
  }


  stlString strSubBlock;
  TCHAR buf[1024];
  _stprintf(buf, _T("\\StringFileInfo\\%04X%04X\\"), dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);
  strSubBlock = buf;


  // catch string table
  stlString sBuf;

  sBuf = strSubBlock;
  sBuf += _T("CompanyName");
  if (VerQueryValue(lpData, sBuf.c_str (), &lpInfo, &unInfoLen))
   m_strCompanyName = stlString((LPCTSTR)lpInfo);

  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("FileDescription");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strFileDescription = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("FileVersion");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strFileVersion = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("InternalName");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strInternalName = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("LegalCopyright");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strLegalCopyright = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("OriginalFileName");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strOriginalFileName = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("ProductName");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strProductName = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("ProductVersion");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strProductVersion = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("Comments");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strComments = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("LegalTrademarks");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strLegalTrademarks = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("PrivateBuild");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strPrivateBuild = stlString((LPCTSTR)lpInfo);


  sBuf.clear();
  sBuf = strSubBlock;
  sBuf += _T("SpecialBuild");
  if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
   m_strSpecialBuild = stlString((LPCTSTR)lpInfo);

  delete[] lpData;
 }
 catch (BOOL)
 {
  delete[] lpData;
  return FALSE;
 }

 return TRUE;
}

 Dec 22, 2015 at 9:36am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#include <string>

#include <winver.h>
typedef std::basic_string <TCHAR> stlString;
class CFileVersionInfo
{
 
 // construction/destruction
public:
 CFileVersionInfo();
 virtual ~CFileVersionInfo();

 // operations
public:
 BOOL Create(HMODULE hModule = NULL);
 BOOL Create(LPCTSTR lpszFileName);

 // attribute operations
public:
 WORD GetFileVersion(int nIndex) const;
 WORD GetProductVersion(int nIndex) const;
 DWORD GetFileFlagsMask() const;
 DWORD GetFileFlags() const;
 DWORD GetFileOs() const;
 DWORD GetFileType() const;
 DWORD GetFileSubtype() const;

 stlString GetCompanyName() const;
 stlString GetFileDescription() const;
 stlString GetFileVersion() const;
 stlString GetInternalName() const;
 stlString GetLegalCopyright() const;
 stlString GetOriginalFileName() const;
 stlString GetProductName() const;
 stlString GetProductVersion() const;
 stlString GetComments() const;
 stlString GetLegalTrademarks() const;
 stlString GetPrivateBuild() const;
 stlString GetSpecialBuild() const;

 // implementation helpers
protected:
 virtual void Reset();
 BOOL GetTranslationId(LPVOID lpData, UINT unBlockSize, WORD wLangId, DWORD &dwId, BOOL bPrimaryEnough = FALSE);

 // attributes
private:
 VS_FIXEDFILEINFO m_FileInfo;

 stlString m_strCompanyName;
 stlString m_strFileDescription;
 stlString m_strFileVersion;
 stlString m_strInternalName;
 stlString m_strLegalCopyright;
 stlString m_strOriginalFileName;
 stlString m_strProductName;
 stlString m_strProductVersion;
 stlString m_strComments;
 stlString m_strLegalTrademarks;
 stlString m_strPrivateBuild;
 stlString m_strSpecialBuild;
};
Example usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 HWND winhandle = GetForegroundWindow ();
 DWORD winid = 0;
 GetWindowThreadProcessId ( winhandle, &winid );

 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, winid );
 
 TCHAR szExe[MAX_PATH + 1];
 DWORD lpdwSize = MAX_PATH;

 BOOL bFile = QueryFullProcessImageName ( hProcess, 0, szExe, &lpdwSize );
 CloseHandle(hProcess);

 CFileVersionInfo data;
 data.Create(szExe);
 MessageBox(NULL, data.GetFileDescription ().c_str (), szExe, MB_OK);
 return 0;
}

Sunday, December 2, 2018

git commands

git discard all uncommitted changes.

git reset
git checkout .
git clean -fdx


Monday, November 26, 2018

PowerShell file/path functions

1: Split-Path
 The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-6


2: Join-Path
The Join-Path cmdlet combines a path and child-path into a single path. The provider supplies the path delimiters.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/join-path?view=powershell-6

Wednesday, September 5, 2018

android load native dll dlopen failed . Undefined Reference to __atomic_* . __atomic_exchange_4

reference to: https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md

Some ABIs (particularly armeabi) need libatomic to provide some implementations for atomic operations.

The case was arm64 works,  armeabi had exception . 
dlopen failed,  Undefined Reference to __atomic_exchange_4

Solution: Add -latomic when linking.

The solution not working, since the -latomic already in link flag.

reference to :

https://github.com/android-ndk/ndk/issues/589

Just FYI, we were running into the same issue. In our case it was not enough to add -latomic to the linker option, but -latomic had to be listed as the last option, even after the C++ standard library.

So the added -Wl -latomic fix the issue.  

Thursday, March 8, 2018

Backslash escape in Ubuntu and MySQL export data from command line

I need write a bash script to export data from mysql.
The below command ran got good results in mysql shell.

Select [datacolum] from [table] into outfile 'outputfile'  FIELDS TERMINATED BY ',' ESCAPED BY '\\' LINES TERMINATED BY '\r\n';

And I ran it in command line:

mysql -u user -ppassword database <<EOF
Select [datacolum] from [table] into outfile 'outputfile'  FIELDS TERMINATED BY ',' ESCAPED BY '\\' LINES TERMINATED BY '\r\n';
EOF

I got exception :

ERROR 1049 (42000) at line 1: Unknown database 'n';'

Finally I realized:

The backslash escape in Ubuntu:

root@ubuntu:~# echo '\'
\
root@ubuntu:~# echo "'\'"
'\'
root@ubuntu:~# echo '\\'
\\
root@ubuntu:~# echo "'\\'"
'\'

So the correct ran the above sql in command line should be:

mysql -u user -ppassword database <<EOF
Select [datacolum] from [table] into outfile 'outputfile'  FIELDS TERMINATED BY ',' ESCAPED BY '\\\\' LINES TERMINATED BY '\r\n';
EOF





Monday, February 5, 2018

Sed -i change the text start with space

I met a issue that want use sed to replace a line start with space. and new line also start with space.

Just like new line want keep indentation as before.

replaced="    def"
sed -i "/  abc/c   $replaced" temp.txt

in this case ,   the new line will lost the space ,  no indentation

The way to do it :

replaced="\    def"
sed -i "/  abc/c   $replaced" temp.txt

This will keep space after the replacement.