Results 1 to 11 of 11

Thread: [RESOLVED] Read Text file content from External DLL Resource file

  1. #1

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Resolved [RESOLVED] Read Text file content from External DLL Resource file

    Hi there,

    I'm trying to load external text from my custom DLL resource file, but it's not working.

    Resource file content (Test.dll, Test.rc and Test.res containing Eula.txt), the Eula.txt file is about 6Kb:

    Code:
    900 CUSTOM  "Eula.txt"
    I'm using the code like this:
    Code:
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long  
    Private Declare Function LoadString Lib "user32" Alias "LoadStringA" (ByVal hInstance As Long, ByVal wID As Long, ByVal lpBuffer As String, ByVal nBufferMax As Long) As Long  
    Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    Code:
    Public Function LoadStrRes(lgResID As Long) As String  
     On Error Resume Next  
       
     Dim lgRet As Long  
     Dim stBuff As String * 32768  
     Dim lgBuffPos As Long  
     Dim stFile As String  
       
     stFile = App.Path & "\Test.dll"  
       
     lgRet = LoadLibrary(stFile)  
      
     Call LoadString(lgRet, lgResID, stBuff, Len(stBuff))  
      
     lgBuffPos = InStr(1, stBuff, Chr$(0))  
      
     LoadStrRes = Left$(stBuff, lgBuffPos - 1)  
      
     Call FreeLibrary(lgRet)  
       
     Exit Function  
    End Function
    Calling the function like this:
    Code:
    Dim stTxtData As String  
       
    stTxtData = LoadStrRes(900)  
      
    txtEula.Text = stTxtData
    Can someone tell my why is it not working for me?

    Regards
    Last edited by beic; Jul 17th, 2014 at 03:08 PM. Reason: Code typo

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Read Text file content from External DLL Resource file

    The InStr() function requires a start position:
    Code:
    lgBuffPos = InStr(1, stBuff, Chr$(0))

  3. #3

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    Quote Originally Posted by Arnoutdv View Post
    The InStr() function requires a start position:
    Code:
    lgBuffPos = InStr(1, stBuff, Chr$(0))
    Corrected that, but still nothing, I'm getting empty String from function!

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Read Text file content from External DLL Resource file

    InStr does not 'need' a starting position, it will default to 1.

    I suspect that the invokation of the LoadString Function is failing. You should not 'Call" API Functions as they all return codes to indicate success or failure. Replace Call LoadString....... with lngReturn = LoadString..... where lngReturn is defined a Long. In the case of LoadSting lngReturn will hold the numbr of characters copied to stBuff. If that is zero you can use Err.LastDLLError to determine the cause of the error.

  5. #5

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    Quote Originally Posted by Doogle View Post
    InStr does not 'need' a starting position, it will default to 1.

    I suspect that the invokation of the LoadString Function is failing. You should not 'Call" API Functions as they all return codes to indicate success or failure. Replace Call LoadString....... with lngReturn = LoadString..... where lngReturn is defined a Long. In the case of LoadSting lngReturn will hold the numbr of characters copied to stBuff. If that is zero you can use Err.LastDLLError to determine the cause of the error.
    Hi Doogle!

    I did what you said and I got this error 1814 (ERROR_RESOURCE_NAME_NOT_FOUND), but the resource is there!

    Here is the full content of Test.rc:

    Code:
    STRINGTABLE DISCARDABLE 
    BEGIN
        100                "1.0.0"
    END
    
    900			CUSTOM	"Eula.txt"
    Can you help me out?
    Last edited by beic; Jul 17th, 2014 at 08:47 AM.

  6. #6

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    I found a solution, but it's in C++ language and I don't know how to convert it to VB6...

    Define the resource ID, add this to the .rc file:

    Code:
    ID_CUSTOM1 ANYTHINGGOESHERE "filename.txt"
    Read it at runtime with code like this:

    Code:
    HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(ID_CUSTOM1), L"ANYTHINGGOESHERE");
    HGLOBAL hMem = LoadResource(hInst, hRes);
    DWORD size = SizeofResource(hInst, hRes);
    char* resText = (char*)LockResource(hMem);
    char* text = (char*)malloc(size + 1);
    memcpy(text, resText, size);
    text[size] = 0;
    FreeResource(hMem);
    // use text...

  7. #7

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    I found a solution, but it's in C++ language and I don't know how to convert it to VB6...

    Define the resource ID, add this to the .rc file:

    Code:
    ID_CUSTOM1 ANYTHINGGOESHERE "filename.txt"
    Read it at runtime with code like this:

    Code:
    HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(ID_CUSTOM1), L"ANYTHINGGOESHERE");
    HGLOBAL hMem = LoadResource(hInst, hRes);
    DWORD size = SizeofResource(hInst, hRes);
    char* resText = (char*)LockResource(hMem);
    char* text = (char*)malloc(size + 1);
    memcpy(text, resText, size);
    text[size] = 0;
    FreeResource(hMem);
    // use text...

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Read Text file content from External DLL Resource file

    Shouldn't the resource be defined between the Begin and End statements?
    Can you read resource 100?

  9. #9

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    Quote Originally Posted by Doogle View Post
    Shouldn't the resource be defined between the Begin and End statements?
    Can you read resource 100?
    Yes, I can read resource ID 100, but it's is a simple STRINGTABLE...but, I want to load resource ID 900, and it's a CUSTOM defined resource, the whole text file.

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Read Text file content from External DLL Resource file

    I think you're using the wrong API ... while what you're trying to load is a string, it's not a string resource. Everything I'm reading about LoadString is that it's for loading string resources. You file though is a custom... which means it's going to be stuffed into the resource file as binary data.

    Here's a thread where someone embedded an Access database in theirs -http://www.vbforums.com/showthread.php?212366-Copy-from-RESOURCE-file&s=&highlight=resource%20AND%20custom - I know you're only using text, BUT it's still a custom resource. Post #14 has a code snip where the returned bits were StrConv ... My current theory is you need to do something similar. Pull out the bytes, convert to a string, then you should have something to display.

    At this point, it can't hurt.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Addicted Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    150

    Re: Read Text file content from External DLL Resource file

    Quote Originally Posted by techgnome View Post
    I think you're using the wrong API ... while what you're trying to load is a string, it's not a string resource. Everything I'm reading about LoadString is that it's for loading string resources. You file though is a custom... which means it's going to be stuffed into the resource file as binary data.

    Here's a thread where someone embedded an Access database in theirs -http://www.vbforums.com/showthread.php?212366-Copy-from-RESOURCE-file&s=&highlight=resource%20AND%20custom - I know you're only using text, BUT it's still a custom resource. Post #14 has a code snip where the returned bits were StrConv ... My current theory is you need to do something similar. Pull out the bytes, convert to a string, then you should have something to display.

    At this point, it can't hurt.

    -tg
    Hi techgnome,

    It's true about API... I need to use this API instead, but I cant figure out how to do it, because I need to load data from external resource file, NOT from it self (same EXE or DLL).

    Code:
    FindResource  
    LoadResource  
    SizeofResource  
    LockResource  
    FreeResource

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width