home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- Module name: SetupPM.C
- Programmer : Jeffrey M. Richter.
- *****************************************************************************/
-
- #include "..\nowindws.h"
- #undef NOATOM
- #undef NOKERNEL
- #undef NOLSTRING
- #undef NOMEMMGR
- #undef NOMSG
- #undef NOSHOWWINDOW
- #undef NOUSER
- #undef NOWINMESSAGES
- #undef NOWINOFFSETS
- #include <windows.h>
- #include <dde.h>
-
- #include <string.h>
-
- #include "Setup.h"
- #include "SetupInf.h"
-
- char _szClassName[] = "DDEClient";
-
- typedef struct {
- HWND hWndServer;
- } WNDEB;
-
- #define DDE_WAITTIME 3000
-
- LONG FAR PASCAL DDEClientWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
- DWORD dwResult = 0, dwStopTime;
- BOOL fCallDefProc = FALSE;
- HWND hWndServer = (HWND) GETWNDEB(hWnd, WNDEB, hWndServer);
- char szBuf[100]; MSG Msg;
-
- switch (wMsg) {
-
- case WM_NCCREATE:
- dwResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
- if (dwResult == NULL) break;
-
- SendMessage(-1, WM_DDE_INITIATE, hWnd,
- (LONG) ((LPCREATESTRUCT) lParam)->lpCreateParams);
-
- if (GETWNDEB(hWnd, WNDEB, hWndServer) != NULL)
- break;
-
- // A conversation was not able to be established. Attempt to
- // execute the desired application.
- GlobalGetAtomName(LOWORD((LONG) ((LPCREATESTRUCT) lParam)->lpCreateParams),
- szBuf, sizeof(szBuf));
- WinExec(szBuf, SW_RESTORE);
-
- SendMessage(-1, WM_DDE_INITIATE, hWnd,
- (LONG) ((LPCREATESTRUCT) lParam)->lpCreateParams);
-
- if (GETWNDEB(hWnd, WNDEB, hWndServer) == NULL)
- DefWindowProc(hWnd, WM_NCDESTROY, wParam, lParam);
-
- break;
-
- case WM_DESTROY:
- PostMessage(hWndServer, WM_DDE_TERMINATE, hWnd, 0);
- SETWNDEB(hWnd, WNDEB, hWndServer, NULL);
- // From now on, do not send a WM_DDE_ACK message to the server in
- // response to any messages sent from the Server.
-
- // Wait for response from the Server.
- dwStopTime = GetTickCount() + DDE_WAITTIME;
- do {
- if (PeekMessage(&Msg, hWnd, WM_DDE_TERMINATE, WM_DDE_TERMINATE,
- PM_REMOVE)) break;
-
- } while (GetTickCount() < dwStopTime);
- break;
-
- case WM_DDE_DATA:
- if (hWndServer != (HWND) wParam) {
- // Conversation not initiated with this Server or Server sent
- // after we have terminated the conversation.
- if (HIWORD(lParam) != NULL) {
- // Data handle is not. If it were NULL, a link was set
- // using the WM_DDE_ADVISE message.
- GlobalFree(HIWORD(lParam));
- }
- GlobalDeleteAtom(LOWORD(lParam));
- }
- break;
-
- case WM_DDE_EXECUTE:
- // Because this is a Client and NOT a Server, this message was
- // sent to this window from the Setup Application. The lParam
- // parameter contains the handle of the memory containing the
- // commands to be executed by the Server.
-
- // Verify that a conversation was started and hasn't been terminated.
- if (hWndServer == NULL) break;
-
- PostMessage(hWndServer, wMsg, hWnd, lParam);
-
- // Wait for response from the Server.
- GetMessage(&Msg, hWnd, WM_DDE_ACK, WM_DDE_ACK);
-
- // Return whether the command was acknowledged successfully.
- wParam = LOWORD(Msg.lParam);
- dwResult = ((DDEACK *) &wParam)->fAck;
- break;
-
- case WM_DDE_TERMINATE:
- if (hWndServer == NULL) break;
- // The Server has terminated the conversation with us.
- // We must send the WM_DDE_TERMINATE message back to the server.
- PostMessage(hWndServer, WM_DDE_TERMINATE, hWnd, 0);
- SETWNDEB(hWnd, WNDEB, hWndServer, (HWND) NULL);
- break;
-
- case WM_DDE_ACK:
- if (hWndServer == NULL) {
- // No conversation initiated, WM_DDE_ACK must be from a
- // potential server that just received my WM_DDE_INITIATE message.
- SETWNDEB(hWnd, WNDEB, hWndServer, (HWND) wParam);
- break;
- }
-
- // WM_DDE_ACK message received from a potential Server but we have
- // already established a conversation with another Server. Tell the
- // Server that we do not wish to continue our conversation with it.
- PostMessage((HWND) wParam, WM_DDE_TERMINATE, hWnd, 0);
- break;
-
- default:
- fCallDefProc = TRUE;
- break;
- }
-
- if (fCallDefProc)
- dwResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
-
- return(dwResult);
- }
-
-
- BOOL FAR PASCAL RegisterDDEClient (HANDLE hInstance) {
- WNDCLASS wc;
- wc.style = 0;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = sizeof(WNDEB);
- wc.lpfnWndProc = DDEClientWndProc;
- wc.hInstance = hInstance;
- wc.hIcon = NULL;
- wc.hCursor = NULL;
- wc.hbrBackground = NULL;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = _szClassName;
- return(RegisterClass(&wc));
- }
-
-
- // ********** Functions for adding files to the Program Manager **************
-
- BOOL NEAR PASCAL CreatePMInfo (HANDLE hInstance) {
- int nDirIndex, nPMProg, nMaxPMProgs;
- DWORD dwTemp;
- BOOL fOk;
- char szPMGroup[100], szPMGroupFileName[100];
- char szPMProgName[100], szPMProgDesc[100], szPMIconFileName[100];
- char szBuf[100], szBuf2[100], szCmd[100], szProgMan[] = "PROGMAN";
- HWND hWndDDEClient, hWndPM;
- ATOM aApp, aTopic;
- GLOBALHANDLE hMem; LPSTR lpCommand;
-
- // Initiate a conversation with the Program Manager.
- aApp = GlobalAddAtom(szProgMan);
- aTopic = GlobalAddAtom(szProgMan);
- hWndDDEClient = CreateWindow("DDEClient", "", 0, 0, 0, 0, 0,
- NULL, NULL, hInstance, (LPSTR) MAKELONG(aApp, aTopic));
- GlobalDeleteAtom(aApp);
- GlobalDeleteAtom(aTopic);
-
- if (hWndDDEClient == NULL) {
- // Conversation could not be initiated.
- return(FALSE);
- }
-
- // Force the Program Manager to open so that the user can
- // see what group and applications we are adding.
-
- // Notice that I use the FindWindow function here. I can not use the
- // window handle of the DDE Server window because the Program Manager
- // could acknowledge our DDE conversation by creating a "DDEServer"
- // window. Calling the ShowWindow function and using this handle would
- // make the "DDEServer" window visible, this is definitely NOT desirable.
- ShowWindow(hWndPM = FindWindow(szProgMan, NULL), SW_RESTORE);
-
- // Disable the Program Manager so that the user can't work with it
- // while we are doing our stuff.
- EnableWindow(FindWindow(szProgMan, NULL), FALSE);
-
- // Create the PM Group box.
- SetupInfoSys(SIM_GETPMGROUP, 0, szPMGroup);
- SetupInfoSys(SIM_GETPMGROUPFILENAME, 0, szPMGroupFileName);
- wsprintf(szCmd, "[CreateGroup(%s%s%s)]",
- (LPSTR) szPMGroup, (LPSTR) (*szPMGroupFileName == 0 ? "" : ","),
- (LPSTR) szPMGroupFileName);
-
- hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, lstrlen(szCmd) + 1);
- lpCommand = GlobalLock(hMem);
- lstrcpy(lpCommand, szCmd);
- GlobalUnlock(hMem);
-
- fOk = (BOOL) SendMessage(hWndDDEClient, WM_DDE_EXECUTE, 0, MAKELONG(0, hMem));
- GlobalFree(hMem);
-
- // Add the individual PM files to the Group box.
- nMaxPMProgs = (int) SetupInfoSys(SIM_GETNUMPMPROGS, 0, 0);
- for (nPMProg = 0; fOk && (nPMProg < nMaxPMProgs); nPMProg++) {
- SetupInfoSys(SIM_GETPMPROGDESC, nPMProg, szPMProgDesc);
- nDirIndex = (int) SetupInfoSys(SIM_GETPMPROGNAME, nPMProg, szPMProgName);
-
- // Calculate the top of the destination directory path.
- wsprintf(szBuf, "%s%s", (LPSTR) _szDstDir, (LPSTR)
- ((*(_fstrrchr(_szDstDir, '\\') + 1) == 0) ? "" : "\\"));
- lstrcpy(szBuf2, szBuf);
-
- // Append the sub-directory where the file is and the file's name.
- SetupInfoSys(SIM_GETDIR, nDirIndex, _fstrchr(szBuf, 0));
- lstrcat(szBuf, "\\");
- lstrcat(szBuf, szPMProgName);
-
- // Append the subdir where the icon file is and the icon file's name.
- dwTemp = SetupInfoSys(SIM_GETPMICONINFO, nPMProg, szPMIconFileName);
- SetupInfoSys(SIM_GETDIR, LOWORD(dwTemp), _fstrchr(szBuf2, 0));
- lstrcat(szBuf2, "\\");
- lstrcat(szBuf2, szPMIconFileName);
-
- // Add the new file to the already created PM Group.
- wsprintf(szCmd, "[AddItem(%s,%s,%s,%d)]", (LPSTR) szBuf,
- (LPSTR) szPMProgDesc, (LPSTR) szBuf2, HIWORD(dwTemp));
- hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, lstrlen(szCmd) + 1);
- lpCommand = GlobalLock(hMem);
- lstrcpy(lpCommand, szCmd);
- GlobalUnlock(hMem);
-
- fOk = (BOOL) SendMessage(hWndDDEClient, WM_DDE_EXECUTE, 0, MAKELONG(0, hMem));
- GlobalFree(hMem);
- }
- // Terminate the DDE conversation with the Program Manager.
- DestroyWindow(hWndDDEClient);
- EnableWindow(hWndPM, TRUE);
- return(fOk);
- }
-