OSDN Git Service

オリジナルのtrunkからwinバイナリ用ソースをインポート。
[coroid/inqubus.git] / win / launcher / Launcher.c
1 /*\r
2  * Windows\97p\83v\83\8d\83O\83\89\83\80\83\89\83\93\83`\83\83\r
3  * copyright (c) 2008 \83Õ\81i\83v\83T\83C\81j\r
4  *\r
5  * \81u\82³\82«\82ã\82Î\82·\81v\96{\91Ì\82ðexe\8co\97R\82Å\93®\82©\82·\82½\82ß\82Ì\83\89\83\93\83`\83\83\82Å\82·\81B\r
6  *\r
7  * \82±\82Ì\83t\83@\83C\83\8b\82Í\81u\82³\82«\82ã\82Î\82·\81v\82Ì\88ê\95\94\82Å\82 \82è\81A\r
8  * \82±\82Ì\83\\81[\83X\83R\81[\83h\82ÍGPL\83\89\83C\83Z\83\93\83X\82Å\94z\95z\82³\82ê\82Ü\82·\82Å\82·\81B\r
9  */\r
10 #include <stdlib.h>\r
11 #include <stdio.h>\r
12 #include <windows.h>\r
13 \r
14 #define CMD_CHECK "java.exe"\r
15 #define CMD_RUN "java -jar Saccubus.jar"\r
16 #define CMD_LOG "[log]frontend.txt"\r
17 \r
18 int doCmd(char* command,int show_msg,const char* log_name);\r
19 \r
20 int WINAPI WinMain (HINSTANCE hInstance, \r
21                         HINSTANCE hPrevInstance, \r
22                         PSTR szCmdLine, \r
23                         int iCmdShow){\r
24                         int ret;\r
25                         if(doCmd(CMD_CHECK,FALSE,NULL) == 0){\r
26                                 if(szCmdLine && strlen(szCmdLine) > 0){\r
27                                         /*\88ø\90\94\82ª\82 \82é\8fê\8d\87\82Í\81A\98A\8c\8b\82µ\82Ä\8eÀ\8ds\81B*/\r
28 \r
29                                         //\83f\81[\83^\82ð\8eû\8fW\81B\r
30                                         const char* cmd_base = CMD_RUN;\r
31                                         int cmd_base_length = strlen(cmd_base);\r
32                                         const char* cmd_add = szCmdLine;\r
33                                         int cmd_add_length = strlen(cmd_add);\r
34                                         //\83\81\83\82\83\8a\8am\95Û\r
35                                         char* call_cmd_line = malloc(cmd_base_length+cmd_add_length+2);\r
36                                         //Java\97p\82Ì\95\94\95ª\82ð\83R\83s\81[\81Bstrcat\82Æ\82©\82Í\82 \82ñ\82Ü\82è\8eg\82¢\82½\82­\82È\82¢\81B\r
37                                         memcpy(call_cmd_line,cmd_base,cmd_base_length);\r
38                                         //\8bó\94\92\82ð\92Ç\89Á\r
39                                         call_cmd_line[cmd_base_length] = ' ';\r
40                                         //\88ø\90\94\82ð\92Ç\89Á\r
41                                         memcpy(call_cmd_line+cmd_base_length+1,cmd_add,cmd_add_length);\r
42                                         //\8dÅ\8cã\82Ì\0\82ð\96Y\82ê\82¸\82É\81B\r
43                                         call_cmd_line[cmd_base_length+cmd_add_length+1] = '\0';\r
44                                         //\83R\83}\83\93\83h\8eÀ\8ds\81B\r
45                                         ret = doCmd(call_cmd_line,TRUE,CMD_LOG);\r
46                                         //\96Y\82ê\82¸\82É\8aJ\95ú\81B\r
47                                         free(call_cmd_line);\r
48                                 }else{/*\88ø\90\94\82ª\96³\82¢\8fê\8d\87\82Í\95\81\92Ê\82É\8eÀ\8ds*/\r
49                                         ret = doCmd(CMD_RUN,TRUE,CMD_LOG);\r
50                                 }\r
51                         }else{\r
52                                 MessageBox(NULL,"Java\82ª\83C\83\93\83X\83g\81[\83\8b\82³\82ê\82Ä\82¢\82È\82¢\82æ\82¤\82Å\82·\81B","\83G\83\89\81[",MB_OK | MB_ICONERROR);\r
53                                 ret = -1;\r
54                         }\r
55                         return 0;\r
56 }\r
57 \r
58 int doCmd(char* command,int show_msg,const char* log_name){\r
59         int ret = 0;\r
60 \r
61         STARTUPINFO startup_info;\r
62         PROCESS_INFORMATION process_info;\r
63         HANDLE _hnd_out = INVALID_HANDLE_VALUE;\r
64         HANDLE hnd_out = INVALID_HANDLE_VALUE;\r
65 \r
66         process_info.hProcess = NULL;\r
67 \r
68         memset(&startup_info, 0, sizeof(STARTUPINFO));\r
69         startup_info.cb = sizeof(STARTUPINFO);\r
70 \r
71         if(log_name){\r
72                 _hnd_out = CreateFile(\r
73                         log_name,\r
74                         GENERIC_WRITE,\r
75                         FILE_SHARE_WRITE,\r
76                         NULL,\r
77                         CREATE_ALWAYS,\r
78                         FILE_ATTRIBUTE_NORMAL,\r
79                         NULL\r
80                 );\r
81                 if(_hnd_out != INVALID_HANDLE_VALUE){\r
82                         DuplicateHandle(GetCurrentProcess(), _hnd_out, GetCurrentProcess(),&hnd_out, 0, 1, DUPLICATE_SAME_ACCESS);\r
83                         if(hnd_out != INVALID_HANDLE_VALUE){\r
84                                 startup_info.dwFlags = STARTF_USESTDHANDLES;\r
85                                 startup_info.hStdOutput = hnd_out;\r
86                                 startup_info.hStdError = hnd_out;\r
87                         }\r
88                 }\r
89         }\r
90         int code = CreateProcessA(\r
91             NULL,                                               // \8eÀ\8ds\83t\83@\83C\83\8b\96¼\r
92             command,                                    // \83R\83}\83\93\83h\83\89\83C\83\93\83p\83\89\83\81\81[\83^\r
93             NULL,                                               // \83v\83\8d\83Z\83X\82Ì\95Û\8cì\91®\90«\r
94             NULL,                                               // \83X\83\8c\83b\83h\82Ì\95Û\8cì\91®\90«\r
95             TRUE,                                               // \83I\83u\83W\83F\83N\83g\83n\83\93\83h\83\8b\8cp\8f³\82Ì\83t\83\89\83O\r
96             DETACHED_PROCESS | \r
97                 CREATE_NEW_PROCESS_GROUP | \r
98                 NORMAL_PRIORITY_CLASS |\r
99                 CREATE_NO_WINDOW,               // \91®\90«\83t\83\89\83O\r
100             NULL,                                               // \8aÂ\8b«\95Ï\90\94\8fî\95ñ\82Ö\82Ì\83|\83C\83\93\83^\r
101             NULL,                                               // \8bN\93®\8e\9e\83J\83\8c\83\93\83g\83f\83B\83\8c\83N\83g\83\8a\r
102             &startup_info,                              // \83E\83B\83\93\83h\83E\95\\8e¦\90Ý\92è\r
103             &process_info                               // \83v\83\8d\83Z\83X\81E\83X\83\8c\83b\83h\82Ì\8fî\95ñ\r
104         );\r
105         if(code == 0){\r
106                 ret = -1;\r
107                 if(show_msg){\r
108                         char* msg;\r
109                         int error_code = FormatMessageA(\r
110                                 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,    // \93®\8dì\83t\83\89\83O\r
111                                 0,                                                                                                                              // \83\81\83b\83Z\81[\83W\92è\8b`\88Ê\92u\r
112                                 GetLastError(),                                                                                                 // \83\81\83b\83Z\81[\83WID\r
113                                 LANG_USER_DEFAULT,                                                                                              // \8c¾\8cêID\r
114                                 (LPTSTR)&msg,                                                                                                   // \83o\83b\83t\83@\82Ì\83A\83h\83\8c\83X\r
115                                 0,                                                                                                                              // \83o\83b\83t\83@\82Ì\83T\83C\83Y\r
116                                 0                                                                                                                               // \91}\93ü\8bå\82Ì\94z\97ñ\82Ì\83A\83h\83\8c\83X\r
117                         );\r
118                         if(error_code == 0){\r
119                                 MessageBox(NULL,"\89½\82ç\82©\82Ì\83G\83\89\81[\82ª\94­\90\82µ\82Ü\82µ\82½\81B","\83G\83\89\81[",MB_OK | MB_ICONERROR);\r
120                         }else{\r
121                                 MessageBox(NULL,msg, "\83G\83\89\81[", MB_ICONERROR|MB_OK);\r
122                         }\r
123                         LocalFree(msg);\r
124                 }\r
125         }else{\r
126                 /* //\96ß\82è\92l\82ð\8eæ\93¾\82·\82é\8fê\8d\87\82Í\83R\83\81\83\93\83g\83A\83E\83g\89ð\8f\9c\r
127                 // \8fI\97¹\82·\82é\82Ü\82Å\91Ò\82Â\r
128                 WaitForSingleObject(process_info.hProcess,INFINITE);\r
129                 //\96ß\82è\92l\82ð\8eæ\93¾\r
130                 DWORD exit_code;\r
131                 GetExitCodeProcess(process_info.hProcess, &exit_code);\r
132                 ret = exit_code;\r
133                 */\r
134                 if(_hnd_out != INVALID_HANDLE_VALUE){\r
135                         CloseHandle(_hnd_out);\r
136                 }\r
137                 if(hnd_out != INVALID_HANDLE_VALUE){\r
138                         CloseHandle(hnd_out);\r
139                 }\r
140                 CloseHandle(process_info.hThread);\r
141                 CloseHandle(process_info.hProcess);\r
142 \r
143         }\r
144         return ret;\r
145 }\r