Home All Groups Group Topic Archive Search About

Calling API functions dynamically?

Author
22 May 2006 5:59 PM
Usenet User
I need to be able to call the same API function from VB, which will
reside in different DLLs. While all of the functions have the same
signature and name, DLL file names are not known at compile time.
Therefore, my assumption that using Declare statement or DllImport
attribute is NOT an option in such a case.

I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
addreses, but is there a way to actually call an API method by its
address from VB? Or can anyone suggest anything else?

TIA!

Author
22 May 2006 7:52 PM
Thorsten Albers
Usenet User <  @ .  > schrieb im Beitrag
<tau372d0grgrb2mhhdo6g00upj34b5m***@4ax.com>...
> I need to be able to call the same API function from VB, which will
> reside in different DLLs. While all of the functions have the same
> signature and name, DLL file names are not known at compile time.
> Therefore, my assumption that using Declare statement or DllImport
> attribute is NOT an option in such a case.
>
> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
> addreses, but is there a way to actually call an API method by its
> address from VB? Or can anyone suggest anything else?

I can think of two ways:

a) Build and use a standard DLL (C(++)) which redirects the function call
to the respective DLL.

b) Add a declare statement with a - more or less :-) - unique library file
name. Then, before calling the library function, make a copy of the
original library and name it with this unique file name. The copy of the
library of course has to be located somewhere where the OS can find it.

--
----------------------------------------------------------------------
THORSTEN ALBERS                        Universität Freiburg
                                                albers@
                                                       uni-freiburg.de
----------------------------------------------------------------------
Author
23 May 2006 5:03 PM
Usenet User
On Mon, 22 May 2006 12:52:10 -0700, "Thorsten Albers"
<alber***@MOVEuni-freiburg.de> wrote:

Show quoteHide quote
>Usenet User <  @ .  > schrieb im Beitrag
><tau372d0grgrb2mhhdo6g00upj34b5m***@4ax.com>...
>> I need to be able to call the same API function from VB, which will
>> reside in different DLLs. While all of the functions have the same
>> signature and name, DLL file names are not known at compile time.
>> Therefore, my assumption that using Declare statement or DllImport
>> attribute is NOT an option in such a case.
>>
>> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
>> addreses, but is there a way to actually call an API method by its
>> address from VB? Or can anyone suggest anything else?
>
>I can think of two ways:
>
>a) Build and use a standard DLL (C(++)) which redirects the function call
>to the respective DLL.
>

That's what I ended up doing. So far so good.

>b) Add a declare statement with a - more or less :-) - unique library file
>name. Then, before calling the library function, make a copy of the
>original library and name it with this unique file name. The copy of the
>library of course has to be located somewhere where the OS can find it.

Was thinking of this, but decided no to pursue this path...

Thanks!
Author
22 May 2006 8:30 PM
Chris Dunaway
Usenet User wrote:
> I need to be able to call the same API function from VB, which will
> reside in different DLLs. While all of the functions have the same

Why would you do this?  Are you trying to create some sort of "plugin"
architecture?  Do you have control over the .dll you are trying to call
or are they 3rd party?

Perhaps you could create a single class in which you pass the name of
the .dll as a string and then inside the class, call the api method
based on that string.

Good luck
Author
22 May 2006 9:12 PM
aaron.kempf@gmail.com
cant you just use
CallByName "Msgbox('Hello World')"
Author
23 May 2006 1:22 AM
Michael C
<aaron.ke***@gmail.com> wrote in message
news:1148332371.274366.192150@i39g2000cwa.googlegroups.com...
> cant you just use
> CallByName "Msgbox('Hello World')"

No
Show quoteHide quote
>
Author
23 May 2006 5:06 PM
Usenet User
On 22 May 2006 13:30:39 -0700, "Chris Dunaway" <dunaw***@gmail.com>
wrote:

>Usenet User wrote:
>> I need to be able to call the same API function from VB, which will
>> reside in different DLLs. While all of the functions have the same
>
>Why would you do this?  Are you trying to create some sort of "plugin"
>architecture?  Do you have control over the .dll you are trying to call
>or are they 3rd party?
>

Yes, we have a bunch of DLL from different vendors that implement the
same methods.

>Perhaps you could create a single class in which you pass the name of
>the .dll as a string and then inside the class, call the api method
>based on that string.

Well, I am not quite sure what you have in mind. I would still need a
static Declare with the hardcoded DLL name. No?

Thanks!
Author
22 May 2006 9:52 PM
Tom Esh
On Mon, 22 May 2006 10:59:48 -0700, Usenet User <  @ .  > wrote:

>I need to be able to call the same API function from VB, which will
>reside in different DLLs. While all of the functions have the same
>signature and name, DLL file names are not known at compile time.
>Therefore, my assumption that using Declare statement or DllImport
>attribute is NOT an option in such a case.
>
>I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
>addreses, but is there a way to actually call an API method by its
>address from VB? Or can anyone suggest anything else?

