Skip to content

Commit

Permalink
Fix issue with QueryInterface wrapper
Browse files Browse the repository at this point in the history
- Additional debug logging
- Fix issue when using QueryInterface to create newer interfaces
- Fix dwVersion when calling DirectInput8Create() API
- Removed the need for the DllMain() function
  • Loading branch information
elishacloud committed Oct 10, 2018
1 parent 242a0c1 commit b5e8b62
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 177 deletions.
2 changes: 1 addition & 1 deletion BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 3
#define BUILD_NUMBER 4
249 changes: 131 additions & 118 deletions Common/Logging.h
Original file line number Diff line number Diff line change
@@ -1,155 +1,168 @@
#pragma once

#include <fstream>
#include <iostream>
#include <stdarg.h>

namespace Logging
{
extern std::ofstream LOG;
#ifndef DINPUTTO8NOLOG
extern std::ofstream LOG;
#endif

class Log
class Log
{
public:
Log() {}
~Log()
{
public:
Log() {}
~Log()
#ifndef DINPUTTO8NOLOG
if (LOG.is_open())
{
if (LOG.is_open())
{
LOG << std::endl;
}
LOG << std::endl;
}
#endif
}

template <typename T>
Log& operator<<(const T& t)
template <typename T>
Log& operator<<(const T& t)
{
#ifndef DINPUTTO8NOLOG
if (LOG.is_open())
{
if (LOG.is_open())
{
LOG << t;
}
return *this;
LOG << t;
}
};
#else
std::cout << t;
#endif
return *this;
}
};

class LogDebug
class LogDebug
{
public:
LogDebug() {}
~LogDebug()
{
public:
LogDebug() {}
~LogDebug()
{
#ifdef _DEBUG
if (LOG.is_open())
{
LOG << std::endl;
}
#endif // _DEBUG
}

template <typename T>
LogDebug& operator<<(const T& t)
#ifndef DINPUTTO8NOLOG
if (LOG.is_open())
{
#ifdef _DEBUG
if (LOG.is_open())
{
LOG << t;
}
#endif // _DEBUG
return *this;
LOG << std::endl;
}
};
#endif
#endif // _DEBUG
}

template <typename T>
struct Hex
LogDebug& operator<<(const T& t)
{
explicit Hex(T val) : val(val) {}
T val;
};
#ifdef _DEBUG
#ifndef DINPUTTO8NOLOG
if (LOG.is_open())
{
LOG << t;
}
#else
std::cout << t;
#endif
#else
std::cout << t;
#endif // _DEBUG
return *this;
}
};

template <typename T>
static Hex<T> hex(T val) { return Hex<T>(val); }
typedef struct { DWORD num; } hexDWORD;

template <typename T>
typename std::enable_if<std::is_class<T>::value && !std::is_same<T, std::string>::value, std::ostream&>::type operator<<(std::ostream& os, const T& t)
{
return os << static_cast<const void*>(&t);
}
static hexDWORD hex(DWORD val) { return *(hexDWORD*)&val; }

static std::ostream& operator<<(std::ostream& os, const wchar_t* wchr)
{
std::wstring ws(wchr);
return os << std::string(ws.begin(), ws.end()).c_str();
}
template <typename T>
typename std::enable_if<std::is_class<T>::value && !std::is_same<T, std::string>::value, std::ostream&>::type operator<<(std::ostream& os, const T& t)
{
return os << static_cast<const void*>(&t);
}

static std::ostream& operator<<(std::ostream& os, hexDWORD num)
{
return os << std::hex << num.num << std::dec;
}

static std::ostream& operator<<(std::ostream& os, const wchar_t* wchr)
{
std::wstring ws(wchr);
return os << std::string(ws.begin(), ws.end()).c_str();
}

