[Info-vax] VMS Cobol - GnuCOBOL

Arne Vajhøj arne at vajhoej.dk
Sat Feb 25 19:28:06 EST 2023


On 2/25/2023 4:32 PM, John Dallman wrote:
> In article <ttd4ta$2j12a$2 at dont-email.me>, arne at vajhoej.dk (Arne Vajhøj)
> wrote:
>> C# and Java are very similar. I don't think MS ever made a secret
>> of that.
> 
> Microsoft started on .NET during their lawsuit with Sun over their
> incomplete-but-extended Java implementation. They seem to have hoped to
> replace Java with .NET.

Yes.

>> There are of course also some differences. C# got
>> unsafe blocks with pointers (nobody use them) ...
> 
> The main thing those are useful for is calling native code. It's way
> easier than using JNI.

I was not thinking about pointer type aka IntPtr, which are commonly
used for p/Invoke (DllImport).

Example:

using System;
using System.Runtime.InteropServices;

namespace PtrTyp
{
     public class Program
     {
         [DllImport("msvcrt.dll")]
         public static extern IntPtr calloc(int numelm, int elmlen);
         [DllImport("msvcrt.dll")]
         public static extern void memset(IntPtr p, int val, int len);
         [DllImport("msvcrt.dll")]
         public static extern int printf(string fmt, IntPtr p);
         [DllImport("msvcrt.dll")]
         public static extern void free(IntPtr p);

         public static void Main(string[] args)
         {
             IntPtr p = calloc(1, 4);
             memset(p, 65, 3);
             printf("%s\n", p);
             free(p);
         }
     }
}

I was thinking about real star pointer.

Example:

using System;

namespace StarPtr
{
     public class Program
     {
         public static void Main(string[] args)
         {
             int[] a = new int[4] { 1, 2, 3, 4 };
             foreach(int ai in a)
             {
                 Console.Write(" {0}", ai);
             }
             Console.WriteLine();
             unsafe
             {
                 fixed(int *p = &a[0])
                 {
                     for(int i = 0; i < 4; i++)
                     {
                         int *p2 = p + i;
                         Console.Write(" {0}", *p2);
                     }
                     Console.WriteLine();
                 }
             }
         }
     }
}

The star pointer do not need to be used with p/Invoke (DllImport) but
they can be used - I just think it is very rare.

Example:

using System;
using System.Runtime.InteropServices;

namespace Mixed
{
     public class Program
     {
         [DllImport("msvcrt.dll")]
         public static extern void memset(IntPtr p, int val, int len);
         public static void Main(string[] args)
         {
             int a = 0;
             unsafe
             {
                 int *p = &a;
                 memset((IntPtr)p, 123, 1);
             }
             Console.WriteLine(a);
         }
     }
}

Arne

PS: Yes - p/Invoke (DllImport) is way easier than JNI.





More information about the Info-vax mailing list