Nicholas Mladenovic
Nicholas Mladenovic

Reputation: 33

vb6 using variables as DLL paths

I can't get this to work. It says string constant expected...

Dim const path As String = "\Windows\System32\user32"
Private Declare Function CallWindowProcW Lib path...

Any suggestions?

Upvotes: 2

Views: 543

Answers (2)

Akubik
Akubik

Reputation: 530

Private Declare Function CallWindowProcW Lib "User32" ...

Test it

Declare Statement

Example

The following example declares an external reference to a Function procedure that returns the current user name. It then calls the external procedure GetUserNameA as part of the getUser procedure.

Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA" (
    ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Sub getUser()
    Dim buffer As String = New String(CChar(" "), 25)
    Dim retVal As Integer = getUserName(buffer, 25)
    Dim userName As String = Strings.Left(buffer, InStr(buffer, Chr(0)) - 1)
    MsgBox(userName)
End Sub

Upvotes: 0

Jim Mack
Jim Mack

Reputation: 1144

As was said, you can't do that. If you use a path to a library it must be hard-coded.

However, if you don't use a hard-coded path there's a defined order in which Windows will look for for the library. It will search the app directory, the current dir, the Windows and System dirs, and along the current Path. If you put your DLL in any of those places, and omit the hard-coded path in the reference, it will be found (what order they're searched in varies by Windows version and by where it's documented).

There's one other possibility. If a DLL is already loaded in memory, Windows will reuse the loaded copy. So you can omit the path if you first load the DLL yourself, and for that you can use a string variable. Check out the docs for LoadLibrary. You do have to keep a handle and free the library yourself using FreeLibrary.

Upvotes: 5

Related Questions