PDA

View Full Version : [SOLVED] Conditional Compilation Quick Question (Well, at least hopefully)



GTO
01-06-2014, 10:38 PM
Greetings All,

From the "This ain't in the help files, least as I can find" category, here's a question for those of you who actually have skills with this stuff (vs. those trying to not start the PC on fire, like yours truly here).

I am muddling through writing some code that needs to work in at least VB7 with both 32|64 bit, and preferably VB6 and VB7 (and still covering both 32|64). So... I just noticed that if I write a function two ways (#If VB7 #Else...), and I am in VB7 (Excel 2010), then I cannot put a breakpoint in the #Else section.

Am I taking it correctly (okay, I mean guessing) that even in design time, VBA simply prevents looking at any of the declarations/functions/etc that will not be in play under the current conditions? That is to ask: Let us say I was even more goofy than I think I am, and thus chose to not use Option Explicit. To properly debug the code, would I be correct that the code needs run in each environment?

Hope that makes sense, some of us are a wee bit sleep deprived at the moment...


Thank you so much,

Mark

Aflatoon
01-07-2014, 12:50 AM
Hi, Mark,

What would be the point in setting a breakpoint in code that cannot possibly run or be compiled?

GTO
01-07-2014, 04:33 AM
What would be the point in setting a breakpoint in code that cannot possibly run or be compiled?


If Question = FriendlyJab then
Answer = "Ouch! Ouch! Ouch! Okay, I suppose I deserved that one."
ElseIf Question = ProvokeThinkingOnMyEnd
Answer = LotsOfLuckWithThat
Else
Answer = SeeBelow

Hi Aflatoon,
Well yeh, I wasn't really thinking it all the way through. The minimal brain waves available came up with "Huh? I wonder if I misspell a variable, whether even that would be caught?". Thinking it through, I imagine not, as it either compiles or does not...

Okay, so myabe you provoked a little bit of thought :-)

well, I am going to "cheat" a bit and ask an "extra" question. Disregarding any missing declared procedures or missing written procedures, does this look correct? Soecifically, I copied just enough of what I had written tonight at work, to cover how I am providing conditional:

Declared procedures
Variable declaration

Written procedures


#If VBA7 Then
Public Declare PtrSafe Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As LongPtr, _
ByVal lpString As String, _
ByVal cch As Long _
) As Long
#Else
Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long _
) As Long
#End If

Sub test()
Dim lRet As Long
#If VBA7 Then
Dim hwndDesktop As LongPtr
Dim lParam As LongPtr
#Else
Dim hwndDesktop As Long
Dim lParam As Long
#End If

hwndDesktop = GetDesktopWindow()

lRet = EnumChildWindows(hwndDesktop, AddressOf EnumChildProc, lParam)

MsgBox ETermRunning("E-Term32.exe")

End Sub

#If VBA7 Then
Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
Dim lRet As Long
Dim sWinClassBuff As String * 255
Dim sWinTitleBuff As String * 255
Dim sWinClass As String
Dim sWinTitle As String

lRet = GetClassName(lpHwnd, sWinClassBuff, 255)
sWinClass = StripNulls(sWinClassBuff)
lRet = GetWindowText(lpHwnd, sWinTitleBuff, 255)
sWinTitle = StripNulls(sWinTitleBuff)

Debug.Print " Child Class = "; sWinClass; ", Title = "; sWinTitle
EnumChildProc = True

End Function
#Else
Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
Dim lRet As Long
Dim sWinClassBuff As String * 255
Dim sWinTitleBuff As String * 255
Dim sWinClass As String
Dim sWinTitle As String

lRet = GetClassName(lhWnd, WinClassBuf, 255)
sWinClass = StripNulls(sWinClassBuff)
lRet = GetWindowText(lhWnd, sWinTitleBuff, 255)
sWinTitle = StripNulls(sWinTitleBuff)
Debug.Print " Child Class = "; WinClass; ", Title = "; WinTitle
EnumChildProc = True

End Function
#End If


