Skip to content

Commit 37ab23b

Browse files
Use Unicode API for CreateBMPFile, use *CAString where appropriate
1 parent a7e0a13 commit 37ab23b

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

Graphics/Win32/GDI/Bitmap.hsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ sizeofLPBITMAPFILEHEADER = #{size BITMAPFILEHEADER}
376376

377377
createBMPFile :: String -> HBITMAP -> HDC -> IO ()
378378
createBMPFile name bm dc =
379-
withCString name $ \ c_name ->
379+
withCWString name $ \ c_name ->
380380
c_CreateBMPFile c_name bm dc
381381
foreign import ccall unsafe "dumpBMP.h CreateBMPFile"
382-
c_CreateBMPFile :: LPCSTR -> HBITMAP -> HDC -> IO ()
382+
c_CreateBMPFile :: LPCTSTR -> HBITMAP -> HDC -> IO ()
383383

384384
{-# CFILES cbits/dumpBMP.c #-}
385385

System/Win32/DLL.hsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ foreign import stdcall unsafe "windows.h GetModuleHandleW"
5454

5555
getProcAddress :: HMODULE -> String -> IO Addr
5656
getProcAddress hmod procname =
57-
withCString procname $ \ c_procname ->
57+
withCAString procname $ \ c_procname ->
5858
failIfNull "GetProcAddress" $ c_GetProcAddress hmod c_procname
5959
foreign import stdcall unsafe "windows.h GetProcAddress"
6060
c_GetProcAddress :: HMODULE -> LPCSTR -> IO Addr

System/Win32/NLS.hsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ stringToUnicode :: CodePage -> String -> IO String
350350
stringToUnicode _cp "" = return ""
351351
-- MultiByteToWideChar doesn't handle empty strings (#1929)
352352
stringToUnicode cp mbstr =
353-
withCStringLen mbstr $ \(cstr,len) -> do
353+
withCAStringLen mbstr $ \(cstr,len) -> do
354354
wchars <- failIfZero "MultiByteToWideChar" $ multiByteToWideChar
355355
cp
356356
0

System/Win32/SimpleMAPI.hsc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import Foreign ( FunPtr, newForeignPtr, pokeByteOff, maybeWith
2020
, Ptr, castPtr, castPtrToFunPtr, nullPtr
2121
, touchForeignPtr, alloca, peek, allocaBytes
2222
, minusPtr, plusPtr, copyBytes, ForeignPtr )
23-
import Foreign.C ( withCString, withCStringLen )
23+
import Foreign.C ( withCAString, withCAStringLen )
24+
-- Apparently, simple MAPI does not support unicode and probably never will,
25+
-- so this module will just mangle any Unicode in your strings
2426
import Graphics.Win32.GDI.Types ( HWND)
2527
import System.Win32.DLL ( loadLibrary, c_GetProcAddress, freeLibrary
2628
, c_FreeLibraryFinaliser )
@@ -141,7 +143,7 @@ loadMapiFuncs dllname dll = liftM5 MapiFuncs
141143
(loadProc "MAPISendMail" dll mkMapiSendMail)
142144
where
143145
loadProc :: String -> HMODULE -> (FunPtr a -> a) -> IO a
144-
loadProc name dll conv = withCString name $ \name' -> do
146+
loadProc name dll conv = withCAString name $ \name' -> do
145147
proc <- failIfNull ("loadMapiDll: " ++ dllname ++ ": " ++ name)
146148
$ c_GetProcAddress dll name'
147149
return $ conv $ castPtrToFunPtr proc
@@ -190,8 +192,8 @@ mapiLogon
190192
-> MapiFlag -- ^ None, one or many flags: FORCE_DOWNLOAD, NEW_SESSION, LOGON_UI, PASSWORD_UI
191193
-> IO LHANDLE
192194
mapiLogon f hwnd ses pw flags =
193-
maybeWith withCString ses $ \ses ->
194-
maybeWith withCString pw $ \pw ->
195+
maybeWith withCAString ses $ \ses ->
196+
maybeWith withCAString pw $ \pw ->
195197
alloca $ \out -> do
196198
mapiFail_ "MAPILogon: " $ mapifLogon
197199
f (maybeHWND hwnd)
@@ -242,8 +244,8 @@ withRecipient f ses rcls rec act = resolve "" rec
242244
act buf
243245
resolve err rc = case rc of
244246
Recip name addr ->
245-
withCString name $ \name ->
246-
withCString addr $ \addr ->
247+
withCAString name $ \name ->
248+
withCAString addr $ \addr ->
247249
allocaBytes (#size MapiRecipDesc) $ \buf -> do
248250
(#poke MapiRecipDesc, ulReserved) buf (0::ULONG)
249251
(#poke MapiRecipDesc, lpszName) buf name
@@ -253,7 +255,7 @@ withRecipient f ses rcls rec act = resolve "" rec
253255
a buf
254256
RecipResolve hwnd flag name fallback -> do
255257
res <- alloca $ \res ->
256-
withCString name $ \name' -> do
258+
withCAString name $ \name' -> do
257259
errn <- mapifResolveName
258260
f ses (maybeHWND hwnd) name' flag 0 res
259261
if errn==(#const SUCCESS_SUCCESS)
@@ -310,7 +312,7 @@ withFileTag ft act =
310312
where
311313
w v a = case v of
312314
Nothing -> a (nullPtr, 0)
313-
Just x -> withCStringLen x a
315+
Just x -> withCAStringLen x a
314316

315317
data Attachment = Attachment
316318
{ attFlag :: MapiFlag
@@ -330,9 +332,9 @@ withAttachments att act = allocaBytes (len*as) $ \buf -> write (act len buf) buf
330332
len = length att
331333
write act _ [] = act
332334
write act buf (att:y) =
333-
withCString (attPath att) $ \path ->
335+
withCAString (attPath att) $ \path ->
334336
maybeWith withFileTag (attTag att) $ \tag ->
335-
withCString (maybe (attPath att) id (attName att)) $ \name -> do
337+
withCAString (maybe (attPath att) id (attName att)) $ \name -> do
336338
(#poke MapiFileDesc, ulReserved) buf (0::ULONG)
337339
(#poke MapiFileDesc, flFlags) buf (attFlag att)
338340
(#poke MapiFileDesc, nPosition) buf (maybe 0xffffffff id $ attPosition att)
@@ -363,11 +365,11 @@ withMessage
363365
-> (Ptr Message -> IO a)
364366
-> IO a
365367
withMessage f ses m act =
366-
withCString (msgSubject m) $ \subject ->
367-
withCString (msgBody m) $ \body ->
368-
maybeWith withCString (msgType m) $ \message_type ->
369-
maybeWith withCString (msgDate m) $ \date ->
370-
maybeWith withCString (msgConversationId m) $ \conv_id ->
368+
withCAString (msgSubject m) $ \subject ->
369+
withCAString (msgBody m) $ \body ->
370+
maybeWith withCAString (msgType m) $ \message_type ->
371+
maybeWith withCAString (msgDate m) $ \date ->
372+
maybeWith withCAString (msgConversationId m) $ \conv_id ->
371373
withRecipients f ses (msgRecips m) $ \rlen rbuf ->
372374
withAttachments (msgAttachments m) $ \alen abuf ->
373375
maybeWith (withRecipient f ses RcOriginal) (msgFrom m) $ \from ->

cbits/dumpBMP.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434

3535
//typedef LPBITMAPINFO PBITMAPINFO; // hack to keep cygwin32b17 happy
3636

37-
void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
37+
void CreateBMPFile(LPCTSTR pszFileName, HBITMAP hBmp, HDC hDC)
3838
{
39-
int hFile;
40-
OFSTRUCT ofReOpenBuff;
39+
HANDLE hFile;
4140
HBITMAP hTmpBmp, hBmpOld;
4241
BOOL bSuccess;
4342
BITMAPFILEHEADER bfh;
@@ -46,6 +45,7 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
4645
BITMAPINFO bmi;
4746
PBYTE pjTmp, pjTmpBmi;
4847
ULONG sizBMI;
48+
DWORD dwBytesWritten;
4949

5050

5151
bSuccess = TRUE;
@@ -132,8 +132,8 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
132132
//
133133
// Lets open the file and get ready for writing
134134
//
135-
if ((hFile = OpenFile(pszFileName, (LPOFSTRUCT)&ofReOpenBuff,
136-
OF_CREATE | OF_WRITE)) == -1) {
135+
if ((hFile = CreateFileW(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL))
136+
== INVALID_HANDLE_VALUE) {
137137
fprintf(stderr, "Failed in OpenFile!");
138138
goto ErrExit2;
139139
}
@@ -151,7 +151,7 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
151151
//
152152
// Write out the file header now
153153
//
154-
if (_lwrite(hFile, (LPSTR)&bfh, sizeof(BITMAPFILEHEADER)) == -1) {
154+
if (WriteFile(hFile, (LPCVOID)&bfh, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL) == -1) {
155155
fprintf(stderr, "Failed in WriteFile!");
156156
bSuccess = FALSE;
157157
goto ErrExit3;
@@ -179,7 +179,7 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
179179
//
180180
// Now write out the BitmapInfoHeader and color table, if any
181181
//
182-
if (_lwrite(hFile, (LPSTR)pbmi, sizBMI) == -1) {
182+
if (WriteFile(hFile, (LPCVOID)pbmi, sizBMI, &dwBytesWritten, NULL) == -1) {
183183
fprintf(stderr, "Failed in WriteFile!");
184184
bSuccess = FALSE;
185185
goto ErrExit4;
@@ -188,7 +188,7 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
188188
//
189189
// write the bits also
190190
//
191-
if (_lwrite(hFile, (LPSTR)pBits, pbmi->bmiHeader.biSizeImage) == -1) {
191+
if (WriteFile(hFile, (LPCVOID)pBits, pbmi->bmiHeader.biSizeImage, &dwBytesWritten, NULL) == -1) {
192192
fprintf(stderr, "Failed in WriteFile!");
193193
bSuccess = FALSE;
194194
goto ErrExit4;
@@ -199,7 +199,7 @@ void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC)
199199
SelectObject(hDC, hBmpOld);
200200
DeleteObject(hTmpBmp);
201201
ErrExit3:
202-
_lclose(hFile);
202+
CloseHandle(hFile);
203203
ErrExit2:
204204
GlobalFree(pbmi);
205205
ErrExit1:

include/dumpBMP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/* There's currently no #define that indicate whether we're
44
compiling a .hc file. */
55

6-
extern void CreateBMPFile(LPCSTR pszFileName, HBITMAP hBmp, HDC hDC);
6+
extern void CreateBMPFile(LPCTSTR pszFileName, HBITMAP hBmp, HDC hDC);

0 commit comments

Comments
 (0)