Another way (hack) is to use the function address obtained with
GetProcAddress with CallWindowProc. The catch here (big catch) is the
function being called needs to take exactly 4 32-bit args or you'll
hose the stack. There are ways around that limitation (using
~assembly~ opcodes to restore the stack). Matt Currland's book
"Advanced VB6" covers the technique. Definitely not for the feint of
heart - pure voodoo IMO <g>


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Author
22 May 2006 11:06 PM
mr_unreliable
hi Usenet,

There are two pre-programmed, pre-digested solutions to this.

1.  My favorite, written by Arkadiy Olovyannikov, and called:
"Call API functions by Name, without Declare", can be found
on the freevbcode website:

   http://www.freevbcode.com/ShowCode.Asp?ID=1863

2.  However, the api newsgroupies here may be expected to heap
disrespect on Arkadiy's code.  They have asserted that the only
"real" legitimate appropriate code to do this was written by
Matthew Curland. There is an article by Curland on this subject,
originally published in "Visual Basic Programmer's Journal",
and now available on the web, entitled: "Call Function Pointers".

   http://www.fawcette.com/archives/listissue.asp?pubID=1&MagIssueId=265#

Also, if you have $40 in spare change, (and you can still find it)
you may purchase Curland's book "Advanced Visual Basic 6", and you
will find the same "Call Function Pointers" code in there.

cheers, jw
____________________________________________________________

You got questions?  WE GOT ANSWERS!!!  ..(but,
    no guarantee the answers will be applicable to the questions)

Usenet wrote:
Show quoteHide quote
> I need to be able to call the same API function from VB, which will
> reside in different DLLs. While all of the functions have the same
> signature and name, DLL file names are not known at compile time.
> Therefore, my assumption that using Declare statement or DllImport
> attribute is NOT an option in such a case.
>
> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
> addreses, but is there a way to actually call an API method by its
> address from VB? Or can anyone suggest anything else?
>
> TIA!
Author
22 May 2006 11:35 PM
Randy Birch
Matt's book still appears to be available through
http://service.bfast.com/bfast/click?bfmid=2181&sourceid=251059&bfpid=0201707128&bfmtype=book

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.




"mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
news:%23yL9SPffGHA.764@TK2MSFTNGP03.phx.gbl...
hi Usenet,

There are two pre-programmed, pre-digested solutions to this.

1.  My favorite, written by Arkadiy Olovyannikov, and called:
"Call API functions by Name, without Declare", can be found
on the freevbcode website:

   http://www.freevbcode.com/ShowCode.Asp?ID=1863

2.  However, the api newsgroupies here may be expected to heap
disrespect on Arkadiy's code.  They have asserted that the only
"real" legitimate appropriate code to do this was written by
Matthew Curland. There is an article by Curland on this subject,
originally published in "Visual Basic Programmer's Journal",
and now available on the web, entitled: "Call Function Pointers".

   http://www.fawcette.com/archives/listissue.asp?pubID=1&MagIssueId=265#

Also, if you have $40 in spare change, (and you can still find it)
you may purchase Curland's book "Advanced Visual Basic 6", and you
will find the same "Call Function Pointers" code in there.

cheers, jw
____________________________________________________________

You got questions?  WE GOT ANSWERS!!!  ..(but,
    no guarantee the answers will be applicable to the questions)

Usenet wrote:
Show quoteHide quote
> I need to be able to call the same API function from VB, which will
> reside in different DLLs. While all of the functions have the same
> signature and name, DLL file names are not known at compile time.
> Therefore, my assumption that using Declare statement or DllImport
> attribute is NOT an option in such a case.
>
> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
> addreses, but is there a way to actually call an API method by its
> address from VB? Or can anyone suggest anything else?
>
> TIA!
Author
23 May 2006 1:36 AM
Michael C
"mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
news:%23yL9SPffGHA.764@TK2MSFTNGP03.phx.gbl...
> hi Usenet,
>
> There are two pre-programmed, pre-digested solutions to this.
>
> 1.  My favorite, written by Arkadiy Olovyannikov, and called:
> "Call API functions by Name, without Declare", can be found
> on the freevbcode website:
>
>   http://www.freevbcode.com/ShowCode.Asp?ID=1863

I downloaded this code and much to my suprise it was a modification to some
code I put on the net many years ago. I don't see anything wrong with the
method except it could be optimised not to build the assembly code for each
call in this function:

Public Function CallFunction(ParamArray FuncParams()) As Long
   Dim i As Long
   If m_lpFn = 0 Then
      MsgBox "Function not defined!", vbCritical, "Call function error"
      Exit Function
   End If
   ReDim m_abCode(0)
   ReDim m_lParameters(UBound(FuncParams) + 1)
   ReDim m_abCode(18 + 32 + 6 * UBound(m_lParameters))
   For i = 1 To UBound(m_lParameters)
      m_lParameters(i) = CLng(FuncParams(i - 1))
   Next i
   CallFunction = CallWindowProc(PrepareCode, 0, 0, 0, 0)
   m_lpFn = 0
End Function

> 2.  However, the api newsgroupies here may be expected to heap
> disrespect on Arkadiy's code.  They have asserted that the only
> "real" legitimate appropriate code to do this was written by
> Matthew Curland. There is an article by Curland on this subject,
> originally published in "Visual Basic Programmer's Journal",
> and now available on the web, entitled: "Call Function Pointers".

