Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / MFC
Article

Load a CString from DLL within that DLL

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 Aug 2000 88.4K   18   7
CString::LoadString(), resource will not load under certain conditions.

Introduction

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

#ifndef RESOURCE_TO_CSTRING
#define RESOURCE_TO_CSTRING

// 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.             http://www.goldencrater.com/software
	if (!hInst) {
		ASSERT(0);
		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;
}
#endif

License

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


Written By
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.

Comments and Discussions

 
QuestionProblen On Dialog In DLL Pin
Saday Sarkar4-Jan-06 20:28
Saday Sarkar4-Jan-06 20:28 
AnswerRe: Problen On Dialog In DLL Pin
CorruptedOne1-Feb-07 4:54
CorruptedOne1-Feb-07 4:54 
GeneralStrings aren't the only ones Pin
Wolfram Steinke8-Sep-00 14:27
Wolfram Steinke8-Sep-00 14:27 
GeneralAfxSetResourceHandle() is your friend Pin
Greg Marr14-Aug-00 5:07
Greg Marr14-Aug-00 5:07 
GeneralRe: AfxSetResourceHandle() is your friend Pin
Anonymous8-Oct-02 11:30
Anonymous8-Oct-02 11:30 
General...this is why an external file should keep the resource & commands ranges... Pin
Sardaukar11-Aug-00 21:07
Sardaukar11-Aug-00 21:07 
GeneralRe: ...this is why an external file should keep the resource & commands ranges... Pin
Jim Koornneef12-Aug-00 10:13
Jim Koornneef12-Aug-00 10: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.