lstrlenW() in 64-bit Excel

S.Br.

Board Regular
Joined
Oct 5, 2012
Messages
91
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Dear All;
When I run the following code in 32-bit Excel (Windows 10):
VBA Code:
Option Explicit
Option Base 0

Declare PtrSafe Function GetCommandLine Lib "Kernel32" Alias "GetCommandLineW" () As Long
Declare PtrSafe Function lstrlenW Lib "Kernel32" (ByVal lstrptr As Long) As Long
Declare PtrSafe Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal l As Long)

Sub Button1_Click()
  Dim lCmdLineAddr As Long
  Dim lCmdLineLen As Long 'length of command line
  Dim sCmdLineStr As String
  Dim buff() As Byte
  On Error GoTo lblErr
  lCmdLineAddr = GetCommandLine
  lCmdLineLen = lstrlenW(lCmdLineAddr) * 2
  ReDim buff(0 To (lCmdLineLen - 1)) As Byte
  CopyMemory buff(0), ByVal lCmdLineAddr, lCmdLineLen
  sCmdLineStr = buff
  MsgBox "[" & sCmdLineStr & "]"
  Exit Sub
lblErr:
  MsgBox Err.Number & vbCrLf & Err.Description
End Sub
everything looks as expected, but in 64-bit Excel lstrlenW() call always returns 0.
What should I change to have this code '64-bit Excel' compatible?
Thanks in advance. S.Br.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi

Applies to both 32/64 versions

VBA Code:
Option Explicit
Option Base 0

#If VBA7 Then
    Declare PtrSafe Function GetCommandLine Lib "Kernel32" Alias "GetCommandLineW" () As LongPtr
    Declare PtrSafe Function lstrlenW Lib "Kernel32" (ByVal lstrptr As LongPtr) As Long
    Declare PtrSafe Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal l As Long)
#Else
    Declare Function GetCommandLine Lib "Kernel32" Alias "GetCommandLineW" () As Long
    Declare Function lstrlenW Lib "Kernel32" (ByVal lstrptr As Long) As Long
    Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal l As Long)
#End If




Sub Button1_Click()
  #If VBA7 Then
    Dim lCmdLineAddr As LongPtr
  #Else
    Dim lCmdLineAddr As Long
  #End If
  Dim lCmdLineLen As Long 'length of command line
  Dim sCmdLineStr As String
  Dim buff() As Byte
  On Error GoTo lblErr
  lCmdLineAddr = GetCommandLine
  lCmdLineLen = lstrlenW(lCmdLineAddr) * 2
  ReDim buff(0 To (lCmdLineLen - 1)) As Byte
  CopyMemory buff(0), ByVal lCmdLineAddr, lCmdLineLen
  sCmdLineStr = buff
  MsgBox "[" & sCmdLineStr & "]"
  Exit Sub
lblErr:
  MsgBox Err.Number & vbCrLf & Err.Description
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top