I don't see how matthew's method would be any less hacky. I can't see it
though as that page requires a login.

Michael
Author
23 May 2006 1:40 AM
Karl E. Peterson
Michael C wrote:
>> 2.  However, the api newsgroupies here may be expected to heap
>> disrespect on Arkadiy's code.  They have asserted that the only
>> "real" legitimate appropriate code to do this was written by
>> Matthew Curland. There is an article by Curland on this subject,
>> originally published in "Visual Basic Programmer's Journal",
>> and now available on the web, entitled: "Call Function Pointers".
>
> I don't see how matthew's method would be any less hacky. I can't see
> it though as that page requires a login.

http://www.BugMeNot.com  <- HTH!
--
Working without a .NET?
http://classicvb.org/
Author
23 May 2006 2:00 AM
Michael C
"Karl E. Peterson" <k***@mvps.org> wrote in message
news:uZz6xngfGHA.1520@TK2MSFTNGP03.phx.gbl...
>> I don't see how matthew's method would be any less hacky. I can't see
>> it though as that page requires a login.
>
> http://www.BugMeNot.com  <- HTH!

Considering what I have seen of matt's I'd imagine it would be significantly
more hacky.

Michael
Author
23 May 2006 2:17 AM
Michael C
"Karl E. Peterson" <k***@mvps.org> wrote in message
news:uZz6xngfGHA.1520@TK2MSFTNGP03.phx.gbl...
>> I don't see how matthew's method would be any less hacky. I can't see
>> it though as that page requires a login.
>
> http://www.BugMeNot.com  <- HTH!

Ok, I had a look at his code. It shoves assembly code into a currency
variable and does pretty much the same thing but requires the extra
complexity of creating and referencing a typelib (presumably a new typelib
for each different call signature). It also has the reduced flexibility of
not being able to call cdecl functions. Matt has a bad habit of
overcomplicating things. But then again karl, don't let logic get in your
way. :-)

Michael
Author
23 May 2006 3:34 AM
Stefan Berglund
On Tue, 23 May 2006 12:17:36 +1000, "Michael C" <nospam@nospam.com>
wrote:
in <OHP2U7gfGHA.5***@TK2MSFTNGP02.phx.gbl>

Show quoteHide quote
>"Karl E. Peterson" <k***@mvps.org> wrote in message
>news:uZz6xngfGHA.1520@TK2MSFTNGP03.phx.gbl...
>>> I don't see how matthew's method would be any less hacky. I can't see
>>> it though as that page requires a login.
>>
>> http://www.BugMeNot.com  <- HTH!
>
>Ok, I had a look at his code. It shoves assembly code into a currency
>variable and does pretty much the same thing but requires the extra
>complexity of creating and referencing a typelib (presumably a new typelib
>for each different call signature). It also has the reduced flexibility of
>not being able to call cdecl functions. Matt has a bad habit of
>overcomplicating things. But then again karl, don't let logic get in your
>way. :-)
>
>Michael
>

You must be one of those irreverent dot net freaks that think you can
easily discredit someone who spent years building their reputation
simply by virtue of vomiting your spew.

m$' latest piece of f**k doesn't qualify you to make those allegations
but I must admit you do provide great comic relief for those who are
more discerning in what they read and believe.

Thanks for the laughs hyena.

---
This posting is provided "AS IS" with no warranties and no guarantees either express or implied.

Stefan Berglund
Author
23 May 2006 3:47 AM
Michael C
"Stefan Berglund" <sorry.no.kool***@for.me> wrote in message
news:dd0572h6orcn10d88r1i6647l3clbtrhbf@4ax.com...
> You must be one of those irreverent dot net freaks that think you can
> easily discredit someone who spent years building their reputation
> simply by virtue of vomiting your spew.
>
> m$' latest piece of f**k doesn't qualify you to make those allegations
> but I must admit you do provide great comic relief for those who are
> more discerning in what they read and believe.
>
> Thanks for the laughs hyena.

What on earth are you talking about. I did not mention dot net at all in
this thread. Was the link karly posted something about dot net?

I was commenting on matt curland's method of calling a function pointer in
vb6.

Michael
Author
23 May 2006 3:55 PM
Karl E. Peterson
Michael C wrote:
Show quoteHide quote
> "Karl E. Peterson" <k***@mvps.org> wrote in message
> news:uZz6xngfGHA.1520@TK2MSFTNGP03.phx.gbl...
>>> I don't see how matthew's method would be any less hacky. I can't
>>> see it though as that page requires a login.
>>
>> http://www.BugMeNot.com  <- HTH!
>
> Ok, I had a look at his code. It shoves assembly code into a currency
> variable and does pretty much the same thing but requires the extra
> complexity of creating and referencing a typelib (presumably a new
> typelib for each different call signature). It also has the reduced
> flexibility of not being able to call cdecl functions. Matt has a bad
> habit of overcomplicating things. But then again karl, don't let
> logic get in your way. :-)

You seem quite confused.  But then again, what else is new?

I was only attempting to help you view what you saw to be off-limits.

