Get the Command$ value from inside an ActiveX DLL

Get the Command$ value from inside an ActiveX DLL

At times you may need to access the command line passed to the application from within an ActiveX DLL. Unfortunately, when inside a DLL the Command$ function returns a null string, so you have to resort to some API trickery:

Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" _    () As LongPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal _    lpString1 As String, ByVal lpString2 As Long) As Long' inside the ActiveX DLL    Dim lpStr As Long, i As LongDim buffer As StringDim exePath As String, cmdLine As String    ' get a pointer to the command linelpStr = GetCommandLine()' copy into a local bufferbuffer = Space$(512)lstrcpy buffer, lpStr' extract the null-terminated stringbuffer = Left$(buffer, InStr(buffer & vbNullChar, vbNullChar) - 1)    If Left$(buffer, 1) = """" Then    ' if the string begins with double quotes,    ' find the closing quotes    i = InStr(2, buffer, """")    exePath = Mid$(buffer, 2, i - 2)    ' the rest of the line is the command line    cmdLine = LTrim$(Mid$(buffer, i + 1))Else    ' otherwise just find the space that separates    ' the EXE name/path and the command line    i = InStr(buffer, " ")    exePath = Left$(buffer, i - 1)    cmdLine = LTrim$(Mid$(buffer, i))End If

Now cmdLine holds the command line passed to the main application, and exePath is the name and path of the EXEcutable file. Note that according to MSDN exePath contains only the EXE name under NT/2000 (as opposed to the full path) but it doesn’t appear to be true (at least under NT SP4).

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes