Solved

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

Posted on 2003-11-10
1
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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