Fixes
[ryzomcore.git] / tool / open_file / open_file.cpp
blobee5b77656a9059ef675953bedb97470db440f432
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // openhtml.cpp : Defines the entry point for the application.
20 #include <windows.h>
21 #include <stdlib.h>
22 #include <stdio.h>
24 LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
26 HKEY hkey;
27 LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
29 if (retval == ERROR_SUCCESS)
31 long datasize = MAX_PATH;
32 TCHAR data[MAX_PATH];
33 RegQueryValue(hkey, NULL, data, &datasize);
34 lstrcpy(retdata,data);
35 RegCloseKey(hkey);
38 return retval;
41 bool OpenFile (LPCTSTR filename, LPCTSTR ext, int showcmd)
43 TCHAR key[MAX_PATH + MAX_PATH];
45 // First try ShellExecute()
46 HINSTANCE result = ShellExecute(NULL, "open", filename, NULL,NULL, showcmd);
48 // If it failed, get the .htm regkey and lookup the program
49 if ((UINT)result <= HINSTANCE_ERROR)
51 if (GetRegKey(HKEY_CLASSES_ROOT, ext, key) == ERROR_SUCCESS)
53 lstrcat(key, "\\shell\\open\\command");
55 if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
57 TCHAR *pos;
58 pos = strstr(key, "\"%1\"");
59 if (pos == NULL) { // No quotes found
60 pos = strstr(key, "%1"); // Check for %1, without quotes
61 if (pos == NULL) // No parameter at all...
62 pos = key+lstrlen(key)-1;
63 else
64 *pos = '\0'; // Remove the parameter
66 else
67 *pos = '\0'; // Remove the parameter
69 lstrcat(pos, " ");
70 lstrcat(pos, filename);
71 int res = WinExec(key,showcmd);
72 return (res>31);
76 else
77 return true;
79 return false;
82 int APIENTRY WinMain(HINSTANCE hInstance,
83 HINSTANCE hPrevInstance,
84 LPSTR lpCmdLine,
85 int nCmdShow)
87 // TODO: Place code here.
88 if (strcmp (lpCmdLine, "")==0)
90 MessageBox (NULL, "Open a file with the default application.\nUsage: open_file [filepath]", "Open file", MB_OK|MB_ICONINFORMATION);
92 else
94 char ext[512];
95 _splitpath (lpCmdLine, NULL, NULL, NULL, ext);
96 OpenFile(lpCmdLine, ext, SW_SHOW);
99 return 0;