Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
6.6 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. dbsupport.cpp
  5. Abstract:
  6. Author:
  7. clupu created 04/11/2001
  8. Revision History:
  9. --*/
  10. #include "stdafx.h"
  11. #include "appverif.h"
  12. #include "AVUtil.h"
  13. #include "dbsupport.h"
  14. #include "log.h"
  15. char g_szXML[2048];
  16. TCHAR g_szCmd[1024];
  17. BOOL AppCompatSaveSettings( CStringArray &astrExeNames )
  18. {
  19. char szBuff[256] = "";
  20. TCHAR szTempPath[MAX_PATH] = _T("");
  21. TCHAR szXmlFile[MAX_PATH] = _T("");
  22. TCHAR szSdbFile[MAX_PATH] = _T("");
  23. HANDLE hFile = INVALID_HANDLE_VALUE;
  24. DWORD bytesWritten;
  25. STARTUPINFO si;
  26. PROCESS_INFORMATION pi;
  27. BOOL bReturn = FALSE;
  28. INT_PTR nCount;
  29. char szExeName[128];
  30. nCount = astrExeNames.GetSize();
  31. if ( nCount == 0 )
  32. {
  33. return AppCompatDeleteSettings();
  34. }
  35. //
  36. // Check for shimdbc.exe
  37. //
  38. GetSystemWindowsDirectory(szTempPath, MAX_PATH);
  39. lstrcat(szTempPath, _T("\\shimdbc.exe"));
  40. hFile = CreateFile(szTempPath,
  41. GENERIC_READ,
  42. 0,
  43. NULL,
  44. OPEN_EXISTING,
  45. FILE_ATTRIBUTE_NORMAL,
  46. NULL);
  47. if ( hFile == INVALID_HANDLE_VALUE )
  48. {
  49. AVMesssageFromResource( IDS_NO_SHIMDBC );
  50. return FALSE;
  51. }
  52. CloseHandle(hFile);
  53. //
  54. // Construct the XML...
  55. //
  56. lstrcpyA(g_szXML,
  57. "<?xml version=\"1.0\"?>\r\n"
  58. "<DATABASE NAME=\"Application Verifier Database\" ID=\"{448850f4-a5ea-4dd1-bf1b-d5fa285dc64b}\">\r\n"
  59. " <APP NAME=\"All EXEs to be verified\" VENDOR=\"Various\">\r\n");
  60. for ( INT_PTR i = 0; i < nCount; i++ )
  61. {
  62. //
  63. // Convert the EXE name to ANSI
  64. //
  65. WideCharToMultiByte(CP_ACP,
  66. 0,
  67. (LPCTSTR)astrExeNames.GetAt(i),
  68. -1,
  69. szExeName,
  70. 128,
  71. NULL,
  72. NULL);
  73. wsprintfA(szBuff,
  74. " <EXE NAME=\"%s\">\r\n"
  75. " <LAYER NAME=\"AppVerifierLayer\"/>\r\n"
  76. " </EXE>\r\n",
  77. szExeName);
  78. lstrcatA(g_szXML, szBuff);
  79. }
  80. lstrcatA(g_szXML,
  81. " </APP>\r\n"
  82. "</DATABASE>");
  83. if ( GetTempPath(MAX_PATH, szTempPath) == 0 )
  84. {
  85. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] GetTempPath failed."));
  86. goto cleanup;
  87. }
  88. //
  89. // Obtain a temp name for the XML file
  90. //
  91. if ( GetTempFileName(szTempPath, _T("XML"), NULL, szXmlFile) == 0 )
  92. {
  93. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] GetTempFilePath for XML failed."));
  94. goto cleanup;
  95. }
  96. hFile = CreateFile(szXmlFile,
  97. GENERIC_WRITE,
  98. 0,
  99. NULL,
  100. CREATE_ALWAYS,
  101. FILE_ATTRIBUTE_NORMAL,
  102. NULL);
  103. if ( hFile == INVALID_HANDLE_VALUE )
  104. {
  105. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] CreateFile '%s' failed 0x%X."),
  106. szXmlFile, GetLastError());
  107. goto cleanup;
  108. }
  109. if ( WriteFile(hFile, g_szXML, lstrlenA(g_szXML), &bytesWritten, NULL) == 0 )
  110. {
  111. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] WriteFile \"%s\" failed 0x%X."),
  112. szXmlFile, GetLastError());
  113. goto cleanup;
  114. }
  115. CloseHandle(hFile);
  116. hFile = INVALID_HANDLE_VALUE;
  117. //
  118. // Obtain a temp name for the SDB file
  119. //
  120. wsprintf(szSdbFile, _T("%stempdb.sdb"), szTempPath);
  121. DeleteFile(szSdbFile);
  122. //
  123. // Invoke the compiler to generate the SDB file
  124. //
  125. ZeroMemory(&si, sizeof(si));
  126. si.cb = sizeof(si);
  127. wsprintf(g_szCmd, _T("shimdbc.exe fix -q \"%s\" \"%s\""), szXmlFile, szSdbFile);
  128. if ( !CreateProcess(NULL,
  129. g_szCmd,
  130. NULL,
  131. NULL,
  132. FALSE,
  133. NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
  134. NULL,
  135. NULL,
  136. &si,
  137. &pi) )
  138. {
  139. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] CreateProcess \"%s\" failed 0x%X."),
  140. g_szCmd, GetLastError());
  141. goto cleanup;
  142. }
  143. CloseHandle(pi.hThread);
  144. WaitForSingleObject(pi.hProcess, INFINITE);
  145. CloseHandle(pi.hProcess);
  146. //
  147. // The SDB file is generated. Install the database now.
  148. //
  149. ZeroMemory(&si, sizeof(si));
  150. si.cb = sizeof(si);
  151. wsprintf(g_szCmd, _T("sdbinst.exe -q \"%s\""), szSdbFile);
  152. if ( !CreateProcess(NULL,
  153. g_szCmd,
  154. NULL,
  155. NULL,
  156. FALSE,
  157. NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
  158. NULL,
  159. NULL,
  160. &si,
  161. &pi) )
  162. {
  163. LogMessage(LOG_ERROR, _T("[AppCompatSaveSettings] CreateProcess \"%s\" failed 0x%X."),
  164. g_szCmd, GetLastError());
  165. goto cleanup;
  166. }
  167. CloseHandle(pi.hThread);
  168. WaitForSingleObject(pi.hProcess, INFINITE);
  169. CloseHandle(pi.hProcess);
  170. bReturn = TRUE;
  171. cleanup:
  172. if ( hFile != INVALID_HANDLE_VALUE )
  173. {
  174. CloseHandle(hFile);
  175. }
  176. DeleteFile(szXmlFile);
  177. DeleteFile(szSdbFile);
  178. return bReturn;
  179. }
  180. BOOL AppCompatDeleteSettings( void )
  181. {
  182. STARTUPINFO si;
  183. PROCESS_INFORMATION pi;
  184. TCHAR szCmd[MAX_PATH];
  185. ZeroMemory( &si, sizeof( si ) );
  186. si.cb = sizeof( si );
  187. lstrcpy( szCmd, _T("sdbinst.exe -q -u -g {448850f4-a5ea-4dd1-bf1b-d5fa285dc64b}") );
  188. if ( !CreateProcess( NULL,
  189. szCmd,
  190. NULL,
  191. NULL,
  192. FALSE,
  193. NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
  194. NULL,
  195. NULL,
  196. &si,
  197. &pi) )
  198. {
  199. LogMessage(LOG_ERROR, _T("[AppCompatDeleteSettings] CreateProcess \"%s\" failed 0x%X."),
  200. szCmd, GetLastError());
  201. return FALSE;
  202. }
  203. CloseHandle(pi.hThread);
  204. WaitForSingleObject(pi.hProcess, INFINITE);
  205. CloseHandle(pi.hProcess);
  206. return TRUE;
  207. }