News:

Download Pelles C here: http://www.pellesc.se

Main Menu

Recent posts

#81
Work in progress / Re: Task Schedule 2.0 examples
Last post by TimoVJL - December 11, 2025, 10:11:30 AM
Quote from: MrBcx on December 10, 2025, 04:21:11 PMI have attached my four Bcx Basic Task Scheduler source codes, so that people
can examine the codes without needing to sign up on the Bcx forum. 

The BCX Translator is needed, if you want to examine the resulting  C/C++ codes.

I successfully tested each using Pelles C, MSVC, Mingw64, and Clang.


BCX seems to be powerful  :)
#82
Work in progress / Re: Task Schedule 2.0 examples
Last post by TimoVJL - December 11, 2025, 04:47:55 AM
https://learn.microsoft.com/en-us/windows/win32/taskschd/displaying-task-names-and-state--c---

usage example:
ComCpp2C2.exe EnumTasks2.cpp > EnumTasks2.cit just convert from cpp
pTaskCollection->Release();to C
pTaskCollection->lpVtbl->Release(pTaskCollection);
/********************************************************************
 This sample enumerates through all running tasks on the local computer and
 displays their name and state.
********************************************************************/
#define WIN32_LEAN_AND_MEAN
#define _WIN32_DCOM

#include <windows.h>
#include <stdio.h>
//#include <comdef.h>
//  Include the task header file.
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
//#pragma comment(lib, "comsupp.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

int __cdecl wmain()
{
    //  ------------------------------------------------------
    //  Initialize COM.
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if( FAILED(hr) )
    {
        printf("\nCoInitializeEx failed: %x", hr );
        return 1;
    }

    //  Set general COM security levels.
    hr = CoInitializeSecurity(
        NULL,
        -1,
        NULL,
        NULL,
        RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL,
        0,
        NULL);

    if( FAILED(hr) )
    {
        printf("\nCoInitializeSecurity failed: %x", hr );
        CoUninitialize();
        return 1;
    }

    //  ------------------------------------------------------
    //  Create an instance of the Task Service.
    ITaskService *pService = NULL;
    hr = CoCreateInstance( &CLSID_TaskScheduler,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           &IID_ITaskService,
                           (void**)&pService ); 
    if (FAILED(hr))
    {
          printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
          CoUninitialize();
          return 1;
    }
       
    //  Connect to the task service.
    VARIANT v1;
    VariantInit(&v1);
    hr = pService->lpVtbl->Connect(pService, v1, v1, v1, v1);
    if( FAILED(hr) )
    {
        printf("ITaskService::Connect failed: %x", hr );
        pService->lpVtbl->Release(pService);
        CoUninitialize();
        return 1;
    }

       // Get the running tasks.
       IRunningTaskCollection* pRunningTasks = NULL;
       hr = pService->lpVtbl->GetRunningTasks(pService,TASK_ENUM_HIDDEN, &pRunningTasks);

    pService->lpVtbl->Release(pService);
    if( FAILED(hr) )
    {
        printf("Cannot get Root Folder pointer: %x", hr );
        CoUninitialize();
        return 1;
    }
       
    LONG numTasks = 0;
    hr = pRunningTasks->lpVtbl->get_Count(pRunningTasks,&numTasks);

    if( numTasks == 0 )
     {
        printf("\nNo Tasks are currently running" );
        pRunningTasks->lpVtbl->Release(pRunningTasks);
        CoUninitialize();
        return 1;
     }

    printf("\nNumber of running tasks : %d", numTasks );

    TASK_STATE taskState;
    v1.vt = VT_I4;
    for(LONG i=1; i <= numTasks; i++)
    {
        v1.lVal = i;
        IRunningTask* pRunningTask = NULL;
        hr = pRunningTasks->lpVtbl->get_Item(pRunningTasks, v1, &pRunningTask );
       
        if( SUCCEEDED(hr) )
        {
            BSTR taskName = NULL;
            hr = pRunningTask->lpVtbl->get_Name(pRunningTask,&taskName);
            if( SUCCEEDED(hr) )
            {
                printf("\nTask Name: %ls", taskName);
                SysFreeString(taskName);

                hr = pRunningTask->lpVtbl->get_State(pRunningTask,&taskState);
                if (SUCCEEDED (hr) )
                    printf("\n\tState: %d", taskState);
                else
                    printf("\n\tCannot get the registered task state: %x", hr);
            }
            else
            {
                printf("\nCannot get the registered task name: %x", hr);
            }
            pRunningTask->lpVtbl->Release(pRunningTask);
        }
        else
        {
            printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
        }
    }

    pRunningTasks->lpVtbl->Release(pRunningTasks);
    CoUninitialize();
    return 0;
}
#83
General discussion / Re: Pellesc site down?
Last post by John Z - December 11, 2025, 12:04:05 AM
registration expired and has not (yet?) been renewed.

