Help with understanding Declare Function & Windows Messages/Controls

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
351
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have been studying some of the code snippets posted to the forums. I have a very broad question to ask, so forgive me if I ramble.

I am trying to understand how VBA works with Windows Controls, Messages, etc. I don't have anything specific in mind that I'm trying to do, but I want to learn it for future uses. This is from something @Jaafar Tribak posted that is meant to allow mouse scrolling in Combo Boxes.



Code:
 Private Type POINTAPI    x As Long
    y As Long
End Type


Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

I understand the purpose of declaring these, but it is difficult to find them in the documentation since it is not being updated https://docs.microsoft.com/en-us/previous-versions//dd162897(v=vs.85) (RECT struct). I am trying to determine what other structures are available.

Code:
 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  Win64 Then
        Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal Point As LongPtr) As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If




Code:
   Private Declare PtrSafe Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hWnd As LongPtr, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long    Private Declare PtrSafe Function WaitMessage Lib "user32" () As Long
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
    Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
    Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hWnd As LongPtr, lpRect As RECT) As Long
    Private Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long




1) What do I need to understand about the user32.dll & kernel32.dll other than that they are the libraries associated with these functions?
2) Why are they sometimes called macros in the docs instead of functions? Is there any difference I should be concerned about?
3) How do I determine what type is appropriate in the declaration? For example, in the WindowFromPoint function the return type is HWND, but in VBA we use LongPtr - why? In others, BOOL is return type, but in VBA it is Long.
4) Some return types are Long, others are LongPtr, but I thought they were always supposed to be LongPtr in 64-bit?
5) Is Any the same as Variant?
6) Why do some functions have A or W suffix, and what's the difference?

I do plan on learning C++ a bit more than I currently know, so hopefully that will help me understand the intuitive approach to these problems.
I have more questions than this, but I don't want to go overboard.:rofl: Thanks for any help!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi gravanoc,

These are all excellent questions! Some of them I don't have the answer to either, but I can at least impart my understanding. If anyone sees something wrong with what I say, PLEASE correct me! This is all stuff I've learned on my own and have inferred from my understanding of other languages.


I understand the purpose of declaring these, but it is difficult to find them in the documentation since it is not being updated https://docs.microsoft.com/en-us/pre...62897(v=vs.85) (RECT struct). I am trying to determine what other structures are available.

Almost everything you need to know about the Windows API is here in the API Index. It documents tons of functions and has crazy amounts of documentation. In regard to the STRUCTs, they are documented in here too.
https://docs.microsoft.com/en-us/windows/desktop/apiindex/windows-api-list


Code: #If VBA7 Then #If Win64 Then
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal Point As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPtr
#End If

This is code that sees if you are running x64 Excel or x86 Excel and then defines the function WindowFromPoint() accordingly. If you are using x64 then you need to declare functions slightly differently so that they do not error or behave erroneously.

"The problem with running legacy VBA code in 64-bit Office is that trying to load 64-bits into a 32-bit data type truncates the 64-bit quantity. This can result in memory overruns, unexpected results in your code, and possible application failure."
- https://docs.microsoft.com/en-us/of...64-bit-visual-basic-for-applications-overview


1) What do I need to understand about the user32.dll & kernel32.dll other than that they are the libraries associated with these functions?

These are libraries that are included with every version of Microsoft Windows. That means that if you use them, they will be more compatible (ideally) than if you did something with your own code. You can find almost all of the necessary info about functions, constants, and other fun API stuff here:
http://pinvoke.net/

Unfortunately, it's not meant for VBA, but I've found it very helpful overall.

2) Why are they sometimes called macros in the docs instead of functions? Is there any difference I should be concerned about?

It is my understanding that Functions are bits of code you are asking to complete an action and return a value, while Macros are basically a System variable that contains and returns information. So you might use a Macro to get a value about the system you are running on and then pass it to a Function that actually does something with it. I could be wrong here, but that is my understanding.


3) How do I determine what type is appropriate in the declaration? For example, in the WindowFromPoint function the return type is HWND, but in VBA we use LongPtr - why? In others, BOOL is return type, but in VBA it is Long.

It all depends on what the Windows API function is expecting. You can find that information in the WinAPI documentation I pasted earlier. Here is a list of all the Windows data types.
https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types

The HWND is actually just a STRUCT for an intPtr that we can contain in a long. That intPtr is just a handle for the window. Here are some explanations of Windows Handles if you need it.
https://stackoverflow.com/questions/902967/what-is-a-windows-handle

Basically everything in Windows is a window (ha that's why its called Windows). Text boxes are windows, List boxes are windows, buttons are windows, each program has dozens of windows open at a time. If you can find the HWND or handle to that window, you can then communicate directly with it via Windows Messages.


4) Some return types are Long, others are LongPtr, but I thought they were always supposed to be LongPtr in 64-bit?

Here is an explanation that does it better than I can I think.
https://stackoverflow.com/questions...variables-work-in-64-bit-and-32-bit-excel-vba


5) Is Any the same as Variant?

I don't think it is, but honestly I have no idea. I couldn't find ANYthing about any.(heh)


6) Why do some functions have A or W suffix, and what's the difference?

According to this, it indicates whether is is ANSI or UNICODE encoding for their inputs and outputs.
https://stackoverflow.com/questions...etween-the-a-and-w-functions-in-the-win32-api


Well, that certainly was a long post! If anyone else has anymore information or knows a bit more than me (maybe I got something wrong) PLEASE CORRECT ME! We're all still learning here!
 
Upvote 0
Thanks for answering @BlueAure! I've spent the last couple of days reading through the documentation & also the links you provided. I don't know if I go about learning the right way, but I usually read through all the functions and such even though it is tedious & I probably forget most of it without any immediate need of using them. Right now I am almost done with the functions in Winuser.h https://docs.microsoft.com/en-us/windows/desktop/api/winuser/. It's quite a slog.

I've also found it useful to take the existing code I've found and manipulate it a bit so that I can see how it is displayed in the VBE Locals Window. Also, I'm looking for a copy of this thing called Control Spy 2.0 that has a dead link in Windows docs. It sounds like it would be tremendously useful. https://docs.microsoft.com/en-us/windows/desktop/Controls/control-spy

Any idea what is meant when it talks about high-order & low-order words? It comes up pretty often.
 
Upvote 0
No problem! I love it when I can help someone learn something new. I always say that the hardest part of learning is learning what you don't know you don't know. If you know something exists you can usually learn it, but if you don't know it exists it becomes a lot harder to learn it. I usually keep reference materials bookmarked and focus not on the specifics of what is there but instead focus on what kinds of things exist. It helps me to remember that there are some functions that deal with windows and then later search for those when I need them that to remember all of those functions the first time.

I found a link to Control Spy here: (the download file is MsControl.zip, but instead of an I in zip its an accented I so you have to rename it)
Never used it but it looks useful.
http://masm32.com/board/index.php?topic=6908.0

I believe that this will answer your question about high v. low order words.
https://social.msdn.microsoft.com/F...oworder-versus-highorder-word?forum=vcgeneral

Glad I could help! I'll be happy to answer any more questions!
 
Upvote 0
Thanks again! Btw, you should try it out, it's super useful if you're trying to learn the way Windows Messages works. I've been messing around with it for about an hour now.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,091
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top