Dll Import

01/24/2014 19:10 shadowman123#1
well i've been studying C# till Now but i couldnt understand this part ... Why would i need to use External Dll Files ? and How do i know that im using the Right Dll ? i've Seen many Example of ppls using User32.Dll Or Kernel.Dll .. what are these Dlls Provide ? and i've seen Alot Or Parameters being used when these Dlls are used in the Project ..so i need Help in This

Regards
shadowman123
01/24/2014 19:36 phize#2
[Only registered and activated users can see links. Click Here To Register...]
01/24/2014 19:39 funhacker#3
It's because being that C# runs on the CLR it doesn't have full access to certain areas. Even then sometimes it's just easier to use an existing method written within the operating system. The parameters are used to control how the method is loaded generally.

Here's an example (from a program I'm working atm):

Code:
#region Field Region
        //Declare variables
        long 
            _ticksPerSecond = 0,
            _previousElapsedTime = 0;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the Elapsed Time of the Precise Timer.
        /// </summary>
        public double ElapsedTime
        {
            get
            {
                long time = 0;
                double elapsedTime;

                QueryPerformanceCounter(ref time);
                elapsedTime = (double)(time - _previousElapsedTime) / (double)_ticksPerSecond;
                _previousElapsedTime = time;

                return elapsedTime;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Creates a new precise timer.
        /// </summary>
        public PreciseTimer()
        {
            QueryPerformanceFrequency(ref _ticksPerSecond);
            GetElapsedTime(); // Get rid of first rubbish result
        }
        #endregion

        #region Imported Methods
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
        #endregion
In this example it uses the two methods within the kernel32.dll for windows to get an extremely accurate timer. You could try to use "DateTime" but this could return incorrect results between updates and would result in slower processing. However using this method allows for a great handle for an infinite loop like that found in games or graphics based software.

To find these kinds you could read them all found on msdn, however normally you may just come across more methods through experience and searching for solutions.

Lastly another reason could be that someone wrote the majority of code you need in c++ instead of c#. However it would be faster for you to import the methods instead of creating them in c# again.

MSDN Sources:
[Only registered and activated users can see links. Click Here To Register...]
[Only registered and activated users can see links. Click Here To Register...]
01/24/2014 20:06 CptSky#4
Quote:
Originally Posted by shadowman123 View Post
well i've been studying C# till Now but i couldnt understand this part ... Why would i need to use External Dll Files ? and How do i know that im using the Right Dll ? i've Seen many Example of ppls using User32.Dll Or Kernel.Dll .. what are these Dlls Provide ? and i've seen Alot Or Parameters being used when these Dlls are used in the Project ..so i need Help in This

Regards
shadowman123
DLL files are libraries. Using them allow you to use code that somebody written (like System.IO is a DLL). You can use directly .NET DLLs as you do with standard namespaces, but for native DLLs (C/C++), you must convert the CLR data to native (with marshalling). It can be useful for using unported WinAPI functions (User32, Kernel, etc) or to use a C++ library instead of reimplementing everything in .NET.

On MSDN website, the DLL is specified for each WinAPI function. Else, you must know the exported symbols to define the import table in C#.

Note that you'll have an overhead when using native DLLs as you must marshal everything.
01/24/2014 20:13 shadowman123#5
Quote:
Originally Posted by phize View Post
[Only registered and activated users can see links. Click Here To Register...]
First of All thx for your Answer .i've Searched Alot of Internet and Found Alot of Infos about that but couldnt understand them including the Explaination in the link you mentioned Above

Quote:
Originally Posted by funhacker View Post
It's because being that C# runs on the CLR it doesn't have full access to certain areas. Even then sometimes it's just easier to use an existing method written within the operating system. The parameters are used to control how the method is loaded generally.

Here's an example (from a program I'm working atm):

Code:
#region Field Region
        //Declare variables
        long 
            _ticksPerSecond = 0,
            _previousElapsedTime = 0;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the Elapsed Time of the Precise Timer.
        /// </summary>
        public double ElapsedTime
        {
            get
            {
                long time = 0;
                double elapsedTime;

                QueryPerformanceCounter(ref time);
                elapsedTime = (double)(time - _previousElapsedTime) / (double)_ticksPerSecond;
                _previousElapsedTime = time;

                return elapsedTime;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Creates a new precise timer.
        /// </summary>
        public PreciseTimer()
        {
            QueryPerformanceFrequency(ref _ticksPerSecond);
            GetElapsedTime(); // Get rid of first rubbish result
        }
        #endregion

        #region Imported Methods
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
        #endregion
In this example it uses the two methods within the kernel32.dll for windows to get an extremely accurate timer. You could try to use "DateTime" but this could return incorrect results between updates and would result in slower processing. However using this method allows for a great handle for an infinite loop like that found in games or graphics based software.

To find these kinds you could read them all found on msdn, however normally you may just come across more methods through experience and searching for solutions.

Lastly another reason could be that someone wrote the majority of code you need in c++ instead of c#. However it would be faster for you to import the methods instead of creating them in c# again.

MSDN Sources:
[Only registered and activated users can see links. Click Here To Register...]
[Only registered and activated users can see links. Click Here To Register...]
i understood What you Posted .. but i got Question Im a Beginner in Dll Import and all infos i Know are the Infos you mentioned Above so in order to use Specific Dll file i need to know each one of the Dlls Functions so do u got any Sites that include These Dlls Functions Explaination ??? and one more Questions i've Noticed that most of the Function inside these Dlls are written in C++ .. Y is that ? is C++ Better than C# so they used it instead of C# Or what ? Thx For your Answer

Quote:
Originally Posted by CptSky View Post
DLL files are libraries. Using them allow you to use code that somebody written (like System.IO is a DLL). You can use directly .NET DLLs as you do with standard namespaces, but for native DLLs (C/C++), you must convert the CLR data to native (with marshalling). It can be useful for using unported WinAPI functions (User32, Kernel, etc) or to use a C++ library instead of reimplementing everything in .NET.

On MSDN website, the DLL is specified for each WinAPI function. Else, you must know the exported symbols to define the import table in C#.

Note that you'll have an overhead when using native DLLs as you must marshal everything.
Excuse me but whats meant by Marchaling ?
01/24/2014 20:22 funhacker#6
Quote:
Originally Posted by shadowman123 View Post
First of All thx for your Answer .i've Searched Alot of Internet and Found Alot of Infos about that but couldnt understand them including the Explaination in the link you mentioned Above



i understood What you Posted .. but i got Question Im a Beginner in Dll Import and all infos i Know are the Infos you mentioned Above so in order to use Specific Dll file i need to know each one of the Dlls Functions so do u got any Sites that include These Dlls Functions Explaination ??? and one more Questions i've Noticed that most of the Function inside these Dlls are written in C++ .. Y is that ? is C++ Better than C# so they used it instead of C# Or what ? Thx For your Answer



Excuse me but whats meant by Marchaling ?
The best place for you to find all the functions would be to find them by the use you need them for [Only registered and activated users can see links. Click Here To Register...].

The reason for so much code being written in c++ is efficiency. C# is just like Java it runs on a virtual machine named "CLR". This takes the compiled code, and interprets it for the machine it is on.
C++ however is compiled to a specific CPU architecture and will only work on that specific type of CPU. However if a c++ program is written with the aim of compatibility it will be able to compile on many types of CPUs.
This is because the executable is compiled to machine code (ASM). C# is compiled to CLR instructions which are translated to ASM. Which makes it that bit slower than c++.
01/25/2014 02:37 shadowman123#7
Quote:
Originally Posted by funhacker View Post
The best place for you to find all the functions would be to find them by the use you need them for [Only registered and activated users can see links. Click Here To Register...].

The reason for so much code being written in c++ is efficiency. C# is just like Java it runs on a virtual machine named "CLR". This takes the compiled code, and interprets it for the machine it is on.
C++ however is compiled to a specific CPU architecture and will only work on that specific type of CPU. However if a c++ program is written with the aim of compatibility it will be able to compile on many types of CPUs.
This is because the executable is compiled to machine code (ASM). C# is compiled to CLR instructions which are translated to ASM. Which makes it that bit slower than c++.
Thx Guyz for your very useful and helpful Explaination ..it helped me Alot .. there were some stuffs that i didnt understand like CLR and Marshalling but after spending time on searching i understood alot ..Now i Completely understood everything related to this Part... All i need to do now is Implementing what i understood right now ..
01/25/2014 13:56 SteveRambo#8
Quote:
Originally Posted by funhacker View Post
In this example it uses the two methods within the kernel32.dll for windows to get an extremely accurate timer. You could try to use "DateTime" but this could return incorrect results between updates and would result in slower processing. However using this method allows for a great handle for an infinite loop like that found in games or graphics based software.
Kinda off topic, but you know there's a high precision timer in the System.Diagnostics namespace, right? [Only registered and activated users can see links. Click Here To Register...]
01/26/2014 02:50 funhacker#9
Quote:
Originally Posted by SteveRambo View Post
Kinda off topic, but you know there's a high precision timer in the System.Diagnostics namespace, right? [Only registered and activated users can see links. Click Here To Register...]
Yeah, that's great for the use of multiple timers. However for a single one I would assume that the class would use a little more resources than the one I posted and be unnecessary as it's built using similar code.
01/26/2014 12:19 KraHen#10
Add the overhead created by marshaling. Also, the current time is gotten through a simple OS interrupt, so as far as resources go this is a problem that isn`t worth optimizing.
01/26/2014 13:21 SteveRambo#11
Quote:
Originally Posted by funhacker View Post
Yeah, that's great for the use of multiple timers. However for a single one I would assume that the class would use a little more resources than the one I posted and be unnecessary as it's built using similar code.
[Only registered and activated users can see links. Click Here To Register...]
Quote:
Originally Posted by KraHen View Post
Add the overhead created by marshaling. Also, the current time is gotten through a simple OS interrupt, so as far as resources go this is a problem that isn`t worth optimizing.
Actually, the timestamp is read using just a single rdstc instruction.
01/28/2014 00:23 KraHen#12
Thanks for the clarification on that one, although my point stays valid. :)