Solved

CreateProcess cause Winsock error 10106!!!

Posted on 2001-09-18
8
617 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:eugeneng
Comment Utility
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
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:eugeneng
Comment Utility
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
Comment Utility
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
Comment Utility
great!!! it works!!! thanx a lot VincentLawlor, you are my lifesaver!!
0
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
You're welcome.

Vin.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
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.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now