简体   繁体   中英

How to make Ws2_32.lib work with VS 2013 Project targeted at Windows 8.1

So I have a Visual Studio 2013 Update 4 solution with 2 projects inside, one target Windows 8.1, other Windows Phone 8.1 configured to build static lib.

Project is open sourced, so here is the solution for reference: https://github.com/gideros/gideros/tree/master/winrt

What I need to do, is to make it use networking code, which uses winsockets. Since this is a library project, there was no Linker section in project properties, so I simply included

#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")

Both projects built without errors and when library included in Windows Phone app, it worked exactly as expected, but in Windows app networking part was not working, but also no errors or crashes.

I ran it through App Certification program and it said:

Error Found: The supported APIs test detected the following errors:
API WSACleanup in ws2_32.dll is not supported for this application type. 

and lots of other references to ws2_32 library

I started searching on internet, and there were lots of posts stating that you can't use winsocket on Windows 8.1, you have to use Microsoft's winrt socket implementation etc.

But then I stumbled on this blog announcing that it is now (since VS 2013 Update 3) possible to use winsock on both Windows 8.1 and Windows Phone 8.1

http://blogs.windows.com/buildingapps/2014/10/13/winsock-and-more-open-source-for-your-windows-store-apps/

Quoting:

We're happy to announce that starting with the release of Visual Studio 2013 Update 3 (download) in August, the Windows App Certification Kit (WACK) allows use of WinSock APIs in your Windows Store apps. And since WinSock was already allowed to pass certification on Windows Phone, this work completes the story. Now WinSock is available universally across Windows 8.1, Windows Phone 8.1, and universal apps.

As I had VS 2013 Update 4, it should have already worked. So I checked App certification version (it was 3.3), and it stated it had update available, so I downloaded Windows SDK for Windows 8.1 from:

https://dev.windows.com/en-us/develop/app-certification-kit

And now I can't even build the project, because it says that all Ws2_32.lib parts are missing for Windows 8.1 target. (while Windows Phone 8.1 still builds and works as expected)

So is it even possible to use winsockets on Windows 8.1 apps? And if yes, what am I missing?

Update 1:

It seems to be possible, but the problem is with my set up.

When I check the header files where my project points, then they are:

for Windows phone its

Program Files (x86)/Windows Phone Kits/8.1/Include/winsock2.h

for Windows it

Program Files (x86)/Windows Kits/8.1/Include/um/WinSock2.h

And when I check the file that is for Windows, then it has a preprocessor defined:

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)

And it does not go into this if statement, thus for compiler this file is basically empty

I even tried to uninstall Windows 8.1 SDK, which did not change anything.

Then I manually removed files from Program Files folder and installed a new one from

https://dev.windows.com/en-us/develop/app-certification-kit

still I have the same header. So where do I get latest Windows 8.1 SDK with proper winsock2 header file?

Update 2:

So I removed everything and re-installed clean VS 2013 Express Update 4 and again same result, it all builds without errors, but networking does not work on Windows 8.1 app, while it perfectly works on Windows Phone 8.1.

It is like Windows 8.1 have only empty stubs, that do nothing.

I've been asking in Windows developer forum and one of the Microsoft representatives stated that, quoting:

If you have VS 2013 Update 4 properly installed, then you will have gotten an updated Windows 8.1 SDK. In that version of winsock2.h, only the ASCII versions of most of the WinSock APIs are excluded from Windows Store. You have to use UNICODE exclusively for Windows 8 Store apps.

Unfortunately I have no idea what that means and how should I proceed. If that means defining UNICODE macro then it is already defined.

Update 3:

I've managed to debug the winsocket code and everything is running as expected there, server is listening on the port. But nothing can connect to it. It's like it is running in sandboxed mode

Update 4:

Running netstat and wireshark all seems to work correctly and see server and I can even connect to it from other computer. But I can't connect from the same computer.

And it seems that MS forbids Windows apps to communicate with desktop apps on the same machine

I have no idea why, anyone can provide any insights?

How can a Metro app in Windows 8 communicate with a backend desktop app on the same machine?

The last question is

If VS 2013 Update 4 bundles winsock2 header that builds with WACK 3.3 that does not allow winsockets, while Windows 8.1 SDK update has WACK3.4 that allows socket API, but bundles older winsock2 header which does not build.

How to get WACK3.4 with correct winsock2 setup?

WinSock is supported on Windows and Windows Phone, and is allowed using WACK 3.4. It works for me using both #pragma comment(lib, "ws2_32.lib") and by adding ws2_32.lib directly to the project settings.

It looks like there is something wrong with the way you are building the project after the upgrade; can you post the errors you got after the upgrade?

So basically sockets do work, but there are couple of cases to be aware of.

There is a network isolation, which does not allow to connect to Windows Store apps from same machine (as I did in my tests and thought networking does not work).

But if you can connect from remote machine, everything works as expected.

More info on that:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd374089%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

But you still won't be able to pass WACK (which is not needed to submit to store, but still)

Problem is that VS 2013 Update 4 bundles winsock2 header that builds and works and it has WACK 3.3 software that does not allow winsockets.

While Windows 8.1 SDK update has WACK3.4 that allows socket API, but bundles older winsock2 header which does not build and work.

As you don't need to run WACK to submit app, mostly it won't be a problem, but as I needed to check if there are any other specific libraries I use, that can be prohibited, then I had to only install single (App certification) component from Windows 8.1 SDK from here:

https://dev.windows.com/en-us/develop/downloads

And I have WACK 3.4 and proper winsockets now

Still too bad about network isolation, that basically kills the purpose of what I was porting in the first place. Making all the work basically in vain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM