Solved

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

Posted on 2003-11-10
1
305 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

6 Experts available now in Live!

Get 1:1 Help Now