Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1470

[VB6] Code Snippet: Load Language Specific resource String. FindResourceEx

$
0
0
This came up in another thread.
A lot of declarations out there for FindResourceEx for VB6 aren't too accurate, probably a relic of people still using APIViewer or the like.

FindStringResourceEx() was translated from the C routine by Raymond Chen.
https://blogs.msdn.microsoft.com/old...0-00/?p=40813/

And Delphi version from this blog.
https://wiert.me/2014/07/17/delphi-a...pecific-lcids/

Unicode Compliant.

Code:

Option Explicit

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryW" (ByVal lpLibFileName As Long) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function FindResourceEx Lib "kernel32" Alias "FindResourceExW" (ByVal hModule As Long, ByVal lpType As Long, ByVal lpName As Long, ByVal wLanguage As Integer) As Long
Private Declare Function LoadResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long
Private Declare Function LockResource Lib "kernel32" (ByVal hResData As Long) As Long
Private Declare Function SysReAllocStringLen Lib "oleaut32" (ByVal pBSTR As Long, ByVal psz As Long, ByVal Length As Long) As Long
Private Declare Function GetMem2 Lib "msvbvm60" (Src As Any, Dst As Any) As Long
Private Const RT_STRING As Long = 6&

Private Property Get DUInt(ByVal Address As Long) As Long
    ' dereference a WORD* and copy to LOWORD of a Long
    GetMem2 ByVal Address, DUInt
End Property

'https://blogs.msdn.microsoft.com/oldnewthing/20040130-00/?p=40813/
Function FindStringResourceEx(ByVal hInstance As Long, uId As Long, langId As Long) As String
    Dim hResource As Long
    Dim hGlobal As Long
    Dim pwsz As Long, i As Long
    Const STRINGS_PER_BUCKET As Long = 16&
   
    hResource = FindResourceEx(hInstance, RT_STRING, uId \ STRINGS_PER_BUCKET + 1, langId)
    If (hResource) Then
        hGlobal = LoadResource(hInstance, hResource)
        If (hGlobal) Then
            pwsz = LockResource(hGlobal)
            If (pwsz) Then
                For i = 1 To (uId And (STRINGS_PER_BUCKET - 1))
                    pwsz = pwsz + 2 + 2 * DUInt(pwsz)
                Next
                SysReAllocStringLen VarPtr(FindStringResourceEx), pwsz + 2, DUInt(pwsz)
            End If
        End If
    End If
End Function

Private Sub Form_Click()
    Const YES_CAPTION = 805
    Const LANG_ID_ENGLISH_US = 1033
   
    Dim hModule As Long
    hModule = LoadLibrary(StrPtr("user32"))
    If (hModule) Then
        Debug.Print FindStringResourceEx(hModule, YES_CAPTION, 1033)
        FreeLibrary hModule
    End If
End Sub


Viewing all articles
Browse latest Browse all 1470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>