@@ -61,6 +61,7 @@ static LPCTSTR sharedMemBuf;
61
61
static TCHAR sharedHwndName [] = TEXT ("Local\\PT2CloneHwnd" );
62
62
static TCHAR sharedFileName [] = TEXT ("Local\\PT2CloneFilename" );
63
63
static bool handleSingleInstancing (int32_t argc , char * * argv );
64
+ static void closeSingleInstancing (void );
64
65
static void handleSysMsg (SDL_Event inputEvent );
65
66
#endif
66
67
@@ -233,10 +234,7 @@ int main(int argc, char *argv[])
233
234
makeSureDirIsProgramDir ();
234
235
#endif
235
236
236
- hpc_Init ();
237
- hpc_SetDurationInHz (& video .vblankHpc , VBLANK_HZ );
238
-
239
- if (!initializeVars () || !initKaiserTable ())
237
+ if (!initializeVars ())
240
238
{
241
239
cleanUp ();
242
240
SDL_Quit ();
@@ -264,7 +262,10 @@ int main(int argc, char *argv[])
264
262
SDL_EventState (SDL_SYSWMEVENT , SDL_ENABLE );
265
263
#endif
266
264
267
- if (!setupAudio () || !unpackBMPs ())
265
+ hpc_Init ();
266
+ hpc_SetDurationInHz (& video .vblankHpc , VBLANK_HZ );
267
+
268
+ if (!initKaiserTable () || !setupAudio () || !unpackBMPs ())
268
269
{
269
270
cleanUp ();
270
271
SDL_Quit ();
@@ -772,6 +773,7 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
772
773
if (sharedMemBuf != NULL )
773
774
{
774
775
memcpy (& hWnd_to , sharedMemBuf , sizeof (HWND ));
776
+
775
777
UnmapViewOfFile (sharedMemBuf );
776
778
sharedMemBuf = NULL ;
777
779
CloseHandle (hMapFile );
@@ -784,11 +786,12 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
784
786
if (sharedMemBuf != NULL )
785
787
{
786
788
strcpy ((char * )sharedMemBuf , argv [1 ]);
789
+
787
790
UnmapViewOfFile (sharedMemBuf );
788
791
sharedMemBuf = NULL ;
789
792
790
793
SendMessage (hWnd_to , SYSMSG_FILE_ARG , 0 , 0 );
791
- SDL_Delay (80 ); // wait a bit to make sure first instance received msg
794
+ Sleep (80 ); // wait a bit to make sure first instance received msg
792
795
793
796
CloseHandle (hMapFile );
794
797
hMapFile = NULL ;
@@ -809,31 +812,39 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
809
812
810
813
static void handleSysMsg (SDL_Event inputEvent )
811
814
{
812
- SDL_SysWMmsg * wmMsg ;
815
+ if (inputEvent .type != SDL_SYSWMEVENT )
816
+ return ;
813
817
814
- if (inputEvent .type == SDL_SYSWMEVENT )
818
+ SDL_SysWMmsg * wmMsg = inputEvent .syswm .msg ;
819
+ if (wmMsg -> subsystem == SDL_SYSWM_WINDOWS && wmMsg -> msg .win .msg == SYSMSG_FILE_ARG )
815
820
{
816
- wmMsg = inputEvent . syswm . msg ;
817
- if (wmMsg -> subsystem == SDL_SYSWM_WINDOWS && wmMsg -> msg . win . msg == SYSMSG_FILE_ARG )
821
+ hMapFile = OpenFileMapping ( FILE_MAP_READ , FALSE, sharedFileName ) ;
822
+ if (hMapFile != NULL )
818
823
{
819
- hMapFile = OpenFileMapping ( FILE_MAP_READ , FALSE, sharedFileName );
820
- if (hMapFile != NULL )
824
+ sharedMemBuf = ( LPTSTR ) MapViewOfFile ( hMapFile , FILE_MAP_READ , 0 , 0 , ARGV_SHARED_MEM_MAX_LEN );
825
+ if (sharedMemBuf != NULL )
821
826
{
822
- sharedMemBuf = (LPTSTR )MapViewOfFile (hMapFile , FILE_MAP_READ , 0 , 0 , ARGV_SHARED_MEM_MAX_LEN );
823
- if (sharedMemBuf != NULL )
824
- {
825
- loadDroppedFile ((char * )sharedMemBuf , (uint32_t )strlen (sharedMemBuf ), true, true);
826
- UnmapViewOfFile (sharedMemBuf );
827
- sharedMemBuf = NULL ;
828
- }
827
+ loadDroppedFile ((char * )sharedMemBuf , (uint32_t )strlen (sharedMemBuf ), true, true);
829
828
830
- CloseHandle ( hMapFile );
831
- hMapFile = NULL ;
829
+ UnmapViewOfFile ( sharedMemBuf );
830
+ sharedMemBuf = NULL ;
832
831
}
832
+
833
+ CloseHandle (hMapFile );
834
+ hMapFile = NULL ;
833
835
}
834
836
}
835
837
}
836
838
839
+ void closeSingleInstancing (void )
840
+ {
841
+ if (oneInstHandle != NULL )
842
+ {
843
+ CloseHandle (oneInstHandle );
844
+ oneInstHandle = NULL ;
845
+ }
846
+ }
847
+
837
848
static LONG WINAPI exceptionHandler (EXCEPTION_POINTERS * ptr )
838
849
#else
839
850
static void exceptionHandler (int32_t signal )
@@ -898,6 +909,10 @@ static void cleanUp(void) // never call this inside the main loop!
898
909
#ifndef _DEBUG
899
910
UnhookWindowsHookEx (g_hKeyboardHook );
900
911
#endif
901
- if (oneInstHandle != NULL ) CloseHandle (oneInstHandle );
912
+ if (oneInstHandle != NULL )
913
+ {
914
+ CloseHandle (oneInstHandle );
915
+ oneInstHandle = NULL ;
916
+ }
902
917
#endif
903
918
}
0 commit comments