Convert C++ to C# or VB.net

Discussion in 'C#' started by FPForum, Mar 22, 2023.

  1. #1
    I'm wanting to convert the two pieces of code below into either C# or VB.net, but can't find a converter online. Is anyone able to convert these or point me to an online converter?

    
    #define TARGET_RESOLUTION 1  // 1-millisecond target resolution
    
    TIMECAPS tc;
    UINT  wTimerRes;
    
    if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR)
    {
      // Error; application can't continue.
    }
    
    wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
    timeBeginPeriod(wTimerRes);
    
    //  do your stuff here at approx. 1 ms timer resolution
    
    timeEndPeriod(wTimerRes);
    
    Code (markup):
    and

    
    #define STATUS_SUCCESS 0
    #define STATUS_TIMER_RESOLUTION_NOT_SET 0xC0000245
    
    // after loading NtSetTimerResolution from ntdll.dll:
    
    // The requested resolution in 100 ns units:
    ULONG DesiredResolution = 5000;  
    // Note: The supported resolutions can be obtained by a call to NtQueryTimerResolution()
    
    ULONG CurrentResolution = 0;
    
    // 1. Requesting a higher resolution
    // Note: This call is similar to timeBeginPeriod.
    // However, it to to specify the resolution in 100 ns units.
    if (NtSetTimerResolution(DesiredResolution ,TRUE,&CurrentResolution) != STATUS_SUCCESS) {
      // The call has failed
    }
    
    printf("CurrentResolution [100 ns units]: %d\n",CurrentResolution);
    // this will show 5000 on more modern platforms (0.5ms!)
    
    //  do your stuff here at 0.5 ms timer resolution
    
    // 2. Releasing the requested resolution
    // Note: This call is similar to timeEndPeriod
    switch (NtSetTimerResolution(DesiredResolution ,FALSE,&CurrentResolution) {
      case STATUS_SUCCESS:
      printf("The current resolution has returned to %d [100 ns units]\n",CurrentResolution);
      break;
      case STATUS_TIMER_RESOLUTION_NOT_SET:
      printf("The requested resolution was not set\n");   
      // the resolution can only return to a previous value by means of FALSE
      // when the current resolution was set by this application   
      break;
      default:
      // The call has failed
    
    }
    
    Code (markup):
    Thanks in advance!
     
    Solved! View solution.
    FPForum, Mar 22, 2023 IP
  2. #2
    
    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        const int TARGET_RESOLUTION = 1; // 1-millisecond target resolution
    
        [StructLayout(LayoutKind.Sequential)]
        struct TIMECAPS
        {
            public uint wPeriodMin;
            public uint wPeriodMax;
        }
    
        [DllImport("winmm.dll")]
        static extern uint timeGetDevCaps(ref TIMECAPS ptc, uint cbtc);
    
        [DllImport("winmm.dll")]
        static extern uint timeBeginPeriod(uint uPeriod);
    
        [DllImport("winmm.dll")]
        static extern uint timeEndPeriod(uint uPeriod);
    
        static void Main(string[] args)
        {
            TIMECAPS tc = new TIMECAPS();
            uint wTimerRes;
    
            if (timeGetDevCaps(ref tc, (uint)Marshal.SizeOf(tc)) != 0)
            {
                // Error; application can't continue.
                return;
            }
    
            wTimerRes = Math.Min(Math.Max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
            timeBeginPeriod(wTimerRes);
    
            //  do your stuff here at approximately 1 ms timer resolution
    
            timeEndPeriod(wTimerRes);
        }
    }
    
    Code (markup):
     
    hibiscustechno, Aug 26, 2023 IP
    FPForum likes this.
  3. hibiscustechno

    hibiscustechno Well-Known Member

    Messages:
    163
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    175
    #3
    Second one ->

    
    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        const int STATUS_SUCCESS = 0;
        const int STATUS_TIMER_RESOLUTION_NOT_SET = unchecked((int)0xC0000245);
    
        [DllImport("ntdll.dll")]
        static extern int NtSetTimerResolution(uint DesiredResolution, bool SetResolution, ref uint CurrentResolution);
    
        static void Main(string[] args)
        {
            uint DesiredResolution = 5000;
            uint CurrentResolution = 0;
    
            // 1. Requesting a higher resolution
            if (NtSetTimerResolution(DesiredResolution, true, ref CurrentResolution) != STATUS_SUCCESS)
            {
                // The call has failed
            }
    
            Console.WriteLine("CurrentResolution [100 ns units]: " + CurrentResolution);
            // this will show 5000 on more modern platforms (0.5ms!)
    
            // do your stuff here at 0.5 ms timer resolution
    
            // 2. Releasing the requested resolution
            int setResult = NtSetTimerResolution(DesiredResolution, false, ref CurrentResolution);
            switch (setResult)
            {
                case STATUS_SUCCESS:
                    Console.WriteLine("The current resolution has returned to " + CurrentResolution + " [100 ns units]");
                    break;
                case STATUS_TIMER_RESOLUTION_NOT_SET:
                    Console.WriteLine("The requested resolution was not set");
                    // the resolution can only return to a previous value by means of FALSE
                    // when the current resolution was set by this application
                    break;
                default:
                    // The call has failed
                    break;
            }
        }
    }
    
    
    Code (markup):
     
    hibiscustechno, Aug 26, 2023 IP
    FPForum likes this.