eugeneng
asked on
CreateProcess cause Winsock error 10106!!!
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.
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
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.
ASKER
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 ?
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.
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
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.
ASKER
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 ?
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 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great!!! it works!!! thanx a lot VincentLawlor, you are my lifesaver!!
You're welcome.
Vin.
Vin.
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