Calling DLL Functions: Important Issues
After you declare a procedure, you can call it just like any VBA function. The main issue is that you must ensure that you are passing correct values to the DLL. Otherwise, the bad call can cause your application to shut down without warning. In fact, external library calls are very tricky. You therefore should always save your work before you test the calls.Most DLLs expect to receive standard C strings. These strings are terminated with a Null character. If a DLL expects a Null-terminated string, you must pass the string by value. The ByVal keyword tells VBA to pass the string as Null-terminated.Although you must pass strings by value, they actually are received by reference. The ByVal keyword simply means that the string is Null-terminated. The DLL procedure actually can modify the value of the string, which can cause problems. As discussed in the "Passing String Parameters" section earlier in this chapter, if you do not pre-allocate space for the procedure to use, it overwrites any memory it can find, including memory currently being used by your application, another application, or even the operating system. You can avoid this problem by making the string argument long enough to accept the longest entry that you think will be placed into the parameter.