Presentation is loading. Please wait.

Presentation is loading. Please wait.

OE-NIK HP Advanced Programming Using and creating DLL files.

Similar presentations


Presentation on theme: "OE-NIK HP Advanced Programming Using and creating DLL files."— Presentation transcript:

1 OE-NIK HP Advanced Programming Using and creating DLL files

2 V1.0 Dynamic Link Library / Shared Object Program module that can store program code and resources that can be accessed from other programs Separately compiled, it is only loaded/attached to the program during run-time ( = dynamic) Only loaded to the memory once, several programs can use the same memory instance (= shared, partly true in.NET) Today most of the OS and userspace programs are all use such files (although it is possible to use static linking, it is not good practice) In Windows OS, we must distinguish native and managed DLLs: –Native: Contains procedural bytecode that can be directly interpreted by the OS/CPU (a bunch of methods that use only simple types, even direct HW access) –Managed: Contains one (or more).NET CLR classes (CLR? see later) 2 OE-NIK HP

3 V1.0 Classic compile process Object File: An intermediate executable format, generated by the compiler from the source code Contains: the compiled code and the relocation data that is used by the linker to generate the executable External libraries are merged into the EXE/ELF file in case of static linking, and left as separate files in case of dynamic linking 3 OE-NIK HP Source (.cpp,.pas,.c,.cs,.*) PREPROCESS, COMPILE OBJECT FILE (.o,.obj) EXTERNAL LIBRARIES (.o,.obj,.so,.dll) LINKING EXECUTABLE FILE (.exe)

4 V1.0 Static linking Every method/resource is in the same module/program The location of these is already known by the compiler Every method call is pre-defined, because every method is inside the memory region of the same process The same method can be loaded by multiple programs  waste of memory, waste of resources Classic (non-overlay) DOS programs (e.g.: Turbo Pascal) 4 OE-NIK HP SomeFunct1 SomeFunct2 SomeFunct3 Constant2 Constant1 Constant3 Main Code. @SomeFunct3. @SomeFunct1. Program

5 V1.0 Dynamic linking Some methods/resources are outside the memory allocated to the process The exact location of those are not known to the compiler The reference to those methods are dynamic, determined during run-time (not known during compile-time, the DLL will be loaded by the OS on demand) The same method can be used by multiple processes  shared (~ Shared Object) Most of the modern programs work this way 5 OE-NIK HP Program 2. @SomeFunct3. Program 1. @SomeFunct2. DLL SomeFunct2 SomeFunct3

6 V1.0.NET JIT JIT = Just-In-Time Per method, the speed loss is small due to OOP NGEN.EXE can be used, not necessarily makes faster Similar: Java Hotspot VM 6 OE-NIK HP

7 V1.0 Native vs. managed DLL Native DLL –Loaded from the current directory or from the %PATH% –Slow load time –%PATH% = WINDOWS, SYSTEM, SYSTEM32 directories  DLL HELL Managed DLL ( .NET DLL,.NET DLL assembly) –EVERY method/class that we used so far was a DLL call –A project’s „References” section stores the managed DLL files accessible by the project, this is editable –In the documentation you can find which class/namespace is stored in which DLL file –The managed DLLs are registered into a common storage (GAC), solving the problem of versioning and dependencies –VERY fast load time, almost as if no DLL were used 7 OE-NIK HP

8 V1.0 Accessing a DLL in the.NET framework Managed DLL –Add reference to the DLL file: Project/Add reference –After this, the namespaces/classes/methods of the DLL can be accessed the usual way Platform Invoke (P/Invoke: call native methods from managed environment)  DllImport attribute – using System.Runtime.InteropServices; — [DllImport("winmm.dll", SetLastError = true)] static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound); — string fname = @"c:\Windows\Media\tada.wav"; PlaySound(fname, UIntPtr.Zero, 1); –WINAPI signatures, imports: www.pinvoke.net 8 OE-NIK HP

9 V1.0 Creating a managed DLL 1 solution, 2 projects: DLL creator project + DLL user project The DLL creator project: Class Library We have to add a reference to the DLL file (or to the project itself) into the DLL using project The DLL file will be copied next to the executable file After adding the reference, the DLL namespaces and classes can be accessed the usual way 9 OE-NIK HP

10 V1.0 Exercise Create an applictaion, where the main program uses DLL modules to handle the different players User  controlled by the user RandomEnemy  random movements FollowerEnemy  follows the user Solve the problem using DLLs added as a reference, and also with dynamically loaded DLL files Common classes can be downloaded from the website 10 OE-NIK HP

11

12


Download ppt "OE-NIK HP Advanced Programming Using and creating DLL files."

Similar presentations


Ads by Google