Skip to content

Commit

Permalink
C++ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jchv committed Sep 15, 2024
1 parent 1e29f4b commit db7a007
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 131 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CC := i686-w64-mingw32-gcc
CXX := i686-w64-mingw32-g++
RM := rm
SRCDIR := src/
OBJDIR := obj/
Expand Down Expand Up @@ -56,16 +56,19 @@ all: $(OUT) $(TESTOUT)
# Rugburn
$(OBJDIR)%.o: $(SRCDIR)%.c
@mkdir -p "$(dir $@)"
$(CC) -c $(CFLAGS) "$<" -o "$@"
$(CXX) -c $(CFLAGS) "$<" -o "$@"
$(OBJDIR)%.o: $(SRCDIR)%.cpp
@mkdir -p "$(dir $@)"
$(CXX) -c $(CFLAGS) "$<" -o "$@"
$(OBJDIR)%.o: $(SRCDIR)%.S
@mkdir -p "$(dir $@)"
$(CC) -c $(CFLAGS) "$<" -o "$@"
$(CXX) -c $(CFLAGS) "$<" -o "$@"
$(OUT): $(OBJS)
@mkdir -p "$(dir $@)"
$(CC) $(OBJS) $(LDFLAGS) -shared -o "$@" export.def -Wl,-e_DllMain
$(CXX) $(OBJS) $(LDFLAGS) -shared -o "$@" export.def -Wl,-e_DllMain
$(TESTOUT): $(TESTOBJS)
@mkdir -p "$(dir $@)"
$(CC) $(TESTOBJS) $(LDFLAGS) -o "$@" -Wl,-e_start
$(CXX) $(TESTOBJS) $(LDFLAGS) -o "$@" -Wl,-e_start
check: out/test.exe
$(EXECWIN) out/test.exe | tappy
clean:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ make

And you should find an `ijl15.dll` in your `out/` directory.

By default, the Makefile will assume that `i686-w64-mingw32-gcc` is the appropriate compiler. You can override this by passing in the `CC` variable:
By default, the Makefile will assume that `i686-w64-mingw32-g++` is the appropriate compiler. You can override this by passing in the `CXX` variable:

