Solved

CreateProcess cause Winsock error 10106!!!

Posted on 2001-09-18
8
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 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