0

I'm using this Windows API call in 32-bit Excel, and it's working fine:

Declare Function WNetGetConnection Lib "MPR.DLL" _
    Alias "WNetGetConnectionA" ( _
        ByVal lpszLocalName As String, _
        ByVal lpszRemoteName As String, _
        lSize As Long) As Long

I'm told that a small percentage of our corporate users will soon be going to 64-bit Excel. I don't have access to 64-bit Excel and I haven't been able to find any coworkers nearby who do.

According to this file, if I'm reading it correctly...

http://www.cadsharp.com/docs/Win32API_PtrSafe.txt

...this is the correct syntax of that function for 64-bit Excel:

Declare PtrSafe Function WNetGetConnection Lib "MPR.DLL" _
    Alias "WNetGetConnectionA" ( _
        ByVal lpszLocalName As String, _
        ByVal lpszRemoteName As String, _
        lSize As Long) As Long

But is that correct? The only difference is adding PtrSafe. Should it point to a different, 64-bit version of MPR.DLL? Also, should lSize be LongLong rather than Long?

Greg Lovern
  • 958
  • 4
  • 18
  • 36
  • 1
    Please don't ask us to do your testing for you. Install 64 bit Excel yourself. – David Heffernan Oct 07 '15 at 23:24
  • "Testing" is not a reliable method of determining API declarations in Excel. Firstly, Excel does stack correction after API calls: that hides declaration errors: in this case it would hide an error in the bit length of the return value. Secondly, testing the API requires a known valid testing framework, which is almost never the case. And thirdly - some of the API has optional terms which are almost never used, leading to hidden errors. – david Nov 03 '16 at 03:50

1 Answers1

2

But is that correct?

Yes. But don't take my word for it. Get a copy of Office and test it.

The only difference is adding PtrSafe. Should it point to a different, 64-bit version of MPR.DLL?

No. Windows chooses the appropriate DLL for the bitness of your process. For standard WinAPI components, you should only use the DLL name, and never a full path. Windows will pick the correct one.

Also, should lSize be LongLong rather than Long?

It should be ByRef Long. The WNetGetConnection documentation shows the function prototype:

DWORD WNetGetConnection(
  _In_    LPCTSTR lpLocalName,
  _Out_   LPTSTR  lpRemoteName,
  _Inout_ LPDWORD lpnLength
);

DWORD is a 32 bit unsigned integer.1 LPDWORD is a pointer to a DWORD, which in VB(6/A) is ByRef lpnLength As Long.

As a bonus:

  • In the Windows API LONG is also 32 bit. This does not change whether you're running 32 or 64 bit. (sizeof(DWORD) == sizeof(LONG) == sizeof(INT))
  • VBA Data Types

1Fun Fact: VBA doesn't have unsigned integer types, since VB6 didn't have them. VB6 came out in '98.

A brand-new never-before-used single user license for Office 2016 is running about $150 as of 7-Oct-15. It's also available through the MSDN subscription programs. It's also available at many fine retailers. (I'm intentionally not posting links.)

theB
  • 6,450
  • 1
  • 28
  • 38