Click here to Skip to main content
13,355,726 members (63,168 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 8 Aug 2000

Load a CString from DLL within that DLL

, 8 Aug 2000
Rate this:
Please Sign up or sign in to vote.
CString::LoadString(), resource will not load under certain conditions.
<!-- Add the rest of your HTML here -->


While we were designing the plugin architecture for epAssist, we discovered an interesting problem with the MFC implementation of CString::LoadString(). The string resource will not load under certain conditions.

Our plugin architecture is basically re-named DLLs with a standardized interface where the plugins provide a dictionary, grammar, and actions for the main language parser to parse sentences. On a successful parsing match, the program will invoke the proper action.

The main application loads the plugins (DLLs) using LoadLibrary, then locates and calls a function to get the description, copyright, and so forth.

The Problem

All the plugins are created with a Visual Studio AppWizard. Resources are already in place with strings for description, copyright, etc. Thus, every plugin has the same ID for the string.

CString::LoadString will first use the instance handle of the main application to search for a string with the given ID. Failing that, it will look in DLLs in the order they were loaded until there is a match.

If DLL A is loaded, then B and C, calling LoadString from DLL C will first find a matching resource in DLL A before finding its own resource.

The end result of this is that all plugins show the same description and other properties.

The Solution

The function LoadStringFromDLL() was created. It takes an instance handle (can be the main application, any DLL, or any other application) and the resource ID.

Using this as a starting point, you can also create LoadIconFromDLL, or any other resource type.

There is an artificial limit of 255 characters for the string resource. You can change the memory allocation scheme to suit your own needs.

// Load a resource from a given instance

#pragma once


// Comments Doxygen compatible

/** Load a resource from a given instance
* It seems that if you are in a DLL, the standard MFC checks the instance of the 
App, then each DLL in the order loaded. This is no good if someone else is using
the same ID!
* @param hInst the instance to load from
* @param ids the id to load.
* @ingroup global
inline CString LoadStringFromDLL(HINSTANCE hInst, int ids) 
	// (c) 1998-2000 Golden Crater Software, All rights reserved.
	// You may freely use this code
	// You may freely redistribute this code, provided this comment block remains 
        // intact.
	// The body of this function is from the epAssist EAX SDK:
	//   Use plain English to control your PC over email, AppBar, and soon 
        //   voice telephone. 
	//   Check out this free SDK that allows you to interface to epAssist. 
	//   Easily add natural language functionality to your application and 
	//   provide your users with access to your program from anywhere they 
	//   can send and receive email (2-way pagers, public terminals and email 
	//   enabled telephones included.) MFC AppWizard and HTML Help, and sample
	//   source included.   
	if (!hInst) {
		return "Error loading resource.";
	CString str;
	TCHAR szTemp[256];		// Raise limit of 255 char if needed.
	int nLen;
	LPCTSTR lpszName = MAKEINTRESOURCE((ids>>4)+1);
	if (::FindResource(hInst, lpszName, RT_STRING) != NULL &&
		(nLen = ::LoadString(hInst, ids, szTemp, 255) != 0)) {
		str = szTemp;
	return str;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jim Koornneef
Web Developer Golden Crater Corp
Canada Canada
Jim is the President of Golden Crater Corp. (formerly Golden Crater Software) which produces:

Tiny eBook Reader - Read eBooks anywhere, on any web enabled device or phone.

Doberman BMS - Home Automation and Building Management System bridging and enhancing several automation hardware platforms.

You may also be interested in...

Comments and Discussions

QuestionProblen On Dialog In DLL Pin
Saday Sarkar4-Jan-06 21:28
memberSaday Sarkar4-Jan-06 21:28 
AnswerRe: Problen On Dialog In DLL Pin
CorruptedOne1-Feb-07 5:54
memberCorruptedOne1-Feb-07 5:54 
GeneralStrings aren't the only ones Pin
Wolfram Steinke8-Sep-00 15:27
sussWolfram Steinke8-Sep-00 15:27 
GeneralAfxSetResourceHandle() is your friend Pin
Greg Marr14-Aug-00 6:07
sussGreg Marr14-Aug-00 6:07 
GeneralRe: AfxSetResourceHandle() is your friend Pin
Anonymous8-Oct-02 12:30
sussAnonymous8-Oct-02 12:30 
Is this tread-wide of application wide?

I assumed it's for the whole application. Frown | :(

The plugins can be running in several threads.
General...this is why an external file should keep the resource & commands ranges... Pin
Sardaukar11-Aug-00 22:07
sussSardaukar11-Aug-00 22:07 
GeneralRe: ...this is why an external file should keep the resource & commands ranges... Pin
Jim Koornneef12-Aug-00 11:13
sussJim Koornneef12-Aug-00 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 9 Aug 2000
Article Copyright 2000 by Jim Koornneef
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid