Вы находитесь на странице: 1из 7

c 


 
  

What is DLL?
A dynamic-link library (DLL) is an executable file that acts as a shared library of functions. Dynamic linking provides a way for a
process to call a function that is not part of its executable code. The executable code for the function is located in a DLL, which
contains one or more functions that are compiled, linked, and stored separately from the processes that use them. DLLs also facilitate
the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of a DLL in memory.

Difference from the static linking:


Dynamic linking differs from static linking in that it allows an executable module (either a .dll or .exe file) to include only the
information needed at run time to locate the executable code for a DLL function. In static linking, the linker gets all of the referenced
functions from the static link library and places it with your code into your executable.

Static linking Dynamic Linking


Advantages of Using DLLs
Dynamic linking has the following advantages:
Saves memory and reduces swapping. Many processes can use a single DLL simultaneously, sharing a single copy of the DLL in
memory. In contrast, Windows must load a copy of the library code into memory for each application that is built with a static link
library.

Saves disk space. Many applications can share a single copy of the DLL on disk. In contrast, each application built with a static link
library has the library code linked into its executable image as a separate copy.

Upgrades to the DLL are easier. When the functions in a DLL change, the applications that use them do not need to be recompiled or
relinked as long as the function arguments and return values do not change. In contrast, statically linked object code requires that the
application be relinked when the functions change.

Provides after-market support. For example, a display driver DLL can be modified to support a display that was not available when the
application was shipped.

Supports multi language programs. Programs written in different programming languages can call the same DLL function as long as the
programs follow the function's calling convention. The programs and the DLL function must be compatible in the following ways: the
order in which the function expects its arguments to be pushed onto the stack, whether the function or the application is responsible for
cleaning up the stack, and whether any arguments are passed in registers.

How to write a dll:


#include <windows.h>
// DLL entry function (called on load, unload, ...)
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved){
return TRUE;
}

// Exported function - adds two numbers


extern "C" __declspec(dllexport) double AddNumbers(double a, double b){
return a + b;
}

include <windows.h>
#include <stdio.h>
// Import function that adds two numbers
extern "C" __declspec(dllimport) double AddNumbers(double a, double b);
int main(int argc, char *argv[])
{
double result = AddNumbers(1, 2);
printf("The result was: %f\n", result);
return 0;
}

c   

We want to store a character String (³XYZ´ )in the dynamic memory:
1.Define a handle to the memory block
HANDLE hMem;

2.Allocate the memory:


hMem = LocalAlloc( LMEM_MOVABLE | LMEM_ZEROINIT,5);
Allocates a moveable memory
It initializes with zero.
Allocates a block of 5 bytes

Check that the system successfully provides the memory or not.

if (hMem == NULL)
MessageBox(hwnd,´No more room in local memory´,´warning´, MB_OK);

hMem
ï  
3.Define a near pointer to a character String
PSTR pstr;

4.Set the pointer and lock the memory block


pstr= LocalLock(hMem);

5.Copy the character String in the memory


lstrcpy(pstr,´XYZ´);

6.Unlock the block [ Allow the block moveable again ]


LocalUnlock(hMem);

 

   