No moral or technical judgements were offered.
--
Working without a .NET?
http://classicvb.org/
Author
23 May 2006 4:05 PM
Michael C
"Karl E. Peterson" <k***@mvps.org> wrote in message
news:utU1hFofGHA.356@TK2MSFTNGP02.phx.gbl...
> You seem quite confused.  But then again, what else is new?
>
> I was only attempting to help you view what you saw to be off-limits.
>
> No moral or technical judgements were offered.

Sorry karl, I didn't look at the link. Going by the sh1t you've been posting
lately I just assumed it was another one of your insults.

Michael
Author
23 May 2006 4:15 PM
Karl E. Peterson
Michael C wrote:
> "Karl E. Peterson" <k***@mvps.org> wrote in message
> news:utU1hFofGHA.356@TK2MSFTNGP02.phx.gbl...
>> You seem quite confused.  But then again, what else is new?
>>
>> I was only attempting to help you view what you saw to be off-limits.
>>
>> No moral or technical judgements were offered.
>
> Sorry karl, I didn't look at the link. Going by the sh1t you've been
> posting lately I just assumed it was another one of your insults.

I see no need to be redundant.  You "speak for" yourself quite clearly.
--
Working without a .NET?
http://classicvb.org/
Author
23 May 2006 8:41 PM
Bob O`Bob
Michael C wrote:
> "Karl E. Peterson" <k***@mvps.org> wrote in message
> news:utU1hFofGHA.356@TK2MSFTNGP02.phx.gbl...
>> You seem quite confused.  But then again, what else is new?
>>
>> I was only attempting to help you view what you saw to be off-limits.
>>
>> No moral or technical judgements were offered.
>
> Sorry karl, I didn't look at the link. Going by the sh1t you've been posting
> lately I just assumed it was another one of your insults.



Best kettle pointer I've dereferenced all day.  Nice job, pot.
Author
24 May 2006 1:20 AM
Michael C
"Bob O`Bob" <filter***@yahoogroups.com> wrote in message news:e%
> Best kettle pointer I've dereferenced all day.  Nice job, pot.

Funny *you* should say that bob. Still think With is faster?

Michael
Author
26 May 2006 5:31 PM
bitshifter
On Wed, 24 May 2006 11:20:47 +1000, "Michael C" <nospam@nospam.com>
wrote:

>"Bob O`Bob" <filter***@yahoogroups.com> wrote in message news:e%
>> Best kettle pointer I've dereferenced all day.  Nice job, pot.
>
>Funny *you* should say that bob. Still think With is faster?
>
>Michael

He would not be alone.
Letting VB have to follow a long list of
"Thing.anotherThing.yetAnotherThing." waste time cycles that can be
replaced with one With./ end With.

Besides, it does make for clearer code, no ?
Author
27 May 2006 3:10 AM
Michael C
"bitshifter" <richm***@sympatico.ca> wrote in message
news:44773b15.17047109@news1.qc.sympatico.ca...
> He would not be alone.
> Letting VB have to follow a long list of
> "Thing.anotherThing.yetAnotherThing." waste time cycles that can be
> replaced with one With./ end With.

Here we go again :-) This is a myth. With is *not* faster. This has been
discussed at length here and many tried to prove me wrong but no one could.
Bob in particular tried very hard but once he realised I was correct went
very quiet. Try this code: http://mikesdriveway.com/misc/with.zip

> Besides, it does make for clearer code, no ?

No, it's ugly imo. In very rare cases it could be ok, eg:

With A.B.C.D.MyControl
    .Width = 100
    .Height = 100
    .Text = "ABC"
    etc
End with

but what happens when you need to add some code to calculate width, height
and text, do you delete the with block or just stick this code inside it?

Michael
Author
27 May 2006 7:17 PM
Liz
"bitshifter" <richm***@sympatico.ca> wrote in message
news:44773b15.17047109@news1.qc.sympatico.ca...

> He would not be alone.
> Letting VB have to follow a long list of
> "Thing.anotherThing.yetAnotherThing." waste time cycles that can be
> replaced with one With./ end With.

you're thinking like a typist and not like a compiler
Author
27 May 2006 8:34 PM
Bob O`Bob
Liz wrote:
> "bitshifter" <richm***@sympatico.ca> wrote in message
> news:44773b15.17047109@news1.qc.sympatico.ca...
>
>> He would not be alone.
>> Letting VB have to follow a long list of
>> "Thing.anotherThing.yetAnotherThing." waste time cycles that can be
>> replaced with one With./ end With.
>
> you're thinking like a typist and not like a compiler
>
>


You're over-thinking what a compiler can /safely/ do.



    Bob
Author
28 May 2006 3:49 AM
Liz
"Bob O`Bob" <filter***@yahoogroups.com> wrote in message
news:uTtZn0cgGHA.4864@TK2MSFTNGP05.phx.gbl...

>>> He would not be alone.
>>> Letting VB have to follow a long list of
>>> "Thing.anotherThing.yetAnotherThing." waste time cycles that can be
>>> replaced with one With./ end With.
>>
>> you're thinking like a typist and not like a compiler

> You're over-thinking what a compiler can /safely/ do.

