Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use GetProcAddress in VB ?

0 views
Skip to first unread message

Filex Yeung

unread,
Jun 19, 1998, 3:00:00 AM6/19/98
to

In order to load a DLL in VB Application in runtime, I use LoadLibrary API
and the DLL is loaded successfully. However, how can I use the functions in
DLL ?

In C, I use GetProcAddress( ) to retrieve the function pointer in DLL. How
about in VB ?

Bests,
Filex Yeung
E-Mail Address : filex...@ctint.com

Ben Baird (MSMVP)

unread,
Jun 19, 1998, 3:00:00 AM6/19/98
to

Filex...

>In order to load a DLL in VB Application in runtime, I use LoadLibrary API
>and the DLL is loaded successfully. However, how can I use the functions
in
>DLL ?
>

You can't do it this way in VB. You have to write a Declare statement to use
functions in DLLs.

--
Ben Baird, MVP
Microsoft SiteBuilder Network Level 2
Visual Basic Thunder, v. 2.0
http://www.cyberhighway.com/~psy/
Common Controls Replacement Project, Official Member
http://www.mvps.org/ccrp/
Please direct your programming questions to the newsgroups.


Esko

unread,
Jun 19, 1998, 3:00:00 AM6/19/98
to

You can get the pointer using VB but it doesn't do any good cause you can't
call the function using the pointer once you have the pointer!

If all you want to do is call a C function in a DLL make sure the function
is declared as WINAPI. (Also make sure the function is exported from the
DLL) Then use the DECLARE statement in visual basic to explain to the VB
program in which DLL the function resides and what the parameters are.
(All this is so that VB can help "SHIELD" you from the complexities of the
process - like it or not)

Esko


Filex Yeung <filex...@ctint.com> wrote in article
<358a3...@news01.imsbiz.com>...

Filex Yeung

unread,
Jun 24, 1998, 3:00:00 AM6/24/98
to

First of all, thank you for Esko suggestion. However, eventually I want to
load and unload a DLL in runtime such that I can replace the DLL without
stopping the VB Application.

The problem is , can I retrieve the function pointer in DLL by using
GetProcAddress( ) WINAPI ? Even I can retrieve the function pointer, How to
use such pointer ? How to call the function and pass arguments ?

Filex Yeung
E-Mail Address : filex...@ctint.com


>Esko wrote in message :

Sebastian Odin Smith

unread,
Jun 26, 1998, 3:00:00 AM6/26/98
to

You had better make sure that DLL is thread-safe or it will crash the
system. Creating threads with VB doesn't work well under many circumstances.
Third-party controls that are not apartment thread-safe will cause all kinds
of trouble and don't even try it in devlopement mode. Threads are just
damned hard to debug.

-Sebastian Odin Smith

>I haven't tried it yet, but a suggestion for using function pointer is
>VB is to setup a module level boolean, call CreateThread passing the
>address of the function, and monitor the boolean in the main thread.
>Have the spawned thread set the boolean just before terminating.

Simon Morley

unread,
Jun 29, 1998, 3:00:00 AM6/29/98
to

I have a Dll which solves the problem if anyone is interested.
I didn't do it like this though!

Gerry Thomas

unread,
Jun 29, 1998, 3:00:00 AM6/29/98
to Simon Morley

I would be interested in knowing how you call GetProcAddress from vb.

TIA,
Gerry T.


Ian McLaren

unread,
Jun 29, 1998, 3:00:00 AM6/29/98
to

Gerry Thomas wrote:
>
>
> I would be interested in knowing how you call GetProcAddress from vb.
>
> TIA,
> Gerry T.

Hi Gerry:

'Code snippet starts:------------------------------------
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal
lpLibFileName As String) As Long
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long,
ByVal lpProcName As String) As Long
Declare Function FreeLibrary Lib "kernel32" (ByVal hModule As Long) As
Long

Public DLLModuleHandle as Long,DLLProcAddress as Long

DLLModuleHandle = LoadLibrary("c\windows\system\mydll.dll"+chr$(0))
DLLProcAddress = GetProcAddress(DLLModuleHandle, "MyFunction")
'code snippet ends--------------------------------------------

This assumes your DLL is "mydll.dll" and that the exported DLL function
is MyFunction.

At some point later in the code you'll have to do a FreeLibrary call or
your DLL will remain loaded (at least in the VB development environment
-- I haven't yet seen if that's true for EXEs).

It's true that you can't call the procedure address directly from VB5
using the returned DLLProcAddress -- I haven't tried the Thread method
so can't comment. I use the above code to pass the DLLProcAddress value
to a "call forwarding" DLL written in C, which in turn calls my main DLL
written in Assembly Language. I do this because I have never yet been
able to call a 32-bit ASM DLL (written using TASM 5.0) directly from
VB5, and this is my workaround patch.

If anyone *has* managed to call TASM DLLs from VB5, I'd love to hear how
they did it! I was never able to get VB5 to find the ASM DLL using the
standard Declare method. GetProcAddress works fine, however....

Regards,

Ian McLaren

J. Boschen

unread,
Jun 30, 1998, 3:00:00 AM6/30/98
to

Strange that you couldn't use your DLL written in ASM. The ones I've written
using NASM and MS's linker work fine.

--
Jeremy - my reply address is foiled


Ian McLaren wrote in message <35974C...@california.com>...

0 new messages