Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to get windows directory using unsafe code with pointer and unmanaged function from DLL in C#

1 Answer

0 votes
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        [DllImport("kernel32", SetLastError = true)]
        static extern unsafe uint GetWindowsDirectory(byte* lpBuffer, uint uSize);

        // Error CS0227  Unsafe code may only appear if compiling with /unsafe 
        /*
            Open the project Properties page
            Click the Build property page
            Select the Allow Unsafe Code check box
        */
        static unsafe void Main(string[] args)
        {
            byte[] array = new byte[512];

            unsafe
            {
                fixed (byte *p = array)GetWindowsDirectory(p, 512);
            }
            ASCIIEncoding ae = new ASCIIEncoding();

            Console.WriteLine(ae.GetString(array));
        }
    }
}


/*
run:

C:\Windows

*/

 



answered Apr 28, 2017 by avibootz

Related questions

1 answer 3,995 views
1 answer 201 views
1 answer 113 views
2 answers 243 views
243 views asked Apr 28, 2017 by avibootz
...