How so, Bob?  I didn't say a thing about what a compiler can do, safely or
otherwise.  My comment was about what a compiler CAN'T do, namely, save you
clock cycles when you use a WITH ... END WITH construct.  Some may like the
convenience of saving a few keystrokes but it has no impact at all on
efficiency of the machine code emitted.

L
Author
28 May 2006 10:06 AM
Jim Mack
Liz wrote:
Show quoteHide quote
> "Bob O`Bob" wrote...
>
>>>> He would not be alone.
>>>> Letting VB have to follow a long list of
>>>> "Thing.anotherThing.yetAnotherThing." waste time cycles that can be
>>>> replaced with one With./ end With.
>>>
>>> you're thinking like a typist and not like a compiler
>
>> You're over-thinking what a compiler can /safely/ do.
>
> How so, Bob?  I didn't say a thing about what a compiler can do,
> safely or otherwise.  My comment was about what a compiler CAN'T do,
> namely, save you clock cycles when you use a WITH ... END WITH
> construct.  Some may like the convenience of saving a few keystrokes
> but it has no impact at all on efficiency of the machine code emitted.
>

Oh, dear -- not this again. There should never be threads that cross ..NET <-> .NOT boundaries because there's no telling where you're coming from.

Bob is coming from VB6, where With...End can definitely save cycles over naively accessing properties one at a time. It does this by holding a temporary reference to an object in memory. Maybe the compiler could do the work, but it doesn't. You could also create a temporary object yourself, but why bother when you have this handy construct?

In VB6,

With MyObject.Yardarm
   .Farkle = 54
   .Singed = True
   .Unhatting = Argyle
   .Rey = Tal
End With

....is measurably faster than:

  MyObject.Yardarm.Farkle = 54
  MyObject.Yardarm.Singed = True
  MyObject.Yardarm.Unhatting = Argyle
  MyObject.Yardarm.Rey = Tal

I have no idea what the situation in .NET is.

--

    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
28 May 2006 11:20 AM
Michael C
Show quote Hide quote
"Jim Mack" <jmack@mdxi.nospam.com> wrote in message
news:OGnjv5jgGHA.4276@TK2MSFTNGP03.phx.gbl...
> With MyObject.Yardarm
>  .Farkle = 54
>   .Singed = True
>   .Unhatting = Argyle
>   .Rey = Tal
> End With
>
>...is measurably faster than:
>
>  MyObject.Yardarm.Farkle = 54
>  MyObject.Yardarm.Singed = True
>  MyObject.Yardarm.Unhatting = Argyle
>  MyObject.Yardarm.Rey = Tal

But With it is measurably slower than creating an object variable.

Michael
Author
26 May 2006 5:29 PM
bitshifter
On Tue, 23 May 2006 13:41:43 -0700, Bob O`Bob
<filter***@yahoogroups.com> wrote:

Show quoteHide quote
>Michael C wrote:
>> "Karl E. Peterson" <k***@mvps.org> wrote in message
>> news:utU1hFofGHA.356@TK2MSFTNGP02.phx.gbl...
>>> You seem quite confused.  But then again, what else is new?
>>>
>>> I was only attempting to help you view what you saw to be off-limits.
>>>
>>> No moral or technical judgements were offered.
>>
>> Sorry karl, I didn't look at the link. Going by the sh1t you've been posting
>> lately I just assumed it was another one of your insults.
>
>
>
>Best kettle pointer I've dereferenced all day.  Nice job, pot.

HAHAHAHAHAHAHAhahahaha<snort> hahah ha ha ha <cough>

Do you mind if I use that as a sig ?
Author
26 May 2006 7:26 PM
Stefan Berglund
On Fri, 26 May 2006 17:29:43 GMT, richm***@sympatico.ca (bitshifter)
wrote:
in <44773ad9.16986***@news1.qc.sympatico.ca>

Show quoteHide quote
>On Tue, 23 May 2006 13:41:43 -0700, Bob O`Bob
><filter***@yahoogroups.com> wrote:
>
>>Michael C wrote:
>>> "Karl E. Peterson" <k***@mvps.org> wrote in message
>>> news:utU1hFofGHA.356@TK2MSFTNGP02.phx.gbl...
>>>> You seem quite confused.  But then again, what else is new?
>>>>
>>>> I was only attempting to help you view what you saw to be off-limits.
>>>>
>>>> No moral or technical judgements were offered.
>>>
>>> Sorry karl, I didn't look at the link. Going by the sh1t you've been posting
>>> lately I just assumed it was another one of your insults.
>>
>>
>>
>>Best kettle pointer I've dereferenced all day.  Nice job, pot.
>
>HAHAHAHAHAHAHAhahahaha<snort> hahah ha ha ha <cough>
>
>Do you mind if I use that as a sig ?
>

I thought it was really clever too.  :-)

---

Stefan Berglund
Author
26 May 2006 8:27 PM
Bob O`Bob
Stefan Berglund wrote:
Show quoteHide quote
> On Fri, 26 May 2006 17:29:43 GMT, richm***@sympatico.ca (bitshifter)
> wrote:
>  in <44773ad9.16986***@news1.qc.sympatico.ca>
>
>> On Tue, 23 May 2006 13:41:43 -0700, Bob O`Bob

