Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a blank for a LoadString from a resource only dll

Status
Not open for further replies.

richsb

Technical User
Aug 2, 2001
34
NO
Hi

I have created a resource only dll. When I call a string resource from it the messagebox comes up blank. Here's the code. I am not sure whether it is the typecast from char to CString that I have used. Any ideas appreciated.

Code:
void CDllImageDlg::OnOpenFile() 
{
	CFileDialog dlg(
		true,	//create an open file dialog
		NULL,	//default file extension
		NULL,	//filename
		NULL,
                _T("Wreck Data Files (.dll)| .dll|")  
                //filter string
		_T("All files (.txt)| *.*|")
		_T("|")
		);

		if (IDOK != dlg.DoModal())  // display open file dialog
			return;					// do nothing if open file dialog cancel button pressed

		// take selected file and insert file name and path into a public CString variable for later use
		m_NewDatFile = dlg.GetFileName();
	
		// call the dll

		// create a handle to the dll
		HINSTANCE hDLL;       // Handle to DLL
		// ID for the string resource
		UINT uID = 0; 
		// create a pointer to a buffer for the string resource
		char Buffer;
		char *lpBuffer = &Buffer;
		// max size of buffer
		int nBufferMax = 100;
		// call the function
		hDLL = LoadLibrary("m_NewDatFile");
		// call the resource function
		LoadString(
		hDLL,// handle to module containing string resource
		uID, // resource identifier
		lpBuffer, //  pointer to buffer for resource
		nBufferMax // size of buffer
		);

		// error checking
		if (hDLL != NULL)
                 {
		// typecast from char to CString
		CString strBuffer ;
		strBuffer = static_cast<CString>(Buffer);
		MessageBox(strBuffer);
                 }
		else
			MessageBox("failed load");
		}
 
>>hDLL = LoadLibrary("m_NewDatFile");

m_NewDatFile probably is a CString, right?

Calling LoadLibrary like so, won;t actually load the library; more chance with LoadLibrary(m_NewDatFile)


>>UINT uID = 0;

Assuming that the handle to the library you get returned is valid; is the ID of the string resource really 0? This seems higly unlikely...


>>// create a pointer to a buffer for the string resource
char Buffer;
char *lpBuffer = &Buffer;
// max size of buffer
int nBufferMax = 100;

Where do you actually allocate 100 characters? Maybe I'm missing somthing here, or maybe you did not paste all your code here, but you really do need to allocate the buffer to receive the string yourself.

Greetings,
Rick
 
Rick,

Thanks, you spotted my errors in the code that I found myself last night.

Yes, the m_NewDatFile is without the quotes, and the real error was the UINT uID which should have been 1. So really it was working all along.
As for the 100 characters I just threw that in to make sure there was enough room.

Rich
 
>>As for the 100 characters I just threw that in to make sure there was enough room.

I don't understand? I noticed you send a buffersize of 100 characters to the lib call. The only thing I do not notice is that you actually allocated this buffer:

You create a buffer of 1 character (char Buffer). Then you create a pointer to that buffer (char *lpBuffer = &Buffer), which is actually not necessary, but technically not wrong. However, then you tell the library that the buffer you created is 100 characters large? It will fill up the memory with the characters, up to 100 if the string is that large. This is a huge problem howevr, since it's writing beyond the buffer of 1 character that you have allocated....

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top