Solved

how to create resource in dll and use the resource in app?

Posted on 2003-11-10
1
306 Views
Last Modified: 2013-11-20
hi guys:
i am coding a small app,and write a dll (with mfc support) with a lot of text file in it.
i edit thedll.rc with notepad,add all text file in the rc file,such as
10002                   TEXTS   DISCARDABLE     "texts\\10002-first.txt"
in reource.h file,i do nothing.
the resource id is from 10002 to 30002(a huge one :));
my text file is acsii file and not more than 10kb.
in app,i use these code to load resource text:
            hRsrc=FindResource(hInstance,MAKEINTRESOURCE(10002),"TEXTS");
            hGlobal = LoadResource(hInstance, hRsrc);
            LPVOID pdata = LockResource(hGlobal);  
            LPBYTE sData = (LPBYTE)pdata;
            CString strTemp=(CString)(sData);
            FreeResource(hRsrc);
then compiled dll project and app project,all seems ok.
when the app running and load reosurce,sometimes it works fine,but sometimes it loads 2 or more text resources.(it loads 2 or three text files)
why? what's wrong with it?
can anyone tell me?
thank you guys ahead
johns
0
Comment
Question by:johnsmithz
1 Comment
 
LVL 3

Accepted Solution

by:
komar earned 50 total points
ID: 9720912
Hi,

Your code seem to be OK. But you are missing a call to SizeofResource() API which will help you determine the size of the resource you are looking for.
Here is what I suggest:

          hRsrc=FindResource(hInstance,MAKEINTRESOURCE(10002),"TEXTS");
          hGlobal = LoadResource(hInstance, hRsrc);

          TCHAR *szData = (TCHAR *)LockResource(h);
          DWORD dwSizeOfResource = SizeofResource(hRes, hr) / sizeof(TCHAR);

          CString strTemp;
          _tcsncpy(strTemp.GetBuffer(dwSizeOfResource), szData, dwSizeOfResource);
          strTemp.ReleaseBuffer(); //Data is now ready in strTemp

          FreeResource(hRsrc);

Thanks,
Khalid Omar.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now