#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "Version")
// 从exe中读取产品名称
PTSTR FileVersion(LPTSTR szFile, LPTSTR ProductName, DWORD NameLen)
{
DWORD dwHandle, dwlen;
UINT cbTranslate;
// 得到版本信息
dwlen = GetFileVersionInfoSize(szFile,&dwHandle);
PBYTE pBlock = new BYTE[dwlen];
if(GetFileVersionInfo( szFile,
dwHandle,
dwlen,
pBlock) == FALSE)
{
delete []pBlock;
return NULL;
}
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
// Read the list of languages and code pages
if (VerQueryValue( pBlock,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate) == FALSE)
{
delete []pBlock;
return NULL;
}
TCHAR *SubBlock = new TCHAR[64];
UINT dwBytes;
LPVOID lpBuffer;
// Read the file description for each language and code page.
wsprintf( SubBlock,
TEXT("\\StringFileInfo\\%04x%04x\\ProductName") ,
lpTranslate[0].wLanguage,
lpTranslate[0].wCodePage);
VerQueryValue(pBlock,
SubBlock,
(void **)&lpBuffer,
&dwBytes);
if (dwBytes > NameLen-sizeof(TCHAR))
{
delete []SubBlock;
delete []pBlock;
return NULL;
}
ZeroMemory(ProductName, NameLen);
memcpy(ProductName, lpBuffer, dwBytes);
delete []SubBlock;
delete []pBlock;
return ProductName;
}
// 从注册表取得doc关联程序并调用查询exe名称函数
PTSTR GetWordVersion(LPTSTR ProductName, DWORD NameLen)
{
PTSTR RegPath = new TCHAR[MAX_PATH];
PTSTR DocFtype = new TCHAR[MAX_PATH];
PTSTR WordVersion = NULL;
LONG lpcbValue;
// 得到关联.doc的ftype
HKEY hkey;
if( RegOpenKey( HKEY_CLASSES_ROOT,
TEXT(".doc"),
&hkey) != ERROR_SUCCESS)
{
goto QUERY_ERROR ;
}
ZeroMemory(DocFtype, MAX_PATH*sizeof(TCHAR));
lpcbValue = MAX_PATH*sizeof(TCHAR);
if ( RegQueryValue(hkey, NULL, DocFtype, &lpcbValue) != ERROR_SUCCESS)
{
RegCloseKey(hkey);
goto QUERY_ERROR ;
}
RegCloseKey(hkey);
// 从ftype中得到关联exe所在位置
wsprintf(RegPath, TEXT("%s\\shell\\Open\\command"), DocFtype);
if( RegOpenKey( HKEY_CLASSES_ROOT,
RegPath,
&hkey) != ERROR_SUCCESS)
{
goto QUERY_ERROR ;
}
ZeroMemory(DocFtype, MAX_PATH*sizeof(TCHAR));
lpcbValue = MAX_PATH*sizeof(TCHAR);
if ( RegQueryValue(hkey, NULL, DocFtype, &lpcbValue) != ERROR_SUCCESS)
{
RegCloseKey(hkey);
goto QUERY_ERROR ;
}
RegCloseKey(hkey);
// 取出exe路径
ZeroMemory(RegPath, MAX_PATH*sizeof(TCHAR));
if ( DocFtype[0] == TEXT('\"') )
{
for (int i = 0; i<lpcbValue; i++)
{
if (DocFtype[i+1] == TEXT('\"'))
{
break;
}
RegPath[i] = DocFtype[i+1];
}
}
else
{
for (int i = 0; i<lpcbValue; i++)
{
if (DocFtype[i] == TEXT(' '))
{
break;
}
RegPath[i] = DocFtype[i];
}
}
// 得到exe 产品名称
WordVersion = FileVersion(RegPath, ProductName, NameLen);
QUERY_ERROR:
delete []DocFtype;
delete []RegPath;
return WordVersion;
}
int main(int argc, char* argv[])
{
TCHAR FileName[1024] = {0};
printf("%s\n", GetWordVersion(FileName, 1024));
return 0;
}
#include <stdio.h>
#pragma comment(lib, "Version")
// 从exe中读取产品名称
PTSTR FileVersion(LPTSTR szFile, LPTSTR ProductName, DWORD NameLen)
{
DWORD dwHandle, dwlen;
UINT cbTranslate;
// 得到版本信息
dwlen = GetFileVersionInfoSize(szFile,&dwHandle);
PBYTE pBlock = new BYTE[dwlen];
if(GetFileVersionInfo( szFile,
dwHandle,
dwlen,
pBlock) == FALSE)
{
delete []pBlock;
return NULL;
}
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
// Read the list of languages and code pages
if (VerQueryValue( pBlock,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate) == FALSE)
{
delete []pBlock;
return NULL;
}
TCHAR *SubBlock = new TCHAR[64];
UINT dwBytes;
LPVOID lpBuffer;
// Read the file description for each language and code page.
wsprintf( SubBlock,
TEXT("\\StringFileInfo\\%04x%04x\\ProductName") ,
lpTranslate[0].wLanguage,
lpTranslate[0].wCodePage);
VerQueryValue(pBlock,
SubBlock,
(void **)&lpBuffer,
&dwBytes);
if (dwBytes > NameLen-sizeof(TCHAR))
{
delete []SubBlock;
delete []pBlock;
return NULL;
}
ZeroMemory(ProductName, NameLen);
memcpy(ProductName, lpBuffer, dwBytes);
delete []SubBlock;
delete []pBlock;
return ProductName;
}
// 从注册表取得doc关联程序并调用查询exe名称函数
PTSTR GetWordVersion(LPTSTR ProductName, DWORD NameLen)
{
PTSTR RegPath = new TCHAR[MAX_PATH];
PTSTR DocFtype = new TCHAR[MAX_PATH];
PTSTR WordVersion = NULL;
LONG lpcbValue;
// 得到关联.doc的ftype
HKEY hkey;
if( RegOpenKey( HKEY_CLASSES_ROOT,
TEXT(".doc"),
&hkey) != ERROR_SUCCESS)
{
goto QUERY_ERROR ;
}
ZeroMemory(DocFtype, MAX_PATH*sizeof(TCHAR));
lpcbValue = MAX_PATH*sizeof(TCHAR);
if ( RegQueryValue(hkey, NULL, DocFtype, &lpcbValue) != ERROR_SUCCESS)
{
RegCloseKey(hkey);
goto QUERY_ERROR ;
}
RegCloseKey(hkey);
// 从ftype中得到关联exe所在位置
wsprintf(RegPath, TEXT("%s\\shell\\Open\\command"), DocFtype);
if( RegOpenKey( HKEY_CLASSES_ROOT,
RegPath,
&hkey) != ERROR_SUCCESS)
{
goto QUERY_ERROR ;
}
ZeroMemory(DocFtype, MAX_PATH*sizeof(TCHAR));
lpcbValue = MAX_PATH*sizeof(TCHAR);
if ( RegQueryValue(hkey, NULL, DocFtype, &lpcbValue) != ERROR_SUCCESS)
{
RegCloseKey(hkey);
goto QUERY_ERROR ;
}
RegCloseKey(hkey);
// 取出exe路径
ZeroMemory(RegPath, MAX_PATH*sizeof(TCHAR));
if ( DocFtype[0] == TEXT('\"') )
{
for (int i = 0; i<lpcbValue; i++)
{
if (DocFtype[i+1] == TEXT('\"'))
{
break;
}
RegPath[i] = DocFtype[i+1];
}
}
else
{
for (int i = 0; i<lpcbValue; i++)
{
if (DocFtype[i] == TEXT(' '))
{
break;
}
RegPath[i] = DocFtype[i];
}
}
// 得到exe 产品名称
WordVersion = FileVersion(RegPath, ProductName, NameLen);
QUERY_ERROR:
delete []DocFtype;
delete []RegPath;
return WordVersion;
}
int main(int argc, char* argv[])
{
TCHAR FileName[1024] = {0};
printf("%s\n", GetWordVersion(FileName, 1024));
return 0;
}