Solved

CreateProcess cause Winsock error 10106!!!

Posted on 2001-09-18
8
626 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 50 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 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