Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CreateProcess cause Winsock error 10106!!!

Posted on 2001-09-18
8
Medium Priority
?
644 Views
Last Modified: 2013-11-13
On my NT machine, I've the following vb code which will call winapi "CreateProcess" to run another program, say ProgramA. ProgramA is vb program that uses a com object to create a socket(tcp/ip) connection to a server.

The problem is this indirect process causes the socket() , c++ function, return socket error:10106. When I run the ProgramA alone by double click on it, then the socket can be created just fine.

I think I've provided some incorrect arguement for the "CreateProcess" api which caused the socket(..) function (in programA) return error:10106.

here are the function which will call "CreateProcess"

Public Function ExecShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean

    Dim pclass As Long
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    'Not used, but needed
    Dim sec1 As SECURITY_ATTRIBUTES
    Dim sec2 As SECURITY_ATTRIBUTES

    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)

    sinfo.dwFlags = STARTF_USESHOWWINDOW

    sinfo.wShowWindow = start_size

    pclass = Priority_Class
    'Start the program
    If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
    0&, WorkDir, sinfo, pinfo) Then
        'Wait
        WaitForSingleObject pinfo.hProcess, dwMilliseconds
        ExecShell = True
    Else
        ExecShell = False
    End If
End Function

some where is the main vb program, I call the function
        :
ExecShell "ProgramA.exe", "C:\", 0, SW_NORMAL, NORMAL_PRIORITY_CLASS
        :

I doubt it might because I've incorrectly set the Security_Attributes of the "CreateProcess" api , these arguements are ignore on win95/98/me, but not on WinNT.
0
Comment
Question by:eugeneng
8 Comments
 
LVL 5

Expert Comment

by:AndrewDev
ID: 6490362
I found a referenc eto the following win32 error at MSDN. It may be irrelevant but might give you a clue

10106 The requested service provider could not be loaded or initialized.  WSAEPROVIDERFAILEDINIT

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win32/hh/psdkref/errlist_9usz.asp

Hope it helps
Andrew

0
 
LVL 15

Expert Comment

by:lyonst
ID: 6490369
0
 

Author Comment

by:eugeneng
ID: 6490374
I found that too, the I would like to know the cause of this error, is it the thread problem ? or the security attribute problem ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6492066
I don't think you are calling CreateProcess properly.

Usually this is called as follows:


   Dim pclass As Long
   Dim sinfo As STARTUPINFO
   Dim pinfo As PROCESS_INFORMATION
   
   'Don't pass in the security attributes
   'Not used, but needed
   'Dim sec1 As SECURITY_ATTRIBUTES
   'Dim sec2 As SECURITY_ATTRIBUTES

   'sec1.nLength = Len(sec1)
   'sec2.nLength = Len(sec2)
   
   sinfo.cb = Len(sinfo)

   sinfo.dwFlags = STARTF_USESHOWWINDOW

   sinfo.wShowWindow = start_size

   pclass = Priority_Class
   'Start the program
   If CreateProcess(vbNullString, App, 0&, 0&, 1&, pclass, _
   0&, WorkDir, sinfo, pinfo) Then
       'Wait
       WaitForSingleObject pinfo.hProcess, dwMilliseconds
       ExecShell = True
   Else
       ExecShell = False
   End If

The reason your call is failing is because the process created does not inherit handles including access privileges from the calling process. So you need to set the InheritHandles flag to TRUE as in the modified code.

Vin.
0
 

Author Comment

by:eugeneng
ID: 6492190
hi VincentLawlor, I hope your method will solve my problem, but first of all, I can't get the code to compile, i.e, I get compile error when I try to make the exe, the compiler complaint that : the 3rd&4th arguement (0&) "ByRef arguement type mismatch", how to pass null in vb ?

 here is my CreateProcess api declaration in vb

Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpAppName As String, ByVal commandLine 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

can you show me how to pass null to this api properly ?
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 200 total points
ID: 6492809
Try this example.

Note the Security Attributes are declared as Any and when CreateProcess is called they values for these are passed in by value.

     Option Explicit

     Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId 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 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&

Useage:

Private Sub StartCalc()

    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

This should sort the problem out for you.

Vin.
0
 

Author Comment

by:eugeneng
ID: 6492992
great!!! it works!!! thanx a lot VincentLawlor, you are my lifesaver!!
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6494934
You're welcome.

Vin.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

885 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