Files
Slnkdwf/SlnkDWFCom/About.cpp
2020-05-19 10:52:05 +00:00

242 lines
5.7 KiB
C++

// About.cpp : Implementation of CAbout
#include "stdafx.h"
#include "About.h"
#include "SLNKDWFVersion.h"
#define SECURITY_WIN32
#include "Security.h"
#include "accctrl.h"
#include "aclapi.h"
#include "sddl.h"
// CAbout
extern HINSTANCE g_hInstance;
// In release mode verwijderen we altijd About.obj om hercompile te forceren
CString buildDATE(__DATE__);
CString buildTIME(__TIME__);
STDMETHODIMP CAbout::get_All(BSTR* pVal)
{
char module[_MAX_PATH]; // HMODULE
GetModuleFileName(g_hInstance, module, sizeof(module)/sizeof(char));
char name[_MAX_PATH]; // HMODULE
ULONG cnt = sizeof(name)/sizeof(char);
if (!GetUserNameEx(NameSamCompatible, name, &cnt))
{
myTRACE("\nGetUserNameEx failed (%d): %s", GetLastError(), myGetLastErrorMsg());
}
CString result;
result.Format(" SLNKDWF: built on %s, %s"
"\n DLL: %s"
#ifdef _DEBUG
" (debug version)"
#endif
"\nUsercontext: %s", buildDATE, buildTIME, module, name);
(*pVal) = result.AllocSysString();
return S_OK;
}
STDMETHODIMP CAbout::get_DLLPath(BSTR* pVal)
{
char module[_MAX_PATH]; // HMODULE
GetModuleFileName(g_hInstance, module, sizeof(module));
CString result;
result.Format("%s", module);
(*pVal) = result.AllocSysString();
return S_OK;
}
STDMETHODIMP CAbout::get_Buildtime(BSTR* pVal)
{
CString result;
result.Format("%s, %s", buildDATE, buildTIME);
(*pVal) = result.AllocSysString();
return S_OK;
}
STDMETHODIMP CAbout::get_UserContext(BSTR* pVal)
{
char name[_MAX_PATH]; // HMODULE
ULONG cnt = sizeof(name)/sizeof(char);
CString result;
if (!GetUserNameEx(NameSamCompatible, name, &cnt))
{
myTRACE("\nGetUserNameEx failed (%d): %s", GetLastError(), myGetLastErrorMsg());
result = "Unable to detect user";
}
else
result = name;
(*pVal) = result.AllocSysString();
return S_OK;
}
STDMETHODIMP CAbout::get_VersionMajor(LONG* pVal)
{
(*pVal) = SLNK_MAJOR_VERSION;
return S_OK;
}
STDMETHODIMP CAbout::get_VersionMinor(LONG* pVal)
{
(*pVal) = SLNK_MINOR_VERSION;
return S_OK;
}
STDMETHODIMP CAbout::get_VersionString(BSTR* pVal)
{
CString result = SLNK_BUILDVERSION;
(*pVal) = result.AllocSysString();
return S_OK;
}
STDMETHODIMP CAbout::get_usTimer(DOUBLE* pVal)
{
LARGE_INTEGER currTime;
LARGE_INTEGER frequency;
(*pVal)=0;
if (!QueryPerformanceFrequency(&frequency))
return S_OK;
if (!QueryPerformanceCounter(&currTime))
return S_OK;
(*pVal) = ((double)currTime.QuadPart)*1000000/frequency.QuadPart;
return S_OK;
}
// Handig voor sommige load-tests
STDMETHODIMP CAbout::Sleep(LONG pVal)
{
::Sleep(pVal);
return S_OK;
}
STDMETHODIMP CAbout::fileOwner(BSTR fname, BSTR* pVal)
{
CString filename(fname);
CString result = "<unknown>";
DWORD dwRtnCode = 0;
PSID pSidOwner = NULL;
BOOL bRtnBool = TRUE;
LPTSTR AcctName = NULL;
LPTSTR DomainName = NULL;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD = NULL;
// Get the handle of the file object.
hFile = CreateFile(
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
return myAtlReportError(GetObjectCLSID(), "\nCAbout::fileOwner('%ls')", (LPCSTR)fname);
}
// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(
hFile,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSidOwner,
NULL,
NULL,
NULL,
&pSD);
CloseHandle(hFile);
// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
return myAtlReportError(GetObjectCLSID(), "\nCAbout::fileOwner('%ls') GetSecurityInfo", (LPCSTR)fname);
}
// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
NULL, // local computer
pSidOwner,
AcctName,
(LPDWORD)&dwAcctName,
DomainName,
(LPDWORD)&dwDomainName,
&eUse);
// Reallocate memory for the buffers.
AcctName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwAcctName);
// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
return myAtlReportError(GetObjectCLSID(), "\nCAbout::fileOwner('%ls') GlobalAlloc", (LPCSTR)fname);
}
DomainName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
return myAtlReportError(GetObjectCLSID(), "\nCAbout::fileOwner('%ls') GlobalAlloc", (LPCSTR)fname);
}
// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid(
NULL, // name of local or remote computer
pSidOwner, // security identifier
AcctName, // account name buffer
(LPDWORD)&dwAcctName, // size of account name buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type
// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
if (dwErrorCode == ERROR_NONE_MAPPED)
{
LPTSTR psz;
ConvertSidToStringSid(pSidOwner, &psz);
result = CString(DomainName) + "\\" + psz;
LocalFree(psz);
}
else
{
return myAtlReportError(GetObjectCLSID(), "\nCAbout::fileOwner('%ls') LookupAccountSid", (LPCSTR)fname);
}
}
else if (bRtnBool == TRUE)
{
// Print the account name.
result = CString(DomainName) + "\\" + AcctName;
}
GlobalFree(DomainName);
GlobalFree(AcctName);
(*pVal) = result.AllocSysString();
return S_OK;
}