When the site is not available the request is rerouted to that page by whatever server was being used at Network Solutions.

John Z
#84
Work in progress / Re: Task Schedule 2.0 examples
Last post by Vortex - December 10, 2025, 08:37:42 PM
Hi Timo,

Thanks for your ComCpp2C2 tool. Kindly, could you please provide an example explaining how to use the tool?
#85
Work in progress / Re: Task Schedule 2.0 examples
Last post by MrBcx - December 10, 2025, 04:21:11 PM
I have attached my four Bcx Basic Task Scheduler source codes, so that people
can examine the codes without needing to sign up on the Bcx forum. 

The BCX Translator is needed, if you want to examine the resulting  C/C++ codes.

I successfully tested each using Pelles C, MSVC, Mingw64, and Clang.

#86
Work in progress / Re: Task Schedule 2.0 examples
Last post by TimoVJL - December 10, 2025, 04:14:32 AM
My idea was to learn convert MS C++ code to Pelles C code.

Nice to have also MrBcx in this topic too.

I didn't use ComCpp2C2 for conversion.

One example:
https://learn.microsoft.com/en-us/windows/win32/api/taskschd/nf-taskschd-itaskservice-connect
    //  Connect to the task service.
    //hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
    hr = pService->lpVtbl->Connect(pService, v1, v2, v3, v4);
those variants are optional, but C compiler don't allow NULL for them.
Just an one empty VARIANT could be used too ?

If you don't have msvcrt.libs, just remove USE_MSVCRT define from projects
#87
General discussion / Re: Pellesc site down?
Last post by alderman2 - December 09, 2025, 10:27:37 PM
Does anyone know why Pelle's website is not up?
Why is there an image with links on the page that lead to strange things?
#88
Work in progress / Re: Task Schedule 2.0 examples
Last post by John Z - December 09, 2025, 09:39:39 PM
Agree for Unix, I used cron for many things.  However the windows Task Scheduler interactive GUI is very easy to use and access.  Programmatically it is a bit harder.

Here is a powershell script creator in C to schedule a task...
typedef struct names{
  char AppPath[MAX_PATH];
  char VBSTime[50];
  char VBETime[50];
  char TaskName[50];
  char UserSTime[50];
  char Message[50];
}names;

names name;

/****************************************************************************
 *                                                                          *
 * Function: Create_Script_PS1                                              *
 *                                                                          *
 * Purpose : create runnable Powershell script for task                     *
 *                                                                          *
 * History : Date      Reason                                               *
 *           01/31/25  Created   John Z                                     *
 *                                                                          *
 * Globals :SYSTEMTIME dpst, SYSTEMTIME dpet;                               *
 ****************************************************************************/
