home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / win / prg / cenviw / ddesrv.cmm < prev    next >
Text File  |  1994-09-22  |  3KB  |  105 lines

  1. // DDEsrv.cmm - Example of a DDE server
  2. // ver.1
  3.  
  4. #define APPLICATION_NAME   "CEnviClockApp"
  5. #define TOPIC_NAME         "CEnviClockTopic"
  6. #define HOUR_ITEM          "Hour"
  7. #define MINUTE_ITEM        "Minute"
  8. #define SECOND_ITEM        "Second"
  9. #define KEY_TIME_ITEM      "KeypressTime"
  10.  
  11. #include <DDEsrv.lib>
  12.  
  13. // If this application is already running then don't run
  14. if ( AmIRunning() )
  15.    FatalError("Error: This server is already running.\n");
  16.  
  17.  
  18. ddeSrv.Application = APPLICATION_NAME;
  19. ddeSrv.Topic = TOPIC_NAME;
  20. ddeSrv.Initiate = "InitializeServer";
  21. ddeSrv.Terminate = "TerminateServer";
  22. ddeSrv.Request = "RequestTime";
  23. ddeSrv.Advise = "AdviceWanted";
  24. ddeSrv.Unadvise = "AdviceNotWanted";
  25. if ( !RegisterDdeServer(ddeSrv) ) {
  26.    printf("Could not register DdeServer");
  27. }
  28.  
  29. puts("DDEsrv.cmm - Example DDE server for CEnvi.  This server will supply")
  30. puts("             clients with information about hour, minute, or second")
  31. puts("             and will hot link to any clients that what to know at")
  32. puts("             exactly what time a key was pressed.  Start any number")
  33. puts("             of DDEcli.cmm sample clients for this server.  Press")
  34. puts("             any keystroke to inform client of keystroke time.")
  35. puts("")
  36. puts("Server is up and running.");
  37.  
  38. // stay here until this window is closed
  39. while( DoWindows(False) ) {
  40.    if ( kbhit() ) {
  41.       getch();
  42.       printf("Key pressed. Tell clients the time.\n");
  43.       TimeString = asctime(localtime(time()));
  44.       UpdateServerLinkData(ddeSrv,KEY_TIME_ITEM,TimeString);
  45.    }
  46. }
  47.  
  48.  
  49.  
  50. ///////// FUNCTIONS CALLED FOR SERVER ////////
  51.  
  52. InitializeServer(pDdeSession)
  53. {
  54.    printf("Connection: %04X, App=\"%s\", Topic=\"%s\".\n",
  55.           pDdeSession.hwndClient,pDdeSession.Application,pDdeSession.Topic);
  56. }
  57.  
  58. TerminateServer(pDdeSession)
  59. {
  60.    printf("Terminate:  %04X, App=\"%s\", Topic=\"%s\".\n",
  61.           pDdeSession.hwndClient,pDdeSession.Application,pDdeSession.Topic);
  62. }
  63.  
  64. RequestTime(pItem,pDdeSession,pDdeServer)
  65. {
  66.    printf("Client requests: \"%s\".\n",pItem);
  67.    lTime = localtime(time());
  68.    if ( !stricmp(pItem,HOUR_ITEM) ) lData = lTime.tm_hour;
  69.    else if ( !stricmp(pItem,MINUTE_ITEM) ) lData = lTime.tm_min;
  70.    else if ( !stricmp(pItem,SECOND_ITEM) ) lData = lTime.tm_sec;
  71.    else return(NULL);
  72.    sprintf(lBuf,"%d",lData);
  73.    return lBuf;
  74. }
  75.  
  76. AdviceWanted(pItem,pHotLink,pDdeSession,pDdeServer)
  77. {
  78.    printf("Client wants link about \"%s\"\n",pItem);
  79.    // only advice we offer is about KEY_TIME_ITEM
  80.    return( !stricmp(pItem,KEY_TIME_ITEM) );
  81. }
  82.  
  83. AdviceNotWanted(pItem,pHotLink,pDdeSession,pDdeServer)
  84. {
  85.    printf("Client wants no more link about \"%s\"\n",pItem);
  86. }
  87.  
  88. //////// UTILITY FUNCTIONS ////////////
  89.  
  90. AmIRunning()
  91. {
  92.    return( 0 != ServerDirectory(APPLICATION_NAME,TOPIC_NAME,SrvList) );
  93. }
  94.  
  95. FatalError(pFmtString)
  96. {
  97.    va_start(valist,pFmtString);
  98.    vprintf(pFmtString,valist);
  99.    va_end(valist);
  100.    printf("\n\aPress any key to exit...");
  101.    getch();
  102.    exit(EXIT_FAILURE);
  103. }
  104.  
  105.