Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

vb6 - Is there a CallByName equivalent for global functions?

I want to do this: "If there's a module X containing a function Y then call it, otherwise don't."

I'm aware that I can use CallByName(Object, MethodName, ...) to call a method or property of an object instance.

Is it possible to call a global sub/function which is not bound to an object?

//Module1
Public Sub DoSomething
End Sub

//Module2
Public Sub TriggerDoSomething
  On Error Resume Next
  CallByName2 "Module1", "DoSomething", ...
End Sub

I know, it would be better to refactor my code to wrap DoSomething into a class, but currently this is not possible as it would break the code of my coworkers in a way that could not be fixed in a few hours.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It really would be easier to CallByName a class - can't you just wrap the module(s) with a class that redirects the calls to the module?

It's possible to call routines in modules by name using a FunctionDelegator. This is explained in Matt Curland's excellent book Advanced Visual Basic 6.

Googling might find you some hacks using CallWindowProc, but Matt Curland says that's dangerous. Which is pretty much the final word on the matter :)


EDIT: RS Conley's answer doesn't use a module, use a class that has a instancing property of GlobalMultiUse and you will be able to use CallByName. This will work if your code is in a DLL rather than an EXE. RS Conley suggests in any case it is more flexible to have a minimal EXE with nearly all functionality in a DLL: this may well be true.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...