void Create_Script_PS1(void)
{   int RetVal; 
char FileName [MAX_PATH*2]={0};
FILE *p_file;
    char buf2[4000]={0};
    char *p_buf2;

p_buf2 = buf2;  //wide char


    strcpy(FileName,name.AppPath);
strcat(FileName,"scr1.ps1");

p_file = fopen(FileName,"wb+"); //For Binary Access Write
    if (p_file == NULL)
      {
snprintf(buf2,MAX_PATH*2,"Unable to open file.\r\nFilename: %s",FileName);
MessageBoxA( NULL, buf2, "File Access Error",
MB_OK | MB_ICONERROR | MB_TOPMOST);
return;
  }

    snprintf(p_buf2, 3999,"%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A",
             "$TriggerTypeTime = 1",
             "$ActionTypeExec = 0",
             "$service = New-Object -ComObject Schedule.Service",
             "$service.Connect()"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);


    snprintf(p_buf2, 3999,"%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A",
             "$rootFolder = $service.GetFolder(\"\\\")",
             "$taskDefinition = $service.NewTask(0)",
             "$taskDefinition.RegistrationInfo.Description = \"Start Alert at a certain time\""
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);
          fflush(p_file);

    snprintf(p_buf2, 3999,"%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A",
             "$taskDefinition.RegistrationInfo.Author = \"CalendarZ\"",
             "$taskDefinition.Principal.LogonType = 3",
             "$taskDefinition.Settings.Enabled = $true",
             "$taskDefinition.Settings.StartWhenAvailable = $true"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);


    snprintf(p_buf2, 3999,"%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A",
             "$taskDefinition.Settings.Hidden = $false",
             "$taskDefinition.Settings.DeleteExpiredTaskAfter = \"PT10M\"",
             "$triggers = $taskDefinition.Triggers",
             "$trigger = $triggers.Create($TriggerTypeTime)"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);
          fflush(p_file);

   snprintf(p_buf2, 3999,"%s'%s'\x0D\x0A%s'%s'\x0D\x0A%s\x0D\x0A%s\x0D\x0A",
             "$startTime = ",name.VBSTime,
             "$endTime = ",name.VBETime,
             "$trigger.StartBoundary = $startTime",
             "$trigger.EndBoundary = $endTime"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);
          fflush(p_file);
   snprintf(p_buf2, 3999,"%s\x0D\x0A%s\x0D\x0A%s\"%s%s\"\x0D\x0A",
             "$actions = $taskDefinition.Actions",
             "$action = $actions.Create($ActionTypeExec)",
             "$action.Path = ",
              name.AppPath,
         "Alert.exe"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);

   snprintf(p_buf2, 3999,"%s%s -%s\"\x0D\x0A",
             "$Action.Arguments = \"-",
          name.UserSTime,
          name.Message
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);

    snprintf(p_buf2, 3999,"%s\x0D\x0A%s,\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A%s\x0D\x0A)",
             "$rootFolder.RegisterTaskDefinition(",
         name.TaskName,
             "    $taskDefinition,",
             "    6, # Task creation flag (6 = CREATE_OR_UPDATE)",
             "    $null, # User",
         "    $null, # Password",
             "    $null, # Logon type",
             "    $null # SDDL"
             );
    RetVal = fwrite(p_buf2, sizeof(char), strlen(p_buf2), p_file);
          fflush(p_file);

    fclose(p_file);
//MessageBox(NULL,"Completed!","Create Alert",MB_OK);


}/* end create_script_ps1 */

Not too bad, I have one for VBS too if interested...
I would have preferred in C but couldn't quite manage, so I'll learn from Timo's effort.

John Z
#89
Work in progress / Re: Task Schedule 2.0 examples
Last post by Vortex - December 09, 2025, 09:18:57 PM
The cron jobs of UNIX\Linux are much more simple to maintain.
#90
Work in progress / Re: Task Schedule 2.0 examples
Last post by MrBcx - December 09, 2025, 04:53:36 PM
Quote from: Vortex on December 09, 2025, 11:26:23 AMAdditionaly, MrBcx created some useful applications too :

https://bcxbasiccoders.com/smf/index.php?topic=1443.0

To get at my code, one mostly needs to be a registered user and logged in.

Here is a list of my most recent uploads:

* Communicate with an AI Server using COM
* COM Demos For Using Windows Task Scheduler Services
* Send a JSON record via WinHttp
* Fetch a JSON record via WinHttp
* REST API toolkit in BCX
 
Only the BCX BASIC source code is provided for each. 
If you want the Pelles C compatible code, you'll need to use the BCX Translator.