DirectWrite Errors in Win32 Application

This is a discussion on DirectWrite Errors in Win32 Application within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I am trying to get direct write to work in my application. I have been reading through a sample ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    74

    DirectWrite Errors in Win32 Application

    Hello, I am trying to get direct write to work in my application. I have been reading through a sample application called 'DWriteHelloWorld' that is part of the windows sdk but havent had much luck. Here is the error and my code
    Code:
     Error	1	error LNK2019: unresolved external symbol _D2D1CreateFactory@16 referenced in function "long __cdecl D2D1CreateFactory(enum D2D1_FACTORY_TYPE,struct _GUID const &,void * *)" (?D2D1CreateFactory@@YAJW4D2D1_FACTORY_TYPE@@ABU_GUID@@PAPAX@Z)	C:\DWrite_Test\DWrite_Test\DWrite_Test.obj
    Code:
    Error	2	error LNK2019: unresolved external symbol __imp__DWriteCreateFactory@12 referenced in function "long __cdecl CreateDeviceIndependentResources(void)" (?CreateDeviceIndependentResources@@YAJXZ)	C:\DWrite_Test\DWrite_Test\DWrite_Test.obj
    Code:
    Error	3	error LNK1120: 2 unresolved externals	C:\DWrite_Test\Debug\DWrite_Test.exe	1
    Code:
       // DWrite_Test.cpp : Defines the entry point for the application. //  #include "stdafx.h" #include "DWrite_Test.h"  #include  #include  #include  #include  #include   #define MAX_LOADSTRING 100  // Global Variables: HINSTANCE hInst;								// current instance TCHAR szTitle[MAX_LOADSTRING];					// The title bar text TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name  // Forward declarations of functions included in this code module: ATOM				MyRegisterClass(HINSTANCE hInstance); BOOL				InitInstance(HINSTANCE, int); LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);   HWND hwnd_;  // how much to scale a design that assumes 96-DPI pixels float dpiScaleX_; float dpiScaleY_;  // Direct2D  ID2D1Factory* pD2DFactory_; ID2D1HwndRenderTarget* pRT_; ID2D1SolidColorBrush* pBlackBrush_;   // DirectWrite  IDWriteFactory* pDWriteFactory_; IDWriteTextFormat* pTextFormat_;    const wchar_t* wszText_; UINT32 cTextLength_;    // SafeRelease inline function. template  inline void SafeRelease(T **ppT) { 	if (*ppT) 	{ 		(*ppT)->Release(); 		*ppT = NULL; 	} }        HRESULT DrawText() { 	RECT rc;  	GetClientRect( 		hwnd_, 		&rc 		);  	// Create a D2D rect that is the same size as the window.  	D2D1_RECT_F layoutRect = D2D1::RectF( 		static_cast(rc.top) / dpiScaleY_, 		static_cast(rc.left) / dpiScaleX_, 		static_cast(rc.right - rc.left) / dpiScaleX_, 		static_cast(rc.bottom - rc.top) / dpiScaleY_ 		);   	// Use the DrawText method of the D2D render target interface to draw.  	pRT_->DrawText( 		wszText_,        // The string to render. 		cTextLength_,    // The string's length. 		pTextFormat_,    // The text format. 		layoutRect,       // The region of the window where the text will be rendered. 		pBlackBrush_     // The brush used to draw the text. 		);   	return S_OK; }   void DiscardDeviceResources() {  	SafeRelease(&pRT_); 	SafeRelease(&pBlackBrush_);  }  HRESULT CreateDeviceResources() { 	HRESULT hr = S_OK;   	RECT rc; 	GetClientRect(hwnd_, &rc);  	D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);  	if (!pRT_) 	{ 		// Create a Direct2D render target. 		hr = pD2DFactory_->CreateHwndRenderTarget( 			D2D1::RenderTargetProperties(), 			D2D1::HwndRenderTargetProperties( 			hwnd_, 			size 			), 			&pRT_ 			);  		// Create a black brush. 		if (SUCCEEDED(hr)) 		{ 			hr = pRT_->CreateSolidColorBrush( 				D2D1::ColorF(D2D1::ColorF::Black), 				&pBlackBrush_ 				); 		} 	}   	return hr; }     HRESULT DrawD2DContent() { 	HRESULT hr;   	hr = CreateDeviceResources();  	if (!(pRT_->CheckWindowState() & D2D1_WINDOW_STATE_OCCLUDED)) 	{ 		pRT_->BeginDraw();  		pRT_->SetTransform(D2D1::IdentityMatrix());  		pRT_->Clear(D2D1::ColorF(D2D1::ColorF::White));  		// Call the DrawText method of this class. 		hr = DrawText();  		if (SUCCEEDED(hr)) 		{ 			hr = pRT_->EndDraw( 				); 		} 	}  	if (FAILED(hr)) 	{ 		DiscardDeviceResources(); 	}   	return hr; }     HRESULT CreateDeviceIndependentResources() { 	HRESULT hr;  	// Create Direct2D factory.  	hr = D2D1CreateFactory( 		D2D1_FACTORY_TYPE_SINGLE_THREADED, 		&pD2DFactory_ 		);   	// Create a shared DirectWrite factory.  	if (SUCCEEDED(hr)) 	{ 		hr = DWriteCreateFactory( 			DWRITE_FACTORY_TYPE_SHARED, 			__uuidof(IDWriteFactory), 			reinterpret_cast(&pDWriteFactory_) 			); 	}   	// The string to display.  	wszText_ = L"Hello World using  DirectWrite!"; 	cTextLength_ = (UINT32)wcslen(wszText_);   	// Create a text format using Gabriola with a font size of 72. 	// This sets the default font, weight, stretch, style, and locale.  	if (SUCCEEDED(hr)) 	{ 		hr = pDWriteFactory_->CreateTextFormat( 			L"Gabriola",                // Font family name. 			NULL,                       // Font collection (NULL sets it to use the system font collection). 			DWRITE_FONT_WEIGHT_REGULAR, 			DWRITE_FONT_STYLE_NORMAL, 			DWRITE_FONT_STRETCH_NORMAL, 			72.0f, 			L"en-us", 			&pTextFormat_ 			); 	}    	// Center align (horizontally) the text. 	if (SUCCEEDED(hr)) 	{ 		hr = pTextFormat_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); 	}  	if (SUCCEEDED(hr)) 	{ 		hr = pTextFormat_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); 	}   	return hr; }         // //  FUNCTION: MyRegisterClass() // //  PURPOSE: Registers the window class. // ATOM MyRegisterClass(HINSTANCE hInstance) { 	WNDCLASSEX wcex;  	wcex.cbSize = sizeof(WNDCLASSEX);  	wcex.style			= CS_HREDRAW | CS_VREDRAW; 	wcex.lpfnWndProc	= WndProc; 	wcex.cbClsExtra		= 0; 	wcex.cbWndExtra		= 0; 	wcex.hInstance		= hInstance; 	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DWRITE_TEST)); 	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW); 	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); 	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_DWRITE_TEST); 	wcex.lpszClassName	= szWindowClass; 	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));  	return RegisterClassEx(&wcex); }  // //   FUNCTION: InitInstance(HINSTANCE, int) // //   PURPOSE: Saves instance handle and creates main window // //   COMMENTS: // //        In this function, we save the instance handle in a global variable and //        create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {     HRESULT hr;    HWND hWnd;     hInst = hInstance; // Store instance handle in our global variable     hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);     if (!hWnd)    {       return FALSE;    }     hr = CreateDeviceIndependentResources();     ShowWindow(hWnd, nCmdShow);    UpdateWindow(hWnd);     if (SUCCEEDED(hr))    { 	   DrawD2DContent();    }     return TRUE; }     int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 	_In_opt_ HINSTANCE hPrevInstance, 	_In_ LPTSTR    lpCmdLine, 	_In_ int       nCmdShow) { 	UNREFERENCED_PARAMETER(hPrevInstance); 	UNREFERENCED_PARAMETER(lpCmdLine);  	// TODO: Place code here. 	MSG msg; 	HACCEL hAccelTable;  	// Initialize global strings 	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 	LoadString(hInstance, IDC_DWRITE_TEST, szWindowClass, MAX_LOADSTRING); 	MyRegisterClass(hInstance);  	// Perform application initialization: 	if (!InitInstance(hInstance, nCmdShow)) 	{ 		return FALSE; 	}  	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DWRITE_TEST));  	// Main message loop: 	while (GetMessage(&msg, NULL, 0, 0)) 	{ 		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 		{ 			TranslateMessage(&msg); 			DispatchMessage(&msg); 		} 	}  	return (int)msg.wParam; }    // //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // //  PURPOSE:  Processes messages for the main window. // //  WM_COMMAND	- process the application menu //  WM_PAINT	- Paint the main window //  WM_DESTROY	- post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { 	int wmId, wmEvent; 	PAINTSTRUCT ps; 	HDC hdc;  	switch (message) 	{ 	case WM_COMMAND: 		wmId    = LOWORD(wParam); 		wmEvent = HIWORD(wParam); 		// Parse the menu selections: 		switch (wmId) 		{ 		case IDM_ABOUT: 			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 			break; 		case IDM_EXIT: 			DestroyWindow(hWnd); 			break; 		default: 			return DefWindowProc(hWnd, message, wParam, lParam); 		} 		break; 	case WM_PAINT: 		hdc = BeginPaint(hWnd, &ps); 		// TODO: Add any drawing code here... 		EndPaint(hWnd, &ps); 		break; 	case WM_DESTROY: 		PostQuitMessage(0); 		break; 	default: 		return DefWindowProc(hWnd, message, wParam, lParam); 	} 	return 0; }  // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { 	UNREFERENCED_PARAMETER(lParam); 	switch (message) 	{ 	case WM_INITDIALOG: 		return (INT_PTR)TRUE;  	case WM_COMMAND: 		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 		{ 			EndDialog(hDlg, LOWORD(wParam)); 			return (INT_PTR)TRUE; 		} 		break; 	} 	return (INT_PTR)FALSE; }
    Edit, I have no idea why it put all my code on one line
    Last edited by EverydayDiesel; 01-25-2014 at 01:27 AM.

  2. #2
    Registered User
    Join Date
    Jan 2014
    Posts
    74
    Thanks for moving the thread. It seems I had a few problems (for those trying to do this) 1. You have to add these files to your linker d2d1.lib dwrite.lib 2. You need to save the hwnd when the window is created as a global variable

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    You really need to fix the 'all on one line' code, it is impossible to read (and so impossible to really help, without just guessing at the issue)...

    Have you included d2d1.h?
    Are you using a supported OS version; WIN7 or Vista with the required service packs?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Console Application event handlers, or Win32 Project?
    By Joewbarber in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2013, 07:02 AM
  2. Directwrite: Font height issues
    By Boxknife in forum Windows Programming
    Replies: 5
    Last Post: 04-11-2011, 06:09 PM
  3. win32 application designer
    By dayglo77 in forum Windows Programming
    Replies: 3
    Last Post: 08-01-2009, 06:57 AM
  4. Win32 Application
    By miiisuuu in forum Windows Programming
    Replies: 11
    Last Post: 10-27-2007, 08:49 PM
  5. win32 application
    By GravtyKlz in forum Windows Programming
    Replies: 5
    Last Post: 02-28-2003, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21