>>>
>>> Best kettle pointer I've dereferenced all day.  Nice job, pot.
>> HAHAHAHAHAHAHAhahahaha<snort> hahah ha ha ha <cough>
>>
>> Do you mind if I use that as a sig ?
>>
>
> I thought it was really clever too.  :-)


Feel free.
Author
23 May 2006 8:25 AM
Anthony Jones
Show quote Hide quote
"mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
news:%23yL9SPffGHA.764@TK2MSFTNGP03.phx.gbl...
> hi Usenet,
>
> There are two pre-programmed, pre-digested solutions to this.
>
> 1.  My favorite, written by Arkadiy Olovyannikov, and called:
> "Call API functions by Name, without Declare", can be found
> on the freevbcode website:
>
>    http://www.freevbcode.com/ShowCode.Asp?ID=1863
>
> 2.  However, the api newsgroupies here may be expected to heap
> disrespect on Arkadiy's code.  They have asserted that the only
> "real" legitimate appropriate code to do this was written by
> Matthew Curland. There is an article by Curland on this subject,
> originally published in "Visual Basic Programmer's Journal",
> and now available on the web, entitled: "Call Function Pointers".
>
>    http://www.fawcette.com/archives/listissue.asp?pubID=1&MagIssueId=265#
>
> Also, if you have $40 in spare change, (and you can still find it)
> you may purchase Curland's book "Advanced Visual Basic 6", and you
> will find the same "Call Function Pointers" code in there.
>

Both of these approaches have a serious draw back.  They require that a
small piece of assembly be used to thunk the call.  Problem is that this
little piece of assembly resides in memory allocated as data.  Back when
these approaches were originally written that wasn't a problem.  Today
though a machine running with DEP turned on will kill any process trying to
do this.



Show quoteHide quote
> cheers, jw
> ____________________________________________________________
>
> You got questions?  WE GOT ANSWERS!!!  ..(but,
>     no guarantee the answers will be applicable to the questions)
>
> Usenet wrote:
> > I need to be able to call the same API function from VB, which will
> > reside in different DLLs. While all of the functions have the same
> > signature and name, DLL file names are not known at compile time.
> > Therefore, my assumption that using Declare statement or DllImport
> > attribute is NOT an option in such a case.
> >
> > I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
> > addreses, but is there a way to actually call an API method by its
> > address from VB? Or can anyone suggest anything else?
> >
> > TIA!
Author
23 May 2006 11:22 AM
Jim Mack
Anthony Jones wrote:
> "mr_unreliable" wrote:
>>
>> There are two pre-programmed, pre-digested solutions to this.
>
> Both of these approaches have a serious draw back.  They require that
> a small piece of assembly be used to thunk the call.  Problem is that
> this little piece of assembly resides in memory allocated as data.
> Back when these approaches were originally written that wasn't a
> problem.  Today though a machine running with DEP turned on will kill
> any process trying to do this.

I usually hesitate to offer only a commercial solution, but all the others have been mentioned, and as you note they have drawbacks.

While it isn't free, our Stamina library has among its almost 500 assembly-language functions, DxCall and DxCallByName. These can call arbitrary stdcall, pascal and cdecl functions having up to 16 parameters (and a return value) of any mix of variant-safe types.  It also deals with conversions between Unicode and Ansi as needed.

DxCall is best for repeated calls to the same function, or for calling VB functions (internal callbacks, for example) since it takes an address or function pointer. DxCallByName is simpler for one-off calls, since it takes a DLL name and function name as parameters and deals with LoadLibrary etc internally.

Stamina is for VB32 and VBA only, not .net

--

    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
23 May 2006 3:57 PM
mr_unreliable
Right.  But microsoft's advice in this situation is to
allocate memory with both data and execute privileges
turned on, and put your assembly in that.

Fortunately, this is not hard to do, and with that
modification Arkadiy Olovyannikov's code will work
successfully on xp -- even with dep turned on --
until microsoft comes up with some other hurdle
to jump over.

cheers, jw

Anthony Jones wrote:
Show quoteHide quote
> Both of these approaches have a serious draw back.  They require that a
> small piece of assembly be used to thunk the call.  Problem is that this
> little piece of assembly resides in memory allocated as data.  Back when
> these approaches were originally written that wasn't a problem.  Today
> though a machine running with DEP turned on will kill any process trying to
> do this.
Author
23 May 2006 4:06 PM
Michael C
"mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
news:exRaSEofGHA.3588@TK2MSFTNGP02.phx.gbl...
> Right.  But microsoft's advice in this situation is to
> allocate memory with both data and execute privileges
> turned on, and put your assembly in that.
>
> Fortunately, this is not hard to do, and with that
> modification Arkadiy Olovyannikov's code will work
> successfully on xp -- even with dep turned on --
> until microsoft comes up with some other hurdle
> to jump over.

From memory I think Matt's code handled this issue.

