7

Using my own program or others I can't get winsock to run when calling if the process is created with CreateProcessWithLogonW or CreateProcessAsUserW. It returns this error when I create the socket:

WSAEPROVIDERFAILEDINIT 10106

Service provider failed to initialize.

The requested service provider could not be loaded or initialized.

This error is returned if either a service provider's DLL could not be loaded (LoadLibrary failed) or the provider's WSPStartup or NSPStartup function failed

.

However, WSAStartup seems to go w/o an error. Just creating the socket with WSASocket returns this.

UPDATE:

Errors:

LoadUserProfile: Error Code 2. Can't find file specified

AdjustTokenPrivs: Error Code 5. Access Denied

MD XF
  • 7,860
  • 7
  • 40
  • 71
Christopher Tarquini
  • 11,176
  • 16
  • 55
  • 73

6 Answers6

9

I encountered exactly the same problem and it was due to the environment (!): Apparently WinSock expects a valid SystemRoot environment variable to be set. In my case I was calling CreateProcess() by specifying only one environment variable specific to my app (without inheriting the caller's environment), and it was failing.

Check that you created your process by either passing NULL to lpEnvironment to inherit the caller's environment, or specify a valid SystemRoot environment variable.

Completely undocumented AFAIK, but it worked for me.

Nico
  • 811
  • 10
  • 18
  • Not much has changed, this was a fun one in a test: func TestOne(t *testing.T) { os.Clearenv() _ = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) } – Kyle Wiering Dec 21 '22 at 18:49
3

Perhaps the user you executed the process with isn't allowed to use the TCP/IP stack?

Try to start the application with an administrator user that is not your own account.

Yannick Motton
  • 34,761
  • 4
  • 39
  • 55
  • My account is a limited account that is creating the process that uses winsock as an admin. Both can use the TCP/IP stack. I even tried from one admin to another and it fails. – Christopher Tarquini Oct 12 '09 at 16:16
3

You have to have the Act As Operating Priv

Christopher Tarquini
  • 11,176
  • 16
  • 55
  • 73
1

May be you lack the required privileges to run the process as another user. Try getting the handle to the access token by a call to OpenProcessToken and add SE_IMPERSONATE_NAME by a call to AdjustTokenPrivileges and then call CreateProcessAsUserW. I have not tried this myself though.
Code snippet in Python to do something similar with win32 calls

deepsnore
  • 976
  • 5
  • 13
  • Even when I run the process as an admin AdjustTokenPrivs return ERROR_ACCESS_DENIED. – Christopher Tarquini Oct 15 '09 at 15:51
  • Hello @0xdky. At begin I also get the `WSAEPROVIDERFAILEDINIT 10106` when call `socket()`. And after I did what you said, I get `WSASYSCALLFAILURE 10107` when call `WSAStartup()`, which doesn't return error at begin. Do you have any idea? Thank you. – 50u1w4y Aug 10 '21 at 09:03
0

Run Process Monitor on it and see if it is failing to find a file or registry key. Perhaps the impersonated user's profile is not loaded and Winsock (or a service provider it is trying to load) is looking for something there.

Luke
  • 11,211
  • 2
  • 27
  • 38
0

Always start WinSock near the top of main and leave it running. The need to start winsock is an accident of architecture and not germain to any problem domain anymore.

Joshua
  • 40,822
  • 8
  • 72
  • 132