I find the accepted answer to be rather misleading, so I was compelled to post another one.
A good answer will explain benefits (beyond my example above) and risks of using the StrPtr
function, possibly how you use (or don't use) it without giving an opinion as to whether everyone or no one should use it.
There are three "hidden" functions: VarPtr
, StrPtr
and ObjPtr
.
VarPtr
is used when you need to get the address of a variable (that is, the pointer to the variable).
StrPtr
is used when you need to get the address of the text data of a string (that is, the BSTR, a pointer to the first Unicode character of the string).
ObjPtr
is used when you need to get the address of an object (that is, the pointer to the object).
They are hidden because it may be unsafe to mess around with pointers.
But you cannot go completely without them.
So, when do you use them?
You use them when you need to do what they do.
You use VarPtr
when your problem in hand is "I need to know the address of that variable" (e.g. because you want to pass that address to CopyMemory
).
You use StrPtr
when your problem in hand is "I need to know the address of the first character of my BSTR string" (e.g. because you want to pass it to an API function that accepts wide strings only, but if you simply declare the parameter As String
, VB will convert the string into ANSI for you, so you have to pass StrPtr
).
You use ObjPtr
when your problem in hand is "I need to know the address of that object" (e.g. because you want to examine its vtable or manually check if the object address does or does not equal some value you knew previously).
These functions correctly do what they are supposed to do, and you should not be afraid to use them for their intended purpose.
If your task in hand is different, you probably should not be using them, but not out of fear that they will return a wrong value - they will not.
In a perfect world, you would stop at that conclusion. That is not always possible, unfortunately, and the InputBox
situation you mention is one of the examples.
From what is outlined above, it would appear that you should not be using StrPtr
to determine if Cancel was pressed in an InputBox
. Realistically though, you don't have a choice.
VBA.InputBox
returns a String
. (This fact is incorrectly omitted from the current documentation making it look like it returns a Variant
.) It is perfectly okay to pass a string to StrPtr
.
However, it is not documented that InputBox
returns a null pointer on a cancel. It is merely an observation. Even though realistically that behaviour will never change, theoretically it may in a future version of Office. But that observation is all you have; there is no documented return value for a cancel.
With this in mind, you make a decision on whether or not you are comfortable with using StrPtr
on the InputBox
result. If you are happy to take the very small risk of this behaviour changing in future and your app therefore breaking, you do use StrPtr
, otherwise you switch to Application.InputBox
that returns a Variant
and is documented to return a False
on a cancel.
But that decision will not be based on whether StrPtr
is correct in what it tells you. It is. It is always safe to pass the String
result of VBA.InputBox
to it.
Fantastic! But then why do some insist that StrPtr
never be used? I read it's unsupported. Why does that matter?
When someone insists that something should never be used, it's almost always wrong. Even GoTo
has its correct uses.