Michael
Author
23 May 2006 5:21 PM
Anthony Jones
Show quote Hide quote
"Michael C" <nospam@nospam.com> wrote in message
news:OFo$PKofGHA.4940@TK2MSFTNGP05.phx.gbl...
> "mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
> news:exRaSEofGHA.3588@TK2MSFTNGP02.phx.gbl...
> > Right.  But microsoft's advice in this situation is to
> > allocate memory with both data and execute privileges
> > turned on, and put your assembly in that.
> >
> > Fortunately, this is not hard to do, and with that
> > modification Arkadiy Olovyannikov's code will work
> > successfully on xp -- even with dep turned on --
> > until microsoft comes up with some other hurdle
> > to jump over.
>
> From memory I think Matt's code handled this issue.
>

It does if you use the C++ VBoost helper DLL but doesn't if you want avoid
that and use the bas module instead.  Problem with the VBoost DLL is that it
doesn't load in a COM+ environment.  I was able to circument this issue by
using some loader code.

Sadly the loader code used CallFunction techniques which lands us right back
into DEP problems anyway.
Author
23 May 2006 5:16 PM
Anthony Jones
"mr_unreliable" <kindlyReplyToNewsgr***@notmail.com> wrote in message
news:exRaSEofGHA.3588@TK2MSFTNGP02.phx.gbl...
> Right.  But microsoft's advice in this situation is to
> allocate memory with both data and execute privileges
> turned on, and put your assembly in that.
>

Where can I find this advice?
How is this done in VB6?

> Fortunately, this is not hard to do, and with that
> modification Arkadiy Olovyannikov's code will work
> successfully on xp -- even with dep turned on --
> until microsoft comes up with some other hurdle
> to jump over.
>

Any chance of showing us how this is done?


Show quoteHide quote
> cheers, jw
>
> Anthony Jones wrote:
> > Both of these approaches have a serious draw back.  They require that a
> > small piece of assembly be used to thunk the call.  Problem is that this
> > little piece of assembly resides in memory allocated as data.  Back when
> > these approaches were originally written that wasn't a problem.  Today
> > though a machine running with DEP turned on will kill any process trying
to
> > do this.
Author
23 May 2006 8:24 PM
Schmidt
"Anthony Jones" <A**@yadayadayada.com> schrieb im Newsbeitrag
news:%23Lh0TyofGHA.2068@TK2MSFTNGP02.phx.gbl...

> Where can I find this advice?

http://msdn.microsoft.com/security/productinfo/XPSP2/memoryprotection/exec_imp.aspx

> > Fortunately, this is not hard to do, and with that
> > modification Arkadiy Olovyannikov's code will work
> > successfully on xp -- even with dep turned on --
> > until microsoft comes up with some other hurdle
> > to jump over.
> Any chance of showing us how this is done?

Ca. two years ago I've posted an appropriate example:
http://tinyurl.com/ryhlk

Olaf
Author
24 May 2006 2:28 PM
Anthony Jones
"Schmidt" <s**@online.de> wrote in message
news:%23rZT0cqfGHA.4172@TK2MSFTNGP04.phx.gbl...
>
> "Anthony Jones" <A**@yadayadayada.com> schrieb im Newsbeitrag
> news:%23Lh0TyofGHA.2068@TK2MSFTNGP02.phx.gbl...
>
> > Where can I find this advice?
>
>
http://msdn.microsoft.com/security/productinfo/XPSP2/memoryprotection/exec_imp.aspx
>
> > > Fortunately, this is not hard to do, and with that
> > > modification Arkadiy Olovyannikov's code will work
> > > successfully on xp -- even with dep turned on --
> > > until microsoft comes up with some other hurdle
> > > to jump over.
> > Any chance of showing us how this is done?
>
> Ca. two years ago I've posted an appropriate example:
> http://tinyurl.com/ryhlk
>
> Olaf


Nice one :)
Author
23 May 2006 1:40 AM
Michael C
"Usenet User" <  @ .  > wrote in message
news:tau372d0grgrb2mhhdo6g00upj34b5mv32@4ax.com...
>I need to be able to call the same API function from VB, which will
> reside in different DLLs. While all of the functions have the same
> signature and name, DLL file names are not known at compile time.
> Therefore, my assumption that using Declare statement or DllImport
> attribute is NOT an option in such a case.
>
> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
> addreses, but is there a way to actually call an API method by its
> address from VB? Or can anyone suggest anything else?

If your dlls have the same name but diff dir just use LoadLibrary and
FreeLibrary with the standard vb6 declare, eg

Declare Function Whatever Lib "MyDLL" ....
Sub DoIt()
   lib = LoadLibrary("C:\abc\MyDll.dll")
   WhatEver(....)
    FreeLibrary(lib)
End Sub

because a library of name MyDLL is already loaded vb6 will just use that.
Just make sure you match the no of loadlib calls with freelib and don't call
your function without calling LoadLibrary at least once somewhere in code.

Michael
Author
23 May 2006 5:09 PM
Usenet User
Show quote Hide quote
On Tue, 23 May 2006 11:40:32 +1000, "Michael C" <nospam@nospam.com>
wrote:

