-
Jul 17th, 2014, 05:53 AM
#1
Thread Starter
Addicted Member
[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
-
Jul 17th, 2014, 06:41 AM
#2
Re: Read Text file content from External DLL Resource file
The InStr() function requires a start position:
Code:
lgBuffPos = InStr(1, stBuff, Chr$(0))
-
Jul 17th, 2014, 07:32 AM
#3
Thread Starter
Addicted Member
Re: Read Text file content from External DLL Resource file
Originally Posted by Arnoutdv
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!
-
Jul 17th, 2014, 08:14 AM
#4
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.
-
Jul 17th, 2014, 08:44 AM
#5
Thread Starter
Addicted Member
Re: Read Text file content from External DLL Resource file
Originally Posted by Doogle
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.
-
Jul 17th, 2014, 09:57 AM
#6
Thread Starter
Addicted Member
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...
-
Jul 17th, 2014, 09:57 AM
#7
Thread Starter
Addicted Member
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...
-
Jul 17th, 2014, 12:22 PM
#8
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?
-
Jul 17th, 2014, 12:52 PM
#9
Thread Starter
Addicted Member
Re: Read Text file content from External DLL Resource file
Originally Posted by Doogle
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.
-
Jul 17th, 2014, 03:13 PM
#10
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
-
Jul 18th, 2014, 06:21 AM
#11
Thread Starter
Addicted Member
Re: Read Text file content from External DLL Resource file
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|