-2

I create a short snippet to measure elapsed time between two defined points during a VBA macro. But to get the time in SYSTEMTIME structure (must use it) format i can't define, because an Bad DLL calling convention (Error 49) error is thrown. I know that SYSTEMTIME structure is 8 Integer numbers which are passed through the function.

The declaration of the function

Private Declare Function WinHttpTimeToSystemTime Lib "winhttp.dll" (Byval time as String, several attempts)

These attempts:

ByVal systime as String
ByVal systime as Integer
ByVal systime as Long (only for sure)
ByRef systime() as Integer (VBA let only ByRef access)
ByRef systime as userdef (user-defined type with 8 integer variables)

All resulted the above error.

Is there a solution?

Black cat
  • 1,056
  • 1
  • 2
  • 11
  • `Is there a solution?` - yes, to declare the actual `SYSTEMTIME` structure. You [cannot](https://stackoverflow.com/a/11442361/11683) pass `Byval time as String` either because the function expects `LPCWSTR`, not `LPCSTR`. – GSerg Apr 24 '23 at 09:30
  • 1
    [Why](https://meta.stackexchange.com/q/66377/147640) are you using `WinHttpTimeToSystemTime` in the first place? What is wrong with the datetime functions VBA already has? – GSerg Apr 24 '23 at 09:37
  • @gserg In Windows docs. [in] pwszTime Pointer to a null-terminated date/time string to convert. This is just a String. If you would test the declaration no error is on that part of the declaration. The last line is a user-defined type which also generates an error. In VBA there is no! SYSTEMTIME type natively. – Black cat Apr 24 '23 at 10:24
  • It is not a "pointer to null terminated string". It is a "pointer to double null terminated wide string", which is very different. You would not get an "error" on a wrong declaration. There is no mechanism to detect that error. You will realise that's an error when you finally call the function and it returns you 0 and sets last error to something like `ERROR_INVALID_PARAMETER`. `there is no! SYSTEMTIME type natively` - what stops you from [declaring it](https://stackoverflow.com/a/14767680/11683)? – GSerg Apr 24 '23 at 10:43
  • @gserg LPCWSTR stands for "Long Pointer to Constant Wide String" – Black cat Apr 24 '23 at 11:33
  • Yes. And this is not what VBA will send if you declare the argument as string. – GSerg Apr 24 '23 at 11:48
  • 1
    You may want to check out [this answer](https://stackoverflow.com/a/68406681/4717755) with a link to this [VBA Benchmark class](https://github.com/jonadv/VBA-Benchmark) that cleanly provides high resolution system clocks for timing your code execution. – PeterT Apr 24 '23 at 12:20

1 Answers1

0

Based on the comments I recheck the declaration and the problem was that the return value of the function was missing. Added As Boolean and the function was executed.

Black cat
  • 1,056
  • 1
  • 2
  • 11