In close, I am unfortunately, quite the 'babe in the woods' reference API. In this particular case, I did manage to get Excel (though I used Word's DDE) to extract data from a terminal emulator. When due to a new goal, I ran some the old code, it pretty much blew up. I'm just starting to sort the causes, but appears to be related to the OS we previously used (XP 32-bit) and what is slowly replacing (WIN7 64-bit). (And frankly, I have no idea currently as to why we are loading 64-bit currently)

Of course, thank you so much for your guidance and help,

Mark

Aflatoon
01-07-2014, 05:12 AM
It looks OK to me. (I assume you know you only need this if you will be running a 64 bit version of Office? 64bit windows on its own does not require these changes)

You can simplify the last part and only make the declaration conditional, since all the rest is the same:
#If VBA7 Then
Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
#Else
Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
#End If
Dim lRet As Long
Dim sWinClassBuff As String * 255
Dim sWinTitleBuff As String * 255
Dim sWinClass As String
Dim sWinTitle As String

lRet = GetClassName(lpHwnd, sWinClassBuff, 255)
sWinClass = StripNulls(sWinClassBuff)
lRet = GetWindowText(lpHwnd, sWinTitleBuff, 255)
sWinTitle = StripNulls(sWinTitleBuff)

Debug.Print " Child Class = "; sWinClass; ", Title = "; sWinTitle
EnumChildProc = True

End Function

SamT
01-07-2014, 10:41 AM
Module Level Constant:

#Const Design32 = False|True

Conditional Code:

#If Design32 Then
Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
#ElseIf VBA7 Then
Private Function EnumChildProc(ByVal lpHwnd As LongPtr, ByVal lpParam As LongPtr) As Long
#Else
Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
#End If

GTO
01-07-2014, 11:15 PM
@Aflatoon:


It looks OK to me. (I assume you know you only need this if you will be running a 64 bit version of Office? 64bit windows on its own does not require these changes)

You can simplify the last part and only make the declaration conditional, since all the rest is the same:...


Hmmm... Well, embarrassing to admit, but to be forthright - nope, I did not realize this.

I am no doubt too oft guilty of reading only what seems necessary in order to put it to use, but of course this can lead to too much left to chance. I am also guilty of reading (think skimming) too quickly sometimes. After reading your comment (and thank you much; both for the information and the time you just saved me), I re-checked what I had read (er... glossed over). Being a family-friendly site, I cannot relay the words that were falling out of my mouth, but needless to say, when I re-checked Jan Karel Pieterse's article (http://www.jkp-ads.com/Articles/apideclarations.asp) and spotted, in large font, in bold, in all UPPERCASE, this teeny-tiny hint:

Declaring API functions in 64 bit Office

...well now, don't I feel silly. Especially given that farther down in Jan Karel's article is another tiny hint, to wit: "If your code needs to run on both 32 bit and 64 bit Excel..."

I realize that at this point, you would just have to take a leap of faith to believe I am not usually quite so "thick"... Anyways, certainly not an excuse, but I think part of my confusion has been in seeing that user32 is still referred to in either case. Finally I tried coming out of my coma and found out (http://en.wikipedia.org/wiki/Windows_User) that it's not really the same file (which IMO is just plain goofy)!

Thanks again. (BTW, I re-ran what was KABOOMing last night, and it may be timing related, but not yet sure.)

@SamT:


Module Level Constant:

#Const Design32 = False|True

Conditional Code:

#If Design32 Then


I am going to need counseling if this keeps up. I've seen examples of testing, but by heaven's not a thought in my head this week I guess. Thank you so much Sam.

Cheers to you both,

Mark

Aflatoon
01-08-2014, 01:02 AM
Mark,


I realize that at this point, you would just have to take a leap of faith to believe I am not usually quite so "thick"...

I've seen enough of your posts in various forums to recognise this as an aberration. ;)

Jan Karel Pieterse
01-08-2014, 01:49 AM
Module Level Constant:

#Const Design32 = False|True

Great, learned something new today.
I thought compile constants could only be added by adding them to the bottom box of the VBAProject properties window.

Bob Phillips
01-08-2014, 03:00 AM
That's the worst place Jan Karel, you have to set them to 1 or 0 there which I have always struggled with :)

The beauty of declaring them is they can be module specific, I have frequently used this.

GTO
01-08-2014, 03:05 AM
@Aflatoon:

I forgot to add: Could we all swear secrecy and forget this thread?

Seriously, thanks. In closing to "the thread I wish I could delete" (just kidding), if you or Sam or Jan Karel (or for that matter, where's Bob?) were to suggest a book reference API and/or (in laymen's terms) "This is what's going on under-the-hood" (or possibly bonnet?), what would it be?

If nothing strikes to suggest, no problem; just thought to ask. The one-and-only book I have purchased so far is an EBay deal, 'Win32 API Programming with Visual Basic', publisher: O'Reilly, author: Steven Roman, circa: 2000. Whether it is/was one of the better ones or not, I'd probably get a lot more out of the purchase if I actually read/studied it (referenced aforementioned lacking).

Thank you again brother.

@Jan Karel Pieterse:

While I obviously would better benefit if I slowed down a bit and actually studied it, I wanted to thank you for the efforts you expend in sharing knowledge. I don't think we've exchanged directly but a time or two, but it occasionally strikes me as to the efforts good folks put in, in sharing a "thing" they are fond of. Thank you so very much. :beerchug:

Mark Wayne Stump

GTO
01-08-2014, 03:16 AM
ACK. Reference #9, I obviously stepped away too many times in typing #10. No fair being Ninja-like!

Bob Phillips
01-08-2014, 03:43 AM
If nothing strikes to suggest, no problem; just thought to ask. The one-and-only book I have purchased so far is an EBay deal, 'Win32 API Programming with Visual Basic', publisher: O'Reilly, author: Steven Roman, circa: 2000. Whether it is/was one of the better ones or not, I'd probably get a lot more out of the purchase if I actually read/studied it (referenced aforementioned lacking).

The only API book I have is 'Visual Basic Programmer's Guide to the Win32 API' by Dan Appleman, which is a brick, and if I am honest, very boring. I don't know the Steve Roman book, but he is a good author, so I would assume it is alright, and I bet it is a far better read than mine :). But I thought this thread was not an API thread, more one on programming practices and techniques. I remember bringing up conditional compilation in a thread in MrExcel some years ago, it was called 'tricks of gurus' in the Lounge. The CC chat had a little mileage, but quickly degenerated into a discussion about alcohol between Richard Schollar and myself:drunkard:


While I obviously would better benefit if I slowed down a bit and actually studied it, I wanted to thank you for the efforts you expend in sharing knowledge. I don't think we've exchanged directly but a time or two, but it occasionally strikes me as to the efforts good folks put in, in sharing a "thing" they are fond of. Thank you so very much. :beerchug:

There are many ways to absorb knowledge. I would have said that discussing within a specialised forum is one of the best. Don't beat yourself up for being smart and raising the topic.:clever:

GTO
01-08-2014, 05:06 AM
Hi Bob:hi:,

A better reply tomorrow (it's 0503 Hrs, I should have hit the rack NLT 0330 - I am beat!), but I found it! http://www.mrexcel.com/forum/lounge-v-2-0/153357-tricks-gurus.html

Good info, which I will re-read, as tired/giddy, I started laughing too much. Not a bad way to end the day though :-)

Til "tomorrow",

Mark

Jan Karel Pieterse
01-08-2014, 05:54 AM
GTO: You're welcome.
In the end all my posts and publications are marketing (which is just an excuse to hide the fact that I just love to share stuff and help people master stuff in Excel).

I like the API viewer, it has many API functions and as a bonus contains lost of (VB6) examples which translate into VBA relatively easy.

And then there is the one great big programming resource: Google.

Steve bullen has a very nice example file on his website to have fun with Userforms using API's. www.oaltd.co.uk (http://www.oaltd.co.uk)

Jan Karel Pieterse
01-08-2014, 05:57 AM
xld: but of you declare them in a module, they're local to the module, so if you need a "global" compile constant, the project properties is the only way, right?

Aflatoon
01-08-2014, 08:34 AM
@Mark,

I have the same book as Bob - though I find it quite interesting. ;) It's probably not something you'd want to sit down and read in one sitting, but it's about as good a coverage of the subject as I've seen, for those of us that don't speak C. I'll check out the one you mention though - always nice to have something new to read.

@Bob,

In my experience, Mr Schollar isn't much cop at drinking, so I wouldn't really listen to anything he has to say on the subject. :)

Bob Phillips
01-08-2014, 09:25 AM
@Bob,

In my experience, Mr Schollar isn't much cop at drinking, so I wouldn't really listen to anything he has to say on the subject. :)

As I said, real drinkers consider me a woos when it comes to drinking. And from what I hear, none of us are much cop compared to yourself :).

Aflatoon
01-08-2014, 09:34 AM
Oh, I don't know about that. Andy can certainly more than hold his own on that front! :)