HANDLE hMem;
hMem = LocalAlloc( LMEM_MOVABLE | LMEM_ZEROINIT,5);
//Now increase the memory size
hMem = LocalReAlloc(hMem, 256, ( LMEM_MOVABLE );


   ! "#     


PSTR pstr1, pstr2;


pstr1= LocalLock(LMEM_FIXED | LMEM_ZEROINIT, 27);
pstr2 =pstr1;

for(int i=0; i<26; i++)


*pstr2++ = µA¶ +i;

 

      

When a window needs memory space, then


If there is a sufficient size memory available
Then the space is allocated
Else (if window find the memory by moving moveable memory)
Then Window allocates the memory
Else Window makes room by discarding objects.( swap out)
HANDLE hMem;
PSTR pstr;
hMem = LocalAlloc( LMEM_MOVABLE | LMEM_DISCARDABLE,512);
if(hMem){

PSTR pstr;
lstrcpy(pstr,´XYZ´);
LocalUnlock(hMem);

}
c $
%!  
Clipboard is just a memory block that windows maintain. Application can pass the data to the clipboard. Because the clipboard is
maintained by the windows environment, its contents are available to any running program.
The data types are exchanged through the clipboard are text(CF_TEXT) and bitmaps (CF_BITMAP, CF_TIFF, CF_PALETTE)

%!  # 

1.OPENCLIPBOARD:
Purpose: It opens the clipboard for reading and writing of its contents. The clipboard must be open before read its content or write the
content to it. The clipboard must be opened and closed during the processing of a single window message.
Syntax: BOOL OpenClipboard (HWND hwnd)
Parameters: hwnd --- The handle of the window that is opening the clipboard.
Header Needed: winuser.h
2.SETCLIPBOARDDATA:
Purpose: It places the data in the clipboard for a given clipboard format. This is the only way to set the contents of the clipboard. Before
places the data in the clipboard , the clipboard must be opened.
Syntax: HANDLE SetClipboardData (uINT wFormat, HANDLE hMem)
Parameters:
wFormat : Specifies the type of the data.
hMem: A handle to the global memory block that contains the data in specified format.
Header Needed: winuser.h
3.GETCLIPBOARDDATA:
Purpose: It retrieves the data from the clipboard .
Syntax: HANDLE GetClipboardData(UINT wFormat)
Parameters: wFormat: Specifies the clipboard format for the data.
Header Needed: winuser.h
4.EMPTYCLIPBOARD:
Purpose: It deletes the contents of clipboard and frees the associated data.
Syntax: BOOL EmptyClipboard(void)
Header Needed: winuser.h
5.CLOSECLIPBOARD:
Purpose: It closes the clipboard for reading and writing the data.
Syntax: BOOL CloseClipboard(void)

c &!
 %
What is bitmap?
Bitmaps are blocks of data that can be output directly to the device. They can be thought of as a way to store the pixel data directly
from the screen into a memory buffer. Painting bitmap onto the screen is much faster than using the GDI functions like Rectangle ()etc.
The drawbacks to the bitmaps are that they take a lot of memory and disk space and they can not scale well, especially if they contain
the text.

Steps are needed to display the bitmap:


Load the bitmap data into the memory using LoadBitmap().
Create the memory device context using CreateCompatibleDC()
Select the bitmap into the memory device context with the SelectObject()
Copy the bitmap from the memory device context to the output device context with the BitBlt().

LoadBitmap(): It loads the bitmap resource from the module µ s resource file.This function can load the device dependent bitmap or
device independent bitmap.
Syntax: HBITMAP loadBitmap( HINSTANCE hInstance, LPCSTR lpszBitmap)

Pointer to the null terminated String.


BitBlt() is the standard function for displaying the bitmap. This function copies the bitmap from the memory device to the device
context. The bitmap must be selected into the memory device context, created with CreateCompatibleDC function. The bitmap can be
combined with existing background using the raster operation code.
Syntax: BOOL BitBlt (HDC hdc, int xDevicecontext, int yDevicecontext, int width, int height, HDC hMemorydevice, int xSrc, int
ySrc, DWORD rasteroperation)

 !
Message Box: AY Y is a special kind of modal dialog box that an application uses to display messages and prompt for input.
A message box typically contains a text message and one or more predefined buttons. The following screen shot shows a message box.

The MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message
and title, plus any combination of predefined icons and push buttons.
int MessageBox(
HWNDY
, // handle to owner window
LPCTSTRY , // text in message box
LPCTSTRY  
, // message box title
UINTY  // message box style
);
eg:
MessageBox (hWnd, "bfileStatus is positive", "Warning", MB_OK);
Modeless Dialog box

 
! allows the user to switch between the dialog box and the parent window.
This type of the dialog box is preferred when the user would find it convenient to keep the displayed for a while.
(For instance the word processor often use modeless dialog box for the text finding)

To create the Modeless Dialog box we use the function CreateDialog( )


hDlgModeless = CreateDialog (hInstance,  YhwndParent, DialogProc);

STYLE of the modeless dialog box looks like as follows:


STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_VISIBLE

In the modeless dialog box, the message comes through the program¶s message queue. The message queue must be altered to pass these
messages to the dialog box window procedure.

while( GetMessage (&msg, NULL,0,0))


{
if( hDlgModeless = = 0 ||(! IsDialogMessage(hDlgModeless, &msg) ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
[First retrieve the message from the thread¶s message queue. First check whether the message is for the dialog box. If the message is for
dialog box, then sent the message to the dialog box message handler function. Otherwise the message is forwarded to the WndProc. ]

Use the DestroyWindow rather than EndDialog to end a modeless dialog box.

c '
   

File Creation:
CreateFile : The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object:
HANDLE CreateFile(
LPCTSTRY   , // file name
DWORDY  , // access mode
DWORD  , // share mode
LPSECURITY_ATTRIBUTESY , // Security information
DWORDY 
 
, // how to create
DWORDY  
, // file attributes
HANDLEY   // handle to template file
);

Access Mode:
GENERIC_READ Specify the read access. Data can be read from the file and the file pointer can be
moved.
GENERIC_WRITE Specify the write access. Data can be write to the file and the file pointer can be
moved.

Share Mode:
FILE_SHARE_READ Group and other user has the read permission.
FILE_SHARE_WRITE Group and other user has the write permission.

How to create:
CREATE_NEW Create a new file. The function fails if the file already exists.
CREATE_ALWAYS Create a new file. The function over writes if the file already exists.
OPEN_ALWAYS Opens the file if it exists. Other wise it creates the file.

File Attribute:
FILE_ATTRIBUTE_NORMAL The file is normal.
FILE_ATTRIBUTE_HIDDEN If the file is hidden file.
FILE_ATTRIBUTE_SYSTEM If the file is system file.
FILE_ATTRIBUTE_ARCHIVE If the file is back up file.

File creation:
HANDLE hFile;
HFile= CreateFile(³File1.txt´, // File name
GENERIC_READ | GENERIC_WRITE, // specify the access mode
FILE_SHARE_READ, // shared mode
NULL, // Security Information
OPEN_ALWAYS, // how to create
FILE_ATTRIBUTE_NORMAL, // File attribute
NULL);

How to open a File:


Function Name Syntax Purpose
_lopen int _lopen(char *FileName, int Flag ) It opens the file and returns the file
handler as an integer. If the file
could not be open then file handle
will be -1.

Flag Types are given below:


OF_READ The file is opened for read only purpose.
OF_WRITE The file is opened for write only purpose.
OF_READWRITE The file is opened for read and writes only purpose.
OF_EXIST Checks if the file exists.

How to write to the file:


Function used: _lwrite ( File Handler, pointer to the data, size of the data)

Example:
int hFileHandle;
char data [ ] = ³How to write to the file´;
hFileHandle = _lopen (³File1.txt´, OF_WRITE); // open the file

_lwrite(hFileHandle, data, sizeof(data); // write the information to the file


_lclose (hFileHandle);

K(  



Function used: _lread (int File_Handler, target buffer, size of the data)

Example:
int hFileHandle;
char data [ 128];
hFileHandle = _lopen (³File1.txt´, OF_READ);

_lread(hFileHandle, data, sizeof(data);


_lclose (hFileHandle);

K( ) # 



Function used: _llseek ( int File_handle, int offset, int origin)

Origin: If the file pointer is at the beginning of the file then it will be 0.
If the file pointer is at the end of the file then it will be 2.
For the current location, it will be 1.

Example:

) # 

int hFileHandle;

hFileHandle = _lopen (³File1.txt´, OF_WRITE);

// Go to end of file
_llseek(hFileHandle, 0L, 2);

// Go to beginning of file
_llseek(hFileHandle, 0L, 0);

* 




int hFileHandle;

hFileHandle = _lopen (³File1.txt´, OF_WRITE);


// Go to end of file and you get the file size
int filesize = (int)_llseek(hFileHandle, 0L, 2);

K(  


  


int hFileHandle;
char data [ 128];
hFileHandle = _lopen (³File1.txt´, OF_READ);
// Go to end of file
int filesize = (int)_llseek(hFileHandle, 0L, 2);
// Move to the beginning of the file
_llseek(hFileHandle, 0L, 0);

// Allocate the memory dynamically


char * pstr = LocalAlloc (LPTR, filesize);

//Read from the file


_lread(hFileHandle, ptr, filesize);
_lclose( hFileHandle);

// Print the information using hdc

%% 
  


int hFileHandle;
char data [ 128] = ³Hello Win 32 File operation´;
hFileHandle = _lopen (³File1.txt´, OF_WRITE);
// Go to end of file
int filesize = (int)_llseek(hFileHandle, 0L, 2);

//Append to the the file


_lwrite(hFileHandle, data, sizeof(data));
_lclose( hFileHandle);
+,*-.,c/.: To ensure that every message is processed, DefWindowProc () provides the default message processing for the
messages that an application does not process. The default processing function handles the basic operation of a window¶s application.
Synatx: LRESULT DefWindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Function needed for printing text information:

TextOut: The function writes the character string at the specified location, using the currently selected font.
Syntax: BOOL textOut ( HDC hdc, int x, int y, LPSTR lpszstr, int stringlength)

DrawText: This function is used to draw the specified text. It supports the text formatting also.
Syntax: int DrawText ( HDC hdc, LPSTR string, int length, LPRECT rect, UINT flags)

The value of the flag parameter is DT_TOP, DT_LEFT, DT_RIGHT,DT_CENTER

SetTextColor: It sets the text color for the device context to a specified color. The default text color for the device context is black. The
function allows any RGB color for the text output. The color stays in effect until the device context is released or another color is set.
Syntax: COLOREF SetTextColor (HDC hdc, COLORREF newcolor)

SetTextAlign: It sets the text alignment flags for the device context. This function allows an application to change the alignment
location to other location on the first character.

GETDC: GetDC () retrieves the handle of the display device context for the client area of the window. You can use the display DC in
subsequent GDI functions to draw in the client area.
Syntax: HDC GetDC( HWND hwnd)

ReleaseDC: It releases the device context, making it available for use by the other application. It releases the common and windows
DC.
Syntax: int ReleaseDC(HWND hWnd, HDC hdc)

Вам также может понравиться