pmddeml.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /****************************************************************************
  42. PROGRAM: pmddeml.c
  43. PURPOSE: DDEML interface with ProgMan
  44. ****************************************************************************/
  45. #include <windows.h> // required for all Windows applications
  46. #include <ddeml.h> // required for DDEML
  47. #include <stdio.h> // required for strcpy and strlen
  48. #include "nt/ntos.h" // specific to this program
  49. BOOL PMDDEML_SendShellCommand (DWORD idInst, LPSTR lpCommand);
  50. HDDEDATA CALLBACK PMDDEML_DdeCallback( UINT uType, // transaction type
  51. UINT uFmt, // clipboard data format
  52. HCONV hconv, // handle of the conversation
  53. HSZ hsz1, // handle of a string
  54. HSZ hsz2, // handle of a string
  55. HDDEDATA hdata,// handle of a global memory object
  56. DWORD dwData1, // transaction-specific data
  57. DWORD dwData2 // transaction-specific data
  58. );
  59. /****************************************************************************
  60. FUNCTION: PMDDEML_Open()
  61. PURPOSE: Open PMDDEML interface
  62. PARAMETERS:
  63. RETURNS:
  64. DWORD handle used in subsequent calls
  65. ****************************************************************************/
  66. DWORD PMDDEML_Open ( void )
  67. {
  68. DWORD idInst = 0L; // instance identifier
  69. // register this app with the DDEML
  70. if (DdeInitialize(&idInst, // receives instance ID
  71. (PFNCALLBACK)PMDDEML_DdeCallback, // address of callback function
  72. APPCMD_CLIENTONLY, // this is a client app
  73. 0L)) // reserved
  74. return 0;
  75. return idInst;
  76. }
  77. /****************************************************************************
  78. FUNCTION: PMDDEML_Close()
  79. PURPOSE: Closes PMDDEML interface
  80. PARAMETERS:
  81. DWORD idInst handle returned by PMDDEML_Open
  82. RETURNS:
  83. TRUE, if successful
  84. ****************************************************************************/
  85. BOOL PMDDEML_Close ( DWORD idInst )
  86. {
  87. // free all DDEML resources associated with this app
  88. return DdeUninitialize ( idInst );
  89. }
  90. /****************************************************************************
  91. FUNCTION: PMDDEML_CreateProgramManagerGroup()
  92. PURPOSE: Creates a program group
  93. PARAMETERS:
  94. DWORD idInst handle returned by PMDDEML_Open
  95. LPCTSTR lpszGroupNamee name of group
  96. RETURNS:
  97. TRUE, if successful
  98. ****************************************************************************/
  99. BOOL PMDDEML_CreateProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  100. {
  101. char szDDEMsg[256]; // instance identifier
  102. if ( lpszGroupName == NULL )
  103. return FALSE;
  104. sprintf ( szDDEMsg, "[CreateGroup(%s)]", lpszGroupName );
  105. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  106. return FALSE;
  107. return TRUE;
  108. }
  109. /****************************************************************************
  110. FUNCTION: PMDDEML_DeleteProgramManagerGroup()
  111. PURPOSE: Deletes a program group
  112. PARAMETERS:
  113. DWORD idInst handle returned by PMDDEML_Open
  114. LPCTSTR lpszGroupNamee name of group
  115. RETURNS:
  116. TRUE, if successful
  117. ****************************************************************************/
  118. BOOL PMDDEML_DeleteProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  119. {
  120. char szDDEMsg[256]; // instance identifier
  121. if ( lpszGroupName == NULL )
  122. return FALSE;
  123. sprintf ( szDDEMsg, "[DeleteGroup(%s)]", lpszGroupName );
  124. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  125. return FALSE;
  126. return TRUE;
  127. }
  128. BOOL PMDDEML_DeleteProgramCommonManagerGroup ( DWORD idInst,
  129. LPCTSTR lpszGroupName )
  130. {
  131. char szDDEMsg[256]; // instance identifier
  132. if ( lpszGroupName == NULL )
  133. return FALSE;
  134. sprintf ( szDDEMsg, "[DeleteGroup(%s,1)]", lpszGroupName );
  135. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  136. return FALSE;
  137. return TRUE;
  138. }
  139. /****************************************************************************
  140. FUNCTION: PMDDEML_ShowProgramManagerGroup()
  141. PURPOSE: Deletes a program group
  142. PARAMETERS:
  143. DWORD idInst handle returned by PMDDEML_Open
  144. LPCTSTR lpszGroupNamee name of group
  145. RETURNS:
  146. TRUE, if successful
  147. ****************************************************************************/
  148. BOOL PMDDEML_ShowProgramManagerGroup ( DWORD idInst, LPCTSTR lpszGroupName )
  149. {
  150. char szDDEMsg[256]; // instance identifier
  151. if ( lpszGroupName == NULL )
  152. return FALSE;
  153. sprintf ( szDDEMsg, "[ShowGroup(%s,1)]", lpszGroupName );
  154. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  155. return FALSE;
  156. return TRUE;
  157. }
  158. BOOL PMDDEML_ShowProgramManagerCommonGroup ( DWORD idInst,
  159. LPCTSTR lpszGroupName )
  160. {
  161. char szDDEMsg[256]; // instance identifier
  162. if ( lpszGroupName == NULL )
  163. return FALSE;
  164. sprintf ( szDDEMsg, "[ShowGroup(%s,1,1)]", lpszGroupName );
  165. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  166. return FALSE;
  167. return TRUE;
  168. }
  169. /****************************************************************************
  170. FUNCTION: PMDDEML_AddIconToProgramManagerGroup()
  171. PURPOSE: Deletes icon a program group
  172. PARAMETERS:
  173. DWORD idInst handle returned by PMDDEML_Open
  174. LPCTSTR lpszCmdLine title of icon in group
  175. LPCTSTR lpszTitle title of icon in group
  176. LPCTSTR lpszWorkingDir title of icon in group
  177. BOOL bReplace True, if icon should be replaced
  178. RETURNS:
  179. TRUE, if successful
  180. ****************************************************************************/
  181. BOOL PMDDEML_AddIconToProgramManagerGroup ( DWORD idInst, LPCTSTR lpszCmdLine,
  182. LPCTSTR lpszTitle, LPCTSTR lpszIconPath, LPCTSTR lpszWorkingDir, BOOL bReplace )
  183. {
  184. char szDDEMsg[256]; // instance identifier
  185. if ( ( lpszCmdLine == NULL ) || ( lpszTitle == NULL ) || ( lpszWorkingDir == NULL ) )
  186. return FALSE;
  187. if ( bReplace ) {
  188. sprintf ( szDDEMsg, "[ReplaceItem(%s)]", lpszTitle );
  189. PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg );
  190. }
  191. sprintf ( szDDEMsg, "[AddItem(%s,%s,%s,,,,%s)]", lpszCmdLine, lpszTitle,
  192. lpszIconPath, lpszWorkingDir );
  193. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  194. return FALSE;
  195. return TRUE;
  196. }
  197. /****************************************************************************
  198. FUNCTION: PMDDEML_DeleteIconInProgramManagerGroup()
  199. PURPOSE: Deletes icon a program group
  200. PARAMETERS:
  201. DWORD idInst handle returned by PMDDEML_Open
  202. LPCTSTR lpszTitle title of icon in group
  203. RETURNS:
  204. TRUE, if successful
  205. ****************************************************************************/
  206. BOOL PMDDEML_DeleteIconInProgramManagerGroup ( DWORD idInst, LPCTSTR lpszTitle )
  207. {
  208. char szDDEMsg[256]; // instance identifier
  209. if ( lpszTitle == NULL )
  210. return FALSE;
  211. sprintf ( szDDEMsg, "[DeleteItem(%s)]", lpszTitle );
  212. if ( !PMDDEML_SendShellCommand(idInst, (LPSTR)szDDEMsg ) )
  213. return FALSE;
  214. return TRUE;
  215. }
  216. /****************************************************************************
  217. FUNCTION: PMDDEML_DdeCallback()
  218. PURPOSE: Processes messages for DDEML conversation
  219. PARAMETERS:
  220. UINT uType, // transaction type
  221. UINT uFmt, // clipboard data format
  222. HCONV hconv, // handle of the conversation
  223. HSZ hsz1, // handle of a string
  224. HSZ hsz2, // handle of a string
  225. HDDEDATA hdata,// handle of a global memory object
  226. DWORD dwData1, // transaction-specific data
  227. DWORD dwData2 // transaction-specific data
  228. RETURNS:
  229. HDDEDATA
  230. ****************************************************************************/
  231. HDDEDATA CALLBACK PMDDEML_DdeCallback( UINT uType, // transaction type
  232. UINT uFmt, // clipboard data format
  233. HCONV hconv, // handle of the conversation
  234. HSZ hsz1, // handle of a string
  235. HSZ hsz2, // handle of a string
  236. HDDEDATA hdata,// handle of a global memory object
  237. DWORD dwData1, // transaction-specific data
  238. DWORD dwData2 // transaction-specific data
  239. )
  240. {
  241. // Nothing need be done here...
  242. return (HDDEDATA)NULL;
  243. }
  244. /****************************************************************************
  245. FUNCTION: PMDDEML_SendShellCommand()
  246. PURPOSE: Sends the given command string to Program Manager
  247. PARAMETERS:
  248. LPSTR - pointer to command string
  249. RETURNS:
  250. BOOL - TRUE if this function succeeds, FALSE otherwise
  251. ****************************************************************************/
  252. BOOL PMDDEML_SendShellCommand (DWORD idInst, // instance identifier
  253. LPSTR lpCommand) // command string to execute
  254. {
  255. HSZ hszServTop; // Service and Topic name are "PROGMAN"
  256. HCONV hconv; // handle of conversation
  257. int nLen; // length of command string
  258. HDDEDATA hData; // return value of DdeClientTransaction
  259. DWORD dwResult; // result of transaction
  260. BOOL bResult=FALSE; // TRUE if this function is successful
  261. // create string handle to service/topic
  262. hszServTop = DdeCreateStringHandle(idInst, "PROGMAN", CP_WINANSI);
  263. // attempt to start conversation with server app
  264. if ((hconv = DdeConnect(idInst, hszServTop, hszServTop, NULL))!= NULL)
  265. {
  266. // get length of the command string
  267. nLen = lstrlen((LPSTR)lpCommand);
  268. // send command to server app
  269. hData = DdeClientTransaction((LPBYTE)lpCommand, // data to pass
  270. nLen + 1, // length of data
  271. hconv, // handle of conversation
  272. NULL, // handle of name-string
  273. CF_TEXT, // clipboard format
  274. XTYP_EXECUTE, // transaction type
  275. 1000, // timeout duration
  276. &dwResult); // points to transaction result
  277. if (hData)
  278. bResult = TRUE;
  279. // end conversation
  280. DdeDisconnect(hconv);
  281. }
  282. // free service/topic string handle
  283. DdeFreeStringHandle(idInst, hszServTop);
  284. return bResult;
  285. }
  286. /****************************************************************************
  287. FUNCTION: PMDDEML_GetProgramGroupInfo()
  288. PURPOSE: Gets group info from progman
  289. PARAMETERS:
  290. LPSTR - pointer to command string
  291. RETURNS:
  292. BOOL - TRUE if this function succeeds, FALSE otherwise
  293. ****************************************************************************/
  294. BOOL PMDDEML_GetProgramGroupInfo(DWORD idInst, LPSTR lpProgramGroup, char *szBuffer, DWORD cbBuffer)
  295. {
  296. HSZ hszServTop; // Service and Topic name are "PROGMAN"
  297. HSZ hszTopic; // Topic name is the lpRequest
  298. HCONV hconv; // handle of conversation
  299. HDDEDATA hData = 0; // return value of DdeClientTransaction
  300. DWORD dwResult; // result of transaction
  301. BOOL bResult=FALSE; // TRUE if this function is successful
  302. hszServTop = DdeCreateStringHandle(idInst, "PROGMAN", CP_WINANSI);
  303. hszTopic = DdeCreateStringHandle(idInst, lpProgramGroup, CP_WINANSI);
  304. if((hconv = DdeConnect(idInst, hszServTop, hszServTop, NULL)) != NULL)
  305. {
  306. hData = DdeClientTransaction((LPBYTE)NULL, // data to pass
  307. 0L, // length of data
  308. hconv, // handle of conversation
  309. hszTopic, // handle of name-string
  310. CF_TEXT, // clipboard format
  311. XTYP_REQUEST, // transaction type
  312. 5000, // timeout duration
  313. &dwResult); // points to transaction result
  314. bResult = (BOOL)DdeGetData(hData, (void FAR*)szBuffer, cbBuffer, 0);
  315. DdeDisconnect(hconv);
  316. }
  317. // free service/topic string handle
  318. DdeFreeStringHandle(idInst, hszServTop);
  319. DdeFreeStringHandle(idInst, hszTopic);
  320. return bResult;
  321. }