Skip to content

Commit

Permalink
win32_utf8: Add fopen().
Browse files Browse the repository at this point in the history
Well, we had to enter CRT territory at one point.
  • Loading branch information
nmlgc committed Apr 16, 2014
1 parent 0898307 commit 14d309b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions thcrap/src/win32_detour.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void win32_detour(void)
"WideCharToMultiByte", WideCharToMultiByteU
);

detour_cache_add("msvcrt.dll", 1,
"fopen", fopen_u
);

detour_cache_add("gdi32.dll", 3,
"CreateFontA", CreateFontU,
"GetTextExtentPoint32A", GetTextExtentPoint32U,
Expand Down
27 changes: 27 additions & 0 deletions win32_utf8/src/msvcrt_dll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Win32 UTF-8 wrapper
*
* ----
*
* C runtime functions.
*/

#include "win32_utf8.h"

// Yes, this should better be implemented as a wrapper around fopen_s() (and
// thus, _wfopen_s()), but XP's msvcrt.dll doesn't have that function.
_Check_return_ FILE * __cdecl fopen_u(
_In_z_ const char * _Filename,
_In_z_ const char * _Mode
)
{
FILE *ret = NULL;
WCHAR_T_DEC(_Filename);
WCHAR_T_DEC(_Mode);
WCHAR_T_CONV(_Filename);
WCHAR_T_CONV(_Mode);
ret = _wfopen(_Filename_w, _Mode_w);
WCHAR_T_FREE(_Filename);
WCHAR_T_FREE(_Mode);
return ret;
}
16 changes: 16 additions & 0 deletions win32_utf8/src/msvcrt_dll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Win32 UTF-8 wrapper
*
* ----
*
* C runtime functions.
*/

#pragma once

_Check_return_ FILE * __cdecl fopen_u(
_In_z_ const char * _Filename,
_In_z_ const char * _Mode
);
#undef fopen
#define fopen fopen_u
2 changes: 2 additions & 0 deletions win32_utf8/src/win32_utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

#include <Windows.h>
#include <psapi.h>
#include <stdio.h>

#include "macros.h"
#include "utf.h"

#include "gdi32_dll.h"
#include "kernel32_dll.h"
#include "msvcrt_dll.h"
#include "psapi_dll.h"
#include "shlwapi_dll.h"
#include "user32_dll.h"
Expand Down
4 changes: 4 additions & 0 deletions win32_utf8/win32_utf8.def
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ EXPORTS
SetCurrentDirectoryU
WideCharToMultiByteU

; msvcrt.dll
; ----------
fopen_u

; psapi.dll
; ---------
GetModuleFileNameExU
Expand Down
2 changes: 2 additions & 0 deletions win32_utf8/win32_utf8.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<ClInclude Include="src\gdi32_dll.h" />
<ClInclude Include="src\kernel32_dll.h" />
<ClInclude Include="src\macros.h" />
<ClInclude Include="src\msvcrt_dll.h" />
<ClInclude Include="src\psapi_dll.h" />
<ClInclude Include="src\shlwapi_dll.h" />
<ClInclude Include="src\user32_dll.h" />
Expand All @@ -91,6 +92,7 @@
<ItemGroup>
<ClCompile Include="src\gdi32_dll.c" />
<ClCompile Include="src\kernel32_dll.c" />
<ClCompile Include="src\msvcrt_dll.c" />
<ClCompile Include="src\psapi_dll.c" />
<ClCompile Include="src\shlwapi_dll.c" />
<ClCompile Include="src\user32_dll.c" />
Expand Down
6 changes: 6 additions & 0 deletions win32_utf8/win32_utf8.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<ClInclude Include="src\wrappers.h">
<Filter>General</Filter>
</ClInclude>
<ClInclude Include="src\msvcrt_dll.h">
<Filter>Wrappers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\utf.c">
Expand All @@ -66,6 +69,9 @@
<ClCompile Include="src\wrappers.c">
<Filter>General</Filter>
</ClCompile>
<ClCompile Include="src\msvcrt_dll.c">
<Filter>Wrappers</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="win32_utf8.def">
Expand Down

0 comments on commit 14d309b

Please sign in to comment.