What went wrong with my Declare statement?
I've recently fixed a bug in a VB6 application, but I'm not sure, what exactly went wrong.
The offending part was a wrong API declaration of CreateEvent
. This is, what API Viewer generated:
Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA"
(lpEventAttributes As SECURITY_ATTRIBUTES, ...) As Long
The next one is the wrong declare, obviously someone didn't want to import the SECURITY_ATTRIBUTES
structure...
Declare Function CreateEvent Lib "kernel32" Alias "CreateEvent开发者_StackOverflow中文版A"
(lpEventAttributes As Any, ...) As Long
The call was:
Event = CreateEvent(Nothing, 0, 0, "MyEventName")
This call worked always in the IDE, but never from the compiled .exe. (CreateEvent
always returned 0)
I changed the declaration to:
Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA"
(ByVal lpEventAttributes As Any, ...) As Long
... and it worked.
Now I'm a little bit puzzled:
- Why is the parameter
ByRef
when usingSECURITY_ATTRIBUTES
but must beByVal
when usingAny
? - Why did the wrong declare always work in the IDE?
If you use an unqualified As Any parameter, you have to be explicit in the Call. This should have fixed the problem:
Event = CreateEvent(ByVal 0&, 0, 0, "MyEventName")
I can't see why you'd use Nothing
here, since that's an Object reference and the call is expecting a pointer. What ByVal 0&
does is pass a null pointer -- since it's null it doesn't matter what it's (not) pointing to. But passing Nothing ByVal
probably forces ByVal 0&
, which is why it worked.
As to why it worked in the IDE, well, the IDE does tend to be more forgiving about things like this.
精彩评论