| |

VerySource

 Forgot password?
 Register
Search
Author: zero9999

Can masters embed C language into C #

[Copy link]

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-22 14:30:01
| Show all posts
// fastcopy.cs
// compile with: / unsafe
using System;
 
class Test
{
    // The unsafe keyword allows pointers to be used within
    // the following method:
    static unsafe void Copy (byte [] src, int srcIndex,
        byte [] dst, int dstIndex, int count)
    {
        if (src == null || srcIndex <0 ||
            dst == null || dstIndex <0 || count <0)
        {
            throw new ArgumentException ();
        }
        int srcLen = src.Length;
        int dstLen = dst.Length;
        if (srcLen-srcIndex <count ||
            dstLen-dstIndex <count)
        {
            throw new ArgumentException ();
        }
 
 
            // The following fixed statement pins the location of
            // the src and dst objects in memory so that they will
            // not be moved by garbage collection.
            fixed (byte * pSrc = src, pDst = dst)
            {
                  byte * ps = pSrc;
                  byte * pd = pDst;

            // Loop over the count in blocks of 4 bytes, copying an
            // integer (4 bytes) at a time:
            for (int n = 0; n <count / 4; n ++)
            {
                * ((int *) pd) = * ((int *) ps);
                pd + = 4;
                ps + = 4;
            }
 
            // Complete the copy by moving any bytes that weren't
            // moved in blocks of 4:
            for (int n = 0; n <count% 4; n ++)
            {
                * pd = * ps;
                pd ++;
                ps ++;
            }
            }
    }
 
 
    static void Main (string [] args)
    {
        byte [] a = new byte [100];
        byte [] b = new byte [100];
        for (int i = 0; i <100; ++ i)
           a [i] = (byte) i;
        Copy (a, 0, b, 0, 100);
        Console.WriteLine ("The first 10 elements are:");
        for (int i = 0; i <10; ++ i)
           Console.Write (b [i] + "");
        Console.WriteLine ("\n");
    }
}

Take a look at this exp unsafe
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-23 17:15:01
| Show all posts
Use unsafe programming, as the following example
   public unsafe class Dog
    {

        public uint DogBytes, DogAddr;
        public byte [] DogData;
        public uint Retcode;
        
        
        [DllImport ("Win32dllRead.dll", CharSet = CharSet.Ansi)]
        public static unsafe extern uint DogRead (uint idogBytes, uint idogAddr, byte * pdogData);
        [DllImport ("Win32dll.dll", CharSet = CharSet.Ansi)]
        public static unsafe extern uint DogWrite (uint idogBytes, uint idogAddr, byte * pdogData);

        public unsafe Dog (ushort num)
        {
            DogBytes = num;
            DogData = new byte [DogBytes];
        }
public unsafe void ReadDog ()
        {
            fixed (byte * pDogData =&DogData [0])
            {
                Retcode = DogRead (DogBytes, DogAddr, pDogData);
            }
        }
}
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-24 09:45:01
| Show all posts
try ..

unsafe
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-29 18:30:01
| Show all posts
unsafe
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-2 21:00:02
| Show all posts
msdn unsafe ~
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-3 08:45:01
| Show all posts
It seems that all of the above use dllimport.
Hehe, I want to use header files. . .
Reply

Use magic Report

0

Threads

31

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 United States

Post time: 2020-3-6 15:30:02
| Show all posts
Sealed as DLL
Reply

Use magic Report

0

Threads

31

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-6 15:45:01
| Show all posts
PS: As long as the DLL is sealed, it does not need dllimport.
Register with regsvr32
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-6 21:45:01
| Show all posts
Can header files be sealed as dlls?
Reply

Use magic Report

1

Threads

31

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-6 23:45:01
| Show all posts
C # can embed non-safe code, which is unsafe programming.If there is unsafe programming, you need to add the unsafe option to the compiler.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list