```
make CC=gcc
make CXX=g++
```
## Install

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
make -j
(printf -- "-nostdlib\n-nostdinc\n-nostdinc++\n--target=i686-pc-windows-gnu\n-D_WIN32\n-D__MINGW32__\n-DSTRSAFE_NO_DEPRECATE" \
&& </dev/null i686-w64-mingw32-gcc -E -v - 2>&1 \
&& </dev/null i686-w64-mingw32-g++ -E -v - 2>&1 \
| grep ^COLLECT_GCC_OPTIONS= \
| tail -1 \
| cut -d= -f2- \
Expand Down
10 changes: 5 additions & 5 deletions src/common-fnptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ typedef struct tagINITCOMMONCONTROLSEX {
typedef BOOL(STDCALL *PFNINITCOMMONCONTROLSEXPROC)(const INITCOMMONCONTROLSEX *picce);

// ole32.dll
typedef HRESULT(STDCALL *PFNCOGETCLASSOBJECTPROC)(REFCLSID rclsid, DWORD dwClsContext,
LPVOID pvReserved, REFIID riid, LPVOID *ppv);
typedef HRESULT(STDCALL *PFNCOCREATEINSTANCEPROC)(REFCLSID rclsid, LPUNKNOWN pUnkOuter,
DWORD dwClsContext, REFIID riid, LPVOID *ppv);
typedef HRESULT(STDCALL *PFNCOGETCLASSOBJECTPROC)(const IID *rclsid, DWORD dwClsContext,
LPVOID pvReserved, const IID *riid, LPVOID *ppv);
typedef HRESULT(STDCALL *PFNCOCREATEINSTANCEPROC)(const IID *rclsid, LPUNKNOWN pUnkOuter,
DWORD dwClsContext, const IID *riid, LPVOID *ppv);

// oleaut32.dll
typedef BSTR(STDCALL *PFNSYSALLOCSTRINGLENPROC)(const OLECHAR *strIn, UINT ui);
typedef void(STDCALL *PFNSYSFREESTRINGPROC)(BSTR bstrString);

// ieframe.dll
typedef HRESULT(STDCALL *PFNINVOKEPROC)(IDispatch *This, DISPID dispIdMember, REFIID riid,
typedef HRESULT(STDCALL *PFNINVOKEPROC)(IDispatch *This, DISPID dispIdMember, const IID *riid,
LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
typedef HRESULT(STDCALL *PFNNAVIGATEPROC)(IWebBrowser2 *This, BSTR URL, VARIANT *Flags,
Expand Down
38 changes: 24 additions & 14 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ static PSTR pszLogPrefix = NULL;
#pragma function(memcpy)
#pragma function(memset)
#else

#ifdef __cplusplus
extern "C" {
#endif

void __chkstk_ms(void) { return; }
unsigned long long __stdcall _aullshr(unsigned long long a, long b) { return a >> b; }

#ifdef __cplusplus
}
#endif

#endif

// String formatting
Expand Down Expand Up @@ -58,7 +68,7 @@ LPSTR DupStr(LPCSTR src) {

while (src[len])
len++;
str = AllocMem(len + 1);
str = (LPSTR)AllocMem(len + 1);
p = str;
while (*src)
*p++ = *src++;
Expand All @@ -72,7 +82,7 @@ PSTR GetSelfPath() {
return pszSelfPath;
}

pszSelfPath = AllocMem(4096);
pszSelfPath = (PSTR)AllocMem(4096);

GetModuleFileNameA(GetModuleHandleA("ijl15"), pszSelfPath, 4096);

Expand All @@ -95,7 +105,7 @@ LPSTR ReadEntireFile(LPCSTR szPath, LPDWORD dwFileSize) {
}

dwBytesToRead = GetFileSize(hFile, NULL);
buffer = AllocMem(dwBytesToRead + 1);
buffer = (LPSTR)AllocMem(dwBytesToRead + 1);
memset(buffer, 0, dwBytesToRead + 1);

data = buffer;
Expand Down Expand Up @@ -145,11 +155,11 @@ VOID WriteEntireFile(LPCSTR szPath, LPCSTR data, DWORD dwBytesToWrite) {
CloseHandle(hFile);
}

VOID FatalError(PCHAR fmt, ...) {
VOID FatalError(LPCSTR fmt, ...) {
va_list args;
PCHAR buffer;

buffer = AllocMem(4096);
buffer = (PCHAR)AllocMem(4096);

if (!buffer) {
MessageBoxA(HWND_DESKTOP, "Out of memory.", "rugburn", MB_OK | MB_ICONERROR);
Expand All @@ -165,9 +175,9 @@ VOID FatalError(PCHAR fmt, ...) {
ExitProcess(1);
}

VOID Warning(PCHAR fmt, ...) {
VOID Warning(LPCSTR fmt, ...) {
va_list args;
PCHAR buffer = AllocMem(4096);
PCHAR buffer = (PCHAR)AllocMem(4096);

va_start(args, fmt);
VSPrintfZ(buffer, fmt, args);
Expand All @@ -177,11 +187,11 @@ VOID Warning(PCHAR fmt, ...) {
FreeMem(buffer);
}

VOID Log(PCHAR fmt, ...) {
VOID Log(LPCSTR fmt, ...) {
va_list args;
HANDLE hAppend;
PCHAR buffer = AllocMem(4096);
PCHAR pfxbuffer = AllocMem(128);
PCHAR buffer = (PCHAR)AllocMem(4096);
PCHAR pfxbuffer = (PCHAR)AllocMem(128);
PCHAR logmsg = buffer;
DWORD cb = 0;

Expand Down Expand Up @@ -212,8 +222,8 @@ VOID Log(PCHAR fmt, ...) {
}

VOID InitLog() {
PCHAR szPfxBuf = AllocMem(4096);
PCHAR szFileName = AllocMem(4096);
PCHAR szPfxBuf = (PCHAR)AllocMem(4096);
PCHAR szFileName = (PCHAR)AllocMem(4096);
PCHAR szBaseName;
DWORD cbFileName;

Expand All @@ -233,10 +243,10 @@ VOID InitLog() {
pszLogPrefix = szPfxBuf;
}

VOID ConsoleLog(PCHAR fmt, ...) {
VOID ConsoleLog(LPCSTR fmt, ...) {
va_list args;
HANDLE hConsole;
PCHAR buffer = AllocMem(4096);
PCHAR buffer = (PCHAR)AllocMem(4096);
DWORD cb = 0;
va_start(args, fmt);
cb += VSPrintfZ(buffer, fmt, args);
Expand Down
19 changes: 15 additions & 4 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@

#pragma once

#ifdef __cplusplus
// We must use COM CINTERFACE even in C++
#define CINTERFACE
// Temporary: make REF parameters pointers even in C++
// Otherwise, making our code compatible with both C and C++ is hard.
#define _REFCLSID_DEFINED
#define REFCLSID const IID *
#define _REFIID_DEFINED
#define REFIID const IID *
#endif

#define STDCALL __stdcall
#define EXPORT __export

Expand Down Expand Up @@ -62,10 +73,10 @@ LPSTR DupStr(LPCSTR src);
BOOL FileExists(LPCSTR szPath);
LPSTR ReadEntireFile(LPCSTR szPath, LPDWORD dwFileSize);
VOID WriteEntireFile(LPCSTR szPath, LPCSTR data, DWORD dwBytesToWrite);
VOID FatalError(PCHAR fmt, ...);
VOID Warning(PCHAR fmt, ...);
VOID Log(PCHAR fmt, ...);
VOID ConsoleLog(PCHAR fmt, ...);
VOID FatalError(LPCSTR fmt, ...);
VOID Warning(LPCSTR fmt, ...);
VOID Log(LPCSTR fmt, ...);
VOID ConsoleLog(LPCSTR fmt, ...);
VOID InitLog();
HMODULE LoadLib(LPCSTR szName);
PVOID GetProc(HMODULE hModule, LPCSTR szName);
Expand Down
4 changes: 4 additions & 0 deletions src/dll/rugburn/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static VOID InitEnvironment() {
FreeMem(szPangyaArg);
}

#ifdef __cplusplus
extern "C" BOOL STDCALL DllMain(HANDLE hInstance, DWORD dwReason, LPVOID reserved);
#endif

extern BOOL STDCALL DllMain(HANDLE hInstance, DWORD dwReason, LPVOID reserved) {
if (dwReason != DLL_PROCESS_ATTACH) {
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion src/exdispport.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,4 @@ struct IWebBrowser2 {
};
#else
#include <ExDisp.h>
#endif
#endif
12 changes: 8 additions & 4 deletions src/exe/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ static void STDCALL DispatchTest(const DISPATCH_TESTCASE *_this, DWORD testnum)
}

static void STDCALL DispatchThroughThiscallTest(const DISPATCH_TESTCASE *_this, DWORD testnum) {
PFNDISPATCHTESTPROC pfnDispatchProc =
(PFNDISPATCHTESTPROC)BuildStdcallToThiscallThunk(BuildThiscallToStdcallThunk(DispatchTest));
PFNDISPATCHTESTPROC pfnDispatchProc = (PFNDISPATCHTESTPROC)BuildStdcallToThiscallThunk(
BuildThiscallToStdcallThunk((LPCVOID)DispatchTest));
pfnDispatchProc(_this, testnum);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ static BOOL ijl15_crash_test() {

// Parse JPEG header
jcp.JPGFile = 0;
jcp.JPGBytes = pJPGBytes;
jcp.JPGBytes = (PBYTE)pJPGBytes;
jcp.JPGSizeBytes = dwJPGSizeBytes;
ijlRead(&jcp, IJL_JBUFF_READPARAMS);
if (jerr < 0) {
Expand All @@ -131,7 +131,7 @@ static BOOL ijl15_crash_test() {
jcp.DIBWidth = jcp.JPGWidth;
jcp.DIBHeight = jcp.JPGHeight;
jcp.DIBPadBytes = 0;
jcp.DIBBytes = buffer;
jcp.DIBBytes = (PBYTE)buffer;
jerr = ijlRead(&jcp, IJL_JBUFF_READWHOLEIMAGE);
FreeMem(buffer);

Expand All @@ -147,6 +147,10 @@ const UNIT_TEST unit_tests[] = {

#define COUNT_OF(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))

#ifdef __cplusplus
extern "C" void __cdecl start(void);
#endif

extern void __cdecl start(void) {
int i, result = 0, testnum = 0, totaltests = 0;

Expand Down
8 changes: 4 additions & 4 deletions src/hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ static CHAR ToHexChar(BYTE bValue) {
return 0;
}

LPSTR ToHex(PVOID pData, DWORD dwLen) {
LPSTR ToHex(LPCVOID pData, DWORD dwLen) {
LPSTR pHexData, pHexPtr;
PCHAR pcData = (PCHAR)pData;
pHexData = pHexPtr = AllocMem(dwLen * 2 + 1);
LPCSTR pcData = (LPCSTR)pData;
pHexData = pHexPtr = (LPSTR)AllocMem(dwLen * 2 + 1);
while (dwLen--) {
*pHexPtr++ = ToHexChar((*pcData & 0xF0) >> 4);
*pHexPtr++ = ToHexChar((*pcData & 0x0F) >> 0);
Expand Down Expand Up @@ -142,7 +142,7 @@ void ParsePatch(LPCSTR lpszText, LPSTR *pDataOut, DWORD *pSizeOut) {
}

// Allocate memory
*pDataOut = AllocMem(*pSizeOut);
*pDataOut = (LPSTR)AllocMem(*pSizeOut);

// Parse
inPos = lpszText;
Expand Down
2 changes: 1 addition & 1 deletion src/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "common.h"

LPSTR ToHex(PVOID pData, DWORD dwLen);
LPSTR ToHex(LPCVOID pData, DWORD dwLen);
DWORD FromHex(LPCSTR pHex, PVOID pData);

DWORD ParseAddress(LPCSTR lpszText);
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/comctl32/dynamic_patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ static VOID STDCALL GetStartupInfoWHook(LPSTARTUPINFOW lpStartupInfo) {
VOID InitComCtl32Hook() {
hComCtl32Module = LoadLib("comctl32");
hKernel32Module = LoadLib("kernel32");
pInitCommonControlsEx =
HookProc(hComCtl32Module, "InitCommonControlsEx", InitCommonControlsExHook);
pGetStartupInfoA = HookProc(hKernel32Module, "GetStartupInfoA", GetStartupInfoAHook);
pGetStartupInfoW = HookProc(hKernel32Module, "GetStartupInfoW", GetStartupInfoWHook);
pInitCommonControlsEx = (PFNINITCOMMONCONTROLSEXPROC)HookProc(
hComCtl32Module, "InitCommonControlsEx", (PVOID)InitCommonControlsExHook);
pGetStartupInfoA = (PFNGETSTARTUPINFOAPROC)HookProc(hKernel32Module, "GetStartupInfoA",
(PVOID)GetStartupInfoAHook);
pGetStartupInfoW = (PFNGETSTARTUPINFOWPROC)HookProc(hKernel32Module, "GetStartupInfoW",
(PVOID)GetStartupInfoWHook);
}
8 changes: 5 additions & 3 deletions src/hooks/kernel32/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ static BOOL STDCALL GetExitCodeProcessHook(HANDLE hProcess, LPDWORD lpExitCode)

VOID InitInjectHook() {
hKernel32Module = LoadLib("kernel32");
pCreateMutexA = GetProc(hKernel32Module, "CreateMutexA");
pCreateMutexA = (PFNCREATEMUTEXAPROC)GetProc(hKernel32Module, "CreateMutexA");
hGameguardFakeHandle = pCreateMutexA(NULL, FALSE, NULL);
pCreateProcessA = HookProc(hKernel32Module, "CreateProcessA", CreateProcessAHook);
pGetExitCodeProcess = HookProc(hKernel32Module, "GetExitCodeProcess", GetExitCodeProcessHook);
pCreateProcessA = (PFNCREATEPROCESSAPROC)HookProc(hKernel32Module, "CreateProcessA",
(PVOID)CreateProcessAHook);
pGetExitCodeProcess = (PFNGETEXITCODEPROCESSPROC)HookProc(hKernel32Module, "GetExitCodeProcess",
(PVOID)GetExitCodeProcessHook);
}
12 changes: 6 additions & 6 deletions src/hooks/msvcr100/msvcr100.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ static VOID InitMsvcr100Hook() {
return;
}

pStricmp = GetProc(hMsvcrModule, "_stricmp");
pSetLocale = GetProc(hMsvcrModule, "setlocale");
pStricmp = (STRICMPFNPTR)GetProc(hMsvcrModule, "_stricmp");
pSetLocale = (SETLOCALEFNPTR)GetProc(hMsvcrModule, "setlocale");
oldlocale = pSetLocale(2, NULL);
if (!pStricmp || !pSetLocale) {
Log("Not checking for Wine _stricmp bug: msvcr100 functions not found.\n");
Expand All @@ -66,7 +66,7 @@ static VOID InitMsvcr100Hook() {

if (result == 0) {
Log("Wine _stricmp bug detected; mitigating.\n");
pStricmp = HookProc(hMsvcrModule, "_stricmp", StricmpHook);
pStricmp = (STRICMPFNPTR)HookProc(hMsvcrModule, "_stricmp", (PVOID)StricmpHook);
} else {
Log("Wine _stricmp bug not detected.\n");
}
Expand All @@ -82,8 +82,8 @@ static VOID InitMsvcr71Hook() {
return;
}

pStricmp = GetProc(hMsvcrModule, "_stricmp");
pSetLocale = GetProc(hMsvcrModule, "setlocale");
pStricmp = (STRICMPFNPTR)GetProc(hMsvcrModule, "_stricmp");
pSetLocale = (SETLOCALEFNPTR)GetProc(hMsvcrModule, "setlocale");
oldlocale = pSetLocale(2, NULL);
if (!pStricmp || !pSetLocale) {
Log("Not checking for Wine _stricmp bug: msvcr71 functions not found.\n");
Expand All @@ -99,7 +99,7 @@ static VOID InitMsvcr71Hook() {

if (result == 0) {
Log("Wine _stricmp bug detected; mitigating.\n");
pStricmp = HookProc(hMsvcrModule, "_stricmp", StricmpHook);
pStricmp = (STRICMPFNPTR)HookProc(hMsvcrModule, "_stricmp", (PVOID)StricmpHook);
} else {
Log("Wine _stricmp bug not detected.\n");
}
Expand Down
Loading

0 comments on commit db7a007

Please sign in to comment.