pmddeml.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /****************************************************************************
  39. PROGRAM: pmddeml.c
  40. PURPOSE: DDEML interface with ProgMan
  41. ****************************************************************************/
  42. #include <windows.h> // required for all Windows applications
  43. #include <ddeml.h> // required for DDEML
  44. #include <stdio.h> // required for strcpy and strlen
  45. #include "nt/ntos.h" // specific to this program
  46. BOOL PMDDEML_SendShellCommand (DWORD idInst, LPSTR lpCommand);
  47. HDDEDATA CALLBACK PMDDEML_DdeCallback( UINT uType, // transaction type
  48. UINT uFmt, // clipboard data format
  49. HCONV hconv, // handle of the conversation
  50. HSZ hsz1, // handle of a string
  51. HSZ hsz2, // handle of a string
  52. HDDEDATA hdata,// handle of a global memory object
  53. DWORD dwData1, // transaction-specific data
  54. DWORD dwData2 // transaction-specific data
  55. );
  56. /****************************************************************************
  57. FUNCTION: PMDDEML_Open()
  58. PURPOSE: Open PMDDEML interface
  59. PARAMETERS:
  60. RETURNS:
  61. DWORD handle used in subsequent calls
  62. ****************************************************************************/
  63. DWORD PMDDEML_Open ( void )
  64. {
  65. DWORD idInst = 0L; // instance identifier
  66. // register this app with the DDEML
  67. if (DdeInitialize(&idInst, // receives instance ID
  68. (PFNCALLBACK)PMDDEML_DdeCallback, // address of callback function
  69. APPCMD_CLIENTONLY, // this is a client app
  70. 0L)) // reserved
  71. return 0;
  72. return idInst;
  73. }
  74. /****************************************************************************
  75. FUNCTION: PMDDEML_Close()
  76. PURPOSE: Closes PMDDEML interface
  77. PARAMETERS:
  78. DWORD idInst handle returned by PMDDEML_Open
  79. RETURNS:
  80. TRUE, if successful
  81. ****************************************************************************/
  82. BOOL PMDDEML_Close ( DWORD idInst )
  83. {
  84. // free all DDEML resources associated with this app
  85. return DdeUninitialize ( idInst );
  86. }
  87. /****************************************************************************
  88. FUNCTION: PMDDEML_CreateProgramManagerGroup()
  89. PURPOSE: Creates a program group
  90. PARAMETERS:
  91. DWORD idInst handle returned by PMDDEML_Open
  92. LPCTSTR lpszGroupNamee name of group
  93. RETURNS:
  94. TRUE, if successful
  95. ****************************************************************************/
  96. BOOL PMDDEML_CreateProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  97. {
  98. char szDDEMsg[256]; // instance identifier
  99. if ( lpszGroupName == NULL )
  100. return FALSE;
  101. sprintf ( szDDEMsg, "[CreateGroup(%s)]", lpszGroupName );
  102. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  103. return FALSE;
  104. return TRUE;
  105. }
  106. /****************************************************************************
  107. FUNCTION: PMDDEML_DeleteProgramManagerGroup()
  108. PURPOSE: Deletes a program group
  109. PARAMETERS:
  110. DWORD idInst handle returned by PMDDEML_Open
  111. LPCTSTR lpszGroupNamee name of group
  112. RETURNS:
  113. TRUE, if successful
  114. ****************************************************************************/
  115. BOOL PMDDEML_DeleteProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  116. {
  117. char szDDEMsg[256]; // instance identifier
  118. if ( lpszGroupName == NULL )
  119. return FALSE;
  120. sprintf ( szDDEMsg, "[DeleteGroup(%s)]", lpszGroupName );
  121. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  122. return FALSE;
  123. return TRUE;
  124. }
  125. BOOL PMDDEML_DeleteProgramCommonManagerGroup ( DWORD idInst,
  126. LPCTSTR lpszGroupName )
  127. {
  128. char szDDEMsg[256]; // instance identifier
  129. if ( lpszGroupName == NULL )
  130. return FALSE;
  131. sprintf ( szDDEMsg, "[DeleteGroup(%s,1)]", lpszGroupName );
  132. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  133. return FALSE;
  134. return TRUE;
  135. }
  136. /****************************************************************************
  137. FUNCTION: PMDDEML_ShowProgramManagerGroup()
  138. PURPOSE: Deletes a program group
  139. PARAMETERS:
  140. DWORD idInst handle returned by PMDDEML_Open
  141. LPCTSTR lpszGroupNamee name of group
  142. RETURNS:
  143. TRUE, if successful
  144. ****************************************************************************/
  145. BOOL PMDDEML_ShowProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  146. {
  147. char szDDEMsg[256]; // instance identifier
  148. if ( lpszGroupName == NULL )
  149. return FALSE;
  150. sprintf ( szDDEMsg, "[ShowGroup(%s,1)]", lpszGroupName );
  151. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  152. return FALSE;
  153. return TRUE;
  154. }
  155. BOOL PMDDEML_ShowProgramManagerCommonGroup ( DWORD idInst,
  156. LPCTSTR lpszGroupName )
  157. {
  158. char szDDEMsg[256]; // instance identifier
  159. if ( lpszGroupName == NULL )
  160. return FALSE;
  161. sprintf ( szDDEMsg, "[ShowGroup(%s,1,1)]", lpszGroupName );
  162. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  163. return FALSE;
  164. return TRUE;
  165. }
  166. /****************************************************************************
  167. FUNCTION: PMDDEML_AddIconToProgramManagerGroup()
  168. PURPOSE: Deletes icon a program group
  169. PARAMETERS:
  170. DWORD idInst handle returned by PMDDEML_Open
  171. LPCTSTR lpszCmdLine title of icon in group
  172. LPCTSTR lpszTitle title of icon in group
  173. LPCTSTR lpszWorkingDir title of icon in group
  174. BOOL bReplace True, if icon should be replaced
  175. RETURNS:
  176. TRUE, if successful
  177. ****************************************************************************/
  178. BOOL PMDDEML_AddIconToProgramManagerGroup ( DWORD idInst, LPCTSTR lpszCmdLine,
  179. LPCTSTR lpszTitle, LPCTSTR lpszIconPath, LPCTSTR lpszWorkingDir, BOOL bReplace )
  180. {
  181. char szDDEMsg[256]; // instance identifier
  182. if ( ( lpszCmdLine == NULL ) || ( lpszTitle == NULL ) || ( lpszWorkingDir == NULL ) )
  183. return FALSE;
  184. if ( bReplace ) {
  185. sprintf ( szDDEMsg, "[ReplaceItem(%s)]", lpszTitle );
  186. PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg );
  187. }
  188. sprintf ( szDDEMsg, "[AddItem(%s,%s,%s,,,,%s)]", lpszCmdLine, lpszTitle,
  189. lpszIconPath, lpszWorkingDir );
  190. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  191. return FALSE;
  192. return TRUE;
  193. }
  194. /****************************************************************************
  195. FUNCTION: PMDDEML_DeleteIconInProgramManagerGroup()
  196. PURPOSE: Deletes icon a program group
  197. PARAMETERS:
  198. DWORD idInst handle returned by PMDDEML_Open
  199. LPCTSTR lpszTitle title of icon in group
  200. RETURNS:
  201. TRUE, if successful
  202. ****************************************************************************/
  203. BOOL PMDDEML_DeleteIconInProgramManagerGroup ( DWORD idInst, LPCTSTR lpszTitle )
  204. {
  205. char szDDEMsg[256]; // instance identifier
  206. if ( lpszTitle == NULL )
  207. return FALSE;
  208. sprintf ( szDDEMsg, "[DeleteItem(%s)]", lpszTitle );
  209. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  210. return FALSE;
  211. return TRUE;
  212. }
  213. /****************************************************************************
  214. FUNCTION: PMDDEML_DdeCallback()
  215. PURPOSE: Processes messages for DDEML conversation
  216. PARAMETERS:
  217. UINT uType, // transaction type
  218. UINT uFmt, // clipboard data format
  219. HCONV hconv, // handle of the conversation
  220. HSZ hsz1, // handle of a string
  221. HSZ hsz2, // handle of a string
  222. HDDEDATA hdata,// handle of a global memory object
  223. DWORD dwData1, // transaction-specific data
  224. DWORD dwData2 // transaction-specific data
  225. RETURNS:
  226. HDDEDATA
  227. ****************************************************************************/
  228. HDDEDATA CALLBACK PMDDEML_DdeCallback( UINT uType, // transaction type
  229. UINT uFmt, // clipboard data format
  230. HCONV hconv, // handle of the conversation
  231. HSZ hsz1, // handle of a string
  232. HSZ hsz2, // handle of a string
  233. HDDEDATA hdata,// handle of a global memory object
  234. DWORD dwData1, // transaction-specific data
  235. DWORD dwData2 // transaction-specific data
  236. )
  237. {
  238. // Nothing need be done here...
  239. return (HDDEDATA)NULL;
  240. }
  241. /****************************************************************************
  242. FUNCTION: PMDDEML_SendShellCommand()
  243. PURPOSE: Sends the given command string to Program Manager
  244. PARAMETERS:
  245. LPSTR - pointer to command string
  246. RETURNS:
  247. BOOL - TRUE if this function succeeds, FALSE otherwise
  248. ****************************************************************************/
  249. BOOL PMDDEML_SendShellCommand (DWORD idInst, // instance identifier
  250. LPSTR lpCommand) // command string to execute
  251. {
  252. HSZ hszServTop; // Service and Topic name are "PROGMAN"
  253. HCONV hconv; // handle of conversation
  254. int nLen; // length of command string
  255. HDDEDATA hData; // return value of DdeClientTransaction
  256. DWORD dwResult; // result of transaction
  257. BOOL bResult=FALSE; // TRUE if this function is successful
  258. // create string handle to service/topic
  259. hszServTop = DdeCreateStringHandle(idInst, "PROGMAN", CP_WINANSI);
  260. // attempt to start conversation with server app
  261. if ((hconv = DdeConnect(idInst, hszServTop, hszServTop, NULL))!= NULL)
  262. {
  263. // get length of the command string
  264. nLen = lstrlen((LPSTR)lpCommand);
  265. // send command to server app
  266. hData = DdeClientTransaction((LPBYTE)lpCommand, // data to pass
  267. nLen + 1, // length of data
  268. hconv, // handle of conversation
  269. NULL, // handle of name-string
  270. CF_TEXT, // clipboard format
  271. XTYP_EXECUTE, // transaction type
  272. 1000, // timeout duration
  273. &dwResult); // points to transaction result
  274. if (hData)
  275. bResult = TRUE;
  276. // end conversation
  277. DdeDisconnect(hconv);
  278. }
  279. // free service/topic string handle
  280. DdeFreeStringHandle(idInst, hszServTop);
  281. return bResult;
  282. }
  283. /****************************************************************************
  284. FUNCTION: PMDDEML_GetProgramGroupInfo()
  285. PURPOSE: Gets group info from progman
  286. PARAMETERS:
  287. LPSTR - pointer to command string
  288. RETURNS:
  289. BOOL - TRUE if this function succeeds, FALSE otherwise
  290. ****************************************************************************/
  291. BOOL PMDDEML_GetProgramGroupInfo(DWORD idInst, LPSTR lpProgramGroup, char *szBuffer, DWORD cbBuffer)
  292. {
  293. HSZ hszServTop; // Service and Topic name are "PROGMAN"
  294. HSZ hszTopic; // Topic name is the lpRequest
  295. HCONV hconv; // handle of conversation
  296. HDDEDATA hData = 0; // return value of DdeClientTransaction
  297. DWORD dwResult; // result of transaction
  298. BOOL bResult=FALSE; // TRUE if this function is successful
  299. hszServTop = DdeCreateStringHandle(idInst, "PROGMAN", CP_WINANSI);
  300. hszTopic = DdeCreateStringHandle(idInst, lpProgramGroup, CP_WINANSI);
  301. if((hconv = DdeConnect(idInst, hszServTop, hszServTop, NULL)) != NULL)
  302. {
  303. hData = DdeClientTransaction((LPBYTE)NULL, // data to pass
  304. 0L, // length of data
  305. hconv, // handle of conversation
  306. hszTopic, // handle of name-string
  307. CF_TEXT, // clipboard format
  308. XTYP_REQUEST, // transaction type
  309. 5000, // timeout duration
  310. &dwResult); // points to transaction result
  311. bResult = (BOOL)DdeGetData(hData, (void FAR*)szBuffer, cbBuffer, 0);
  312. DdeDisconnect(hconv);
  313. }
  314. // free service/topic string handle
  315. DdeFreeStringHandle(idInst, hszServTop);
  316. DdeFreeStringHandle(idInst, hszTopic);
  317. return bResult;
  318. }