#pragma warning(suppress: 4505)
static std::ostream& operator<<(std::ostream& os, REFIID riid)
{
static std::ostream& operator<<(std::ostream& os, REFIID riid)
{
#define CHECK_REFIID(riidPrefix, riidName) \
if (riid == riidPrefix ## _ ## riidName) \
{ \
return os << #riidPrefix << "_" << #riidName; \
}

CHECK_REFIID(IID, IUnknown);
CHECK_REFIID(IID, IClassFactory);
// dinput
CHECK_REFIID(CLSID, DirectInput);
CHECK_REFIID(CLSID, DirectInputDevice);
CHECK_REFIID(CLSID, DirectInput8);
CHECK_REFIID(CLSID, DirectInputDevice8);
CHECK_REFIID(IID, IDirectInputA);
CHECK_REFIID(IID, IDirectInputW);
CHECK_REFIID(IID, IDirectInput2A);
CHECK_REFIID(IID, IDirectInput2W);
CHECK_REFIID(IID, IDirectInput7A);
CHECK_REFIID(IID, IDirectInput7W);
CHECK_REFIID(IID, IDirectInput8A);
CHECK_REFIID(IID, IDirectInput8W);
CHECK_REFIID(IID, IDirectInputDeviceA);
CHECK_REFIID(IID, IDirectInputDeviceW);
CHECK_REFIID(IID, IDirectInputDevice2A);
CHECK_REFIID(IID, IDirectInputDevice2W);
CHECK_REFIID(IID, IDirectInputDevice7A);
CHECK_REFIID(IID, IDirectInputDevice7W);
CHECK_REFIID(IID, IDirectInputDevice8A);
CHECK_REFIID(IID, IDirectInputDevice8W);
CHECK_REFIID(IID, IDirectInputEffect);

return os << "{"
<< hex(riid.Data1) << ","
<< hex(riid.Data2) << ","
<< hex(riid.Data3) << ","
<< hex((UINT)riid.Data4[0]) << ","
<< hex((UINT)riid.Data4[1]) << ","
<< hex((UINT)riid.Data4[2]) << ","
<< hex((UINT)riid.Data4[3]) << ","
<< hex((UINT)riid.Data4[4]) << ","
<< hex((UINT)riid.Data4[5]) << ","
<< hex((UINT)riid.Data4[6]) << ","
<< hex((UINT)riid.Data4[7]) << ","
<< "}";
}
CHECK_REFIID(IID, IUnknown);
CHECK_REFIID(IID, IClassFactory);
// dinput
CHECK_REFIID(CLSID, DirectInput);
CHECK_REFIID(CLSID, DirectInputDevice);
CHECK_REFIID(CLSID, DirectInput8);
CHECK_REFIID(CLSID, DirectInputDevice8);
CHECK_REFIID(IID, IDirectInputA);
CHECK_REFIID(IID, IDirectInputW);
CHECK_REFIID(IID, IDirectInput2A);
CHECK_REFIID(IID, IDirectInput2W);
CHECK_REFIID(IID, IDirectInput7A);
CHECK_REFIID(IID, IDirectInput7W);
CHECK_REFIID(IID, IDirectInput8A);
CHECK_REFIID(IID, IDirectInput8W);
CHECK_REFIID(IID, IDirectInputDeviceA);
CHECK_REFIID(IID, IDirectInputDeviceW);
CHECK_REFIID(IID, IDirectInputDevice2A);
CHECK_REFIID(IID, IDirectInputDevice2W);
CHECK_REFIID(IID, IDirectInputDevice7A);
CHECK_REFIID(IID, IDirectInputDevice7W);
CHECK_REFIID(IID, IDirectInputDevice8A);
CHECK_REFIID(IID, IDirectInputDevice8W);
CHECK_REFIID(IID, IDirectInputEffect);

return os << "{"
<< hex(riid.Data1) << ","
<< hex(riid.Data2) << ","
<< hex(riid.Data3) << ","
<< hex((UINT)riid.Data4[0]) << ","
<< hex((UINT)riid.Data4[1]) << ","
<< hex((UINT)riid.Data4[2]) << ","
<< hex((UINT)riid.Data4[3]) << ","
<< hex((UINT)riid.Data4[4]) << ","
<< hex((UINT)riid.Data4[5]) << ","
<< hex((UINT)riid.Data4[6]) << ","
<< hex((UINT)riid.Data4[7]) << ","
<< "}";
}

#pragma warning(suppress: 4505)
static void logf(char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto size = vsnprintf(nullptr, 0, fmt, ap);
std::string output(size + 1, '\0');
vsprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}
static void logf(char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto size = vsnprintf(nullptr, 0, fmt, ap);
std::string output(size + 1, '\0');
vsprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}

#pragma warning(suppress: 4505)
static void logf(wchar_t * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
static void logf(wchar_t * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
#pragma warning(suppress: 4996)
auto size = _vsnwprintf(nullptr, 0, fmt, ap);
std::wstring output(size + 1, '\0');
vswprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}
auto size = _vsnwprintf(nullptr, 0, fmt, ap);
std::wstring output(size + 1, '\0');
vswprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}
Loading

0 comments on commit b5e8b62

Please sign in to comment.