>"Usenet User" <  @ .  > wrote in message
>news:tau372d0grgrb2mhhdo6g00upj34b5mv32@4ax.com...
>>I need to be able to call the same API function from VB, which will
>> reside in different DLLs. While all of the functions have the same
>> signature and name, DLL file names are not known at compile time.
>> Therefore, my assumption that using Declare statement or DllImport
>> attribute is NOT an option in such a case.
>>
>> I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
>> addreses, but is there a way to actually call an API method by its
>> address from VB? Or can anyone suggest anything else?
>
>If your dlls have the same name but diff dir just use LoadLibrary and
>FreeLibrary with the standard vb6 declare, eg
>
>Declare Function Whatever Lib "MyDLL" ....
>Sub DoIt()
>   lib = LoadLibrary("C:\abc\MyDll.dll")
>   WhatEver(....)
>    FreeLibrary(lib)
>End Sub
>
>because a library of name MyDLL is already loaded vb6 will just use that.
>Just make sure you match the no of loadlib calls with freelib and don't call
>your function without calling LoadLibrary at least once somewhere in code.

Well, the names are different, unfortunately. Also, I'm talking about
VB.NET (i.e., read - CLR), not VB6.

Thank you!
Author
23 May 2006 5:16 PM
Bob Butler
"Usenet User" <  @ .  > wrote in message
news:7ag672ljdbjkd6pl9iip3kq8gi11qjc641@4ax.com
> Well, the names are different, unfortunately. Also, I'm talking about
> VB.NET (i.e., read - CLR), not VB6.

Then you aren't talking about VB no matter how misleadingly MS has named
their product


--
try asking in a group with "dotnet" in the name; this group is for VB 6.0
and earlier
VB.Net is Not.VB
Author
25 May 2006 1:14 PM
Paul Clement
On Tue, 23 May 2006 10:16:54 -0700, "Bob Butler" <tiredofit@nospam.ever> wrote:

¤ "Usenet User" <  @ .  > wrote in message
¤ news:7ag672ljdbjkd6pl9iip3kq8gi11qjc641@4ax.com
¤ > Well, the names are different, unfortunately. Also, I'm talking about
¤ > VB.NET (i.e., read - CLR), not VB6.
¤
¤ Then you aren't talking about VB no matter how misleadingly MS has named
¤ their product

It's certainly not VB 6.0, no, despite the fact that it still uses a version of the BASIC language.


Paul
~~~~
Microsoft MVP (Visual Basic)
Author
29 May 2006 9:25 AM
david epsom dot com dot au
It is?
Show quoteHide quote
"Paul Clement" <UseAdddressAtEndofMess***@swspectrum.com> wrote in message
news:84bb721vd0uul50bp3e4ro5v364mncvohg@4ax.com...
> On Tue, 23 May 2006 10:16:54 -0700, "Bob Butler" <tiredofit@nospam.ever>
> wrote:
>
> ¤ "Usenet User" <  @ .  > wrote in message
> ¤ news:7ag672ljdbjkd6pl9iip3kq8gi11qjc641@4ax.com
> ¤ > Well, the names are different, unfortunately. Also, I'm talking about
> ¤ > VB.NET (i.e., read - CLR), not VB6.
> ¤
> ¤ Then you aren't talking about VB no matter how misleadingly MS has named
> ¤ their product
>
> It's certainly not VB 6.0, no, despite the fact that it still uses a
> version of the BASIC language.

It does? I thought it just used some of the same keywords as BASIC.

Ok, I suppose, like Java is a 'version of C'
Author
30 May 2006 3:16 PM
Paul Clement
On Mon, 29 May 2006 19:25:04 +1000, "david epsom dot com dot au" <david@epsomdotcomdotau> wrote:

¤ >
¤ > It's certainly not VB 6.0, no, despite the fact that it still uses a
¤ > version of the BASIC language.
¤
¤ It does? I thought it just used some of the same keywords as BASIC.
¤

Uses most of them from my experience.

¤ Ok, I suppose, like Java is a 'version of C'

You're asking the wrong person. ;-)


Paul
~~~~
Microsoft MVP (Visual Basic)
Author
24 May 2006 12:20 PM
Jay B. Harlow [MVP - Outlook]
Usenet User,
In addition to all the other comments, you should be able to use LoadLibrary
& GetProcAddress. The "trick" is converting the IntPtr that GetProcAddress
returns to a Delegate.

Luckily In .NET 2.0 the System.Runtime.InterpoServices.Marshal class has a
GetDelegateForFunctionPointer method that will convert the IntPtr to a
Delegate.

http://msdn2.microsoft.com/en-us/library/zdx6dyyh(vs.80).aspx

For an example of how to use the above see:
http://www.pinvoke.net/default.aspx/kernel32/GetProcAddress.html

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Usenet User" <  @ .  > wrote in message
news:tau372d0grgrb2mhhdo6g00upj34b5mv32@4ax.com...
|I need to be able to call the same API function from VB, which will
| reside in different DLLs. While all of the functions have the same
| signature and name, DLL file names are not known at compile time.
| Therefore, my assumption that using Declare statement or DllImport
| attribute is NOT an option in such a case.
|
| I can use LoadLibrary() and GetProcAddress() from Kernel32 to get the
| addreses, but is there a way to actually call an API method by its
| address from VB? Or can anyone suggest anything else?
|
| TIA!