Greg Lovern
Greg Lovern

Reputation: 977

Convert Windows API call to 64-bit in Excel VBA

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?

Upvotes: 0

Views: 5247

Answers (1)

theB
theB

Reputation: 6738

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.)

Upvotes: 2

Related Questions