Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Calling Win API function, CreateProcessA - how to convert String parameter to Long

Posted on 2007-10-14
7
Medium Priority
?
1,187 Views
Last Modified: 2012-06-27
Hi,

I am trying to call the Win API function, CreateProcessA, as declared by:

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
    lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    PROCESS_INFORMATION) As Long

I want to use a string (say, "strCurrentDirectory") as input for the parameter "lpCurrentDirectory" which is declared as Long.
How do I convert the string strCurrentDirectory to the Long variable, "lpCurrentDirectory".

Thanks,
Regards,
Leihg
0
Comment
Question by:LeighWardle
  • 4
  • 2
7 Comments
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 20076394
lp is Long Pointer - in this case, a pointer to the string.

When calling functions like this from VB, you can use the string variable directly - no need for any conversion.  See here for an example of using CreateProcessA: http://support.microsoft.com/kb/q129796/
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 20076401
Actually, that wouldn't work as your declaration is wrong... CreateProcessA should be declared as:

Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


lp is long pointer, but is declared here as a string.
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 20076403
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


Interesting - I took the first declaration straight from API Viewer from Visual Studio... and there is a spelling mistake in the declaration!!  Above is correct on.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:LeighWardle
ID: 20076431
Hi purplepomegranite,

Thanks for coming up with the correct declaration.

I am still stuck trying to call CreateProcess. I just need a little help to get it right.

What I have now is:

    ret& = CreateProcess(0&, strPathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, strCurrentDirectory, start, proc)

The 3rd argument is giving an error "ByRef argument type mismatch".

Regards,
Leigh


0
 
LVL 22

Accepted Solution

by:
danaseaman earned 1500 total points
ID: 20076874
See http://support.microsoft.com/kb/129797
Missing several Type declares and Variables that use these types.
Demo below will start and stop Calc.exe:

Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Declare Function CreateProcess Lib "kernel32" _
    Alias "CreateProcessA" _
    (ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    lpProcessAttributes As Any, _
    lpThreadAttributes As Any, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    lpEnvironment As Any, _
    ByVal lpCurrentDriectory As String, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function OpenProcess Lib "kernel32.dll" _
    (ByVal dwAccess As Long, _
    ByVal fInherit As Integer, _
    ByVal hObject As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" _
    (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

      Const SYNCHRONIZE = 1048576
      Const NORMAL_PRIORITY_CLASS = &H20&

Private Sub Form_Click()
    Dim pInfo As PROCESS_INFORMATION
    Dim sInfo As STARTUPINFO
    Dim sNull As String
    Dim lSuccess As Long
    Dim lRetValue As Long

    sInfo.cb = Len(sInfo)
    lSuccess = CreateProcess(sNull, _
                  "Calc.exe", _
                  ByVal 0&, _
                  ByVal 0&, _
                  1&, _
                  NORMAL_PRIORITY_CLASS, _
                  ByVal 0&, _
                  sNull, _
                  sInfo, _
                  pInfo)

    MsgBox "Calculator has been launched!"

    lRetValue = TerminateProcess(pInfo.hProcess, 0&)
    lRetValue = CloseHandle(pInfo.hThread)
    lRetValue = CloseHandle(pInfo.hProcess)

    MsgBox "Calculator has terminated!"
End Sub
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 20083667
Considering I provided both the correct declaration AND a link to a working example of using CreateProcessA, some points would have been appreciated.
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 20083686
Dear purplepomegranite,

Please accept my apologies.
I will make it up to you some other time...

Regards,
Leigh
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question