Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function creates a device context (DC) for a device.

Syntax

HDC CreateDC( 
  LPCTSTR 
lpszDriver, 
  LPCTSTR 
lpszDevice, 
  LPCTSTR 
lpszOutput, 
  CONST DEVMODE* 
lpInitData
);

Parameters

lpszDriver

[in] Long pointer to a null-terminated string that specifies the file name of a driver. If this parameter is set to NULL, the system returns a screen DC.

lpszDevice

[in] Long pointer to a null-terminated string that specifies the name of the specific output device being used, as shown by the Print Manager. It is not the printer model name. The lpszDeviceparameter must be used.

In Windows CE 2.0 and later, this parameter is ignored.

lpszOutput

[in] Long pointer to an output destination.

It can be one of the following values, and the colon is required when specifying a port.

Value Description

COM x:

Serial ports; xis a number from 1 to 9.

LPT x:

Parallel ports; xis a number from 1 to 9.

IRDA:

IRDA port.

For Windows CE 2.0 and 2.01, use COM3: to specify the IRDA port.

\\ servername\sharename

Universal naming convention (UNC) that identifies a network printer server.

lpInitData

[in] Long pointer to a DEVMODEstructure containing device-specific initialization data for the device driver.

The lpInitDataparameter must be NULL if the device driver is to use the default initialization (if any) specified by the user.

Return Value

The handle to a device context for the specified device indicates success.

NULL indicates failure.

To get extended error information, call GetLastError.

Code Example

In the following code example, CreateDCis used to obtain a device context for the display driver.

Note:
To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.
Copy Code
typedef struct _tagUIEXTRA{
	HIMC	 hIMC;
	UICHILD  uiStatus;
	UICHILD  uiCand;
	DWORD	dwCompStyle;
	HFONT	hFont;
	BOOL	 bVertical;
	UICHILD  uiDefComp;
	UICHILD2 uiComp[MAXCOMPWND];
	UICHILD  uiGuide;
} UIEXTRA, NEAR *PUIEXTRA, FAR *LPUIEXTRA;
INT
GetCompFontHeight(
	IN LPUIEXTRA lpUIExtra	// IME UI extra information.
	)
{
	HDC hIC;
	HFONT hOldFont = 0;
	SIZE sz;
	hIC = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
	if (lpUIExtra->hFont)
		hOldFont = SelectObject(hIC,lpUIExtra->hFont);
	GetTextExtentPoint(hIC,TEXT("A"),1,&sz);
	if (hOldFont)
		SelectObject(hIC,hOldFont);
	DeleteDC(hIC);
	return sz.cy;
}

Remarks

Windows Embedded CE passes the lpInitDataand lpszOutputparameters to the driver without modification.

The lpszOutputparameter must contain a colon after the port — that is, use LPT1: or COM1:. Otherwise, the connection fails.

When you no longer need the device context, call the DeleteDCfunction to delete it.

Requirements

Header windows.h
Library coredll.lib
Windows Embedded CE Windows CE 2.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also