Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I actually have to link Ws2_32.lib?

All Winsock2 examples at MSDN shows that I have to statically link Winsock2 libarary as:

#pragma comment(lib, "ws2_32.lib")

Ensure that the build environment links to the Winsock Library file ?>Ws2_32.lib. Applications that use Winsock must be linked with the Ws2_32.lib >library file. The #pragma comment indicates to the linker that the Ws2_32.lib >file is needed.

But why I use it instead of simple loading an existing in Windows Ws2_32.dll (since Windows 2003 as I understand according to requirements at MSDN https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-recv)

So I can use something like this:

typedef int WSAAPI(WINAPI* recv_func)(SOCKET s, char *buf, int len, int flags);

HINSTANCE hGetProcIDDLL = LoadLibraryA("ws2_32.dll");
my_recv = (recv_func)GetProcAddress(hGetProcIDDLL, "recv");

Or I can just use winsock2.h header and compile a program with /MD flag:

include <winsock2.h>
//#pragma comment(lib, "ws2_32.lib")

Is it possible? Can I use /MD or load ws2_32.dll dynamically as in first example without statically linking ws2_32.lib to my application because all Windows since Win2003 have ws2_32.dll it in System32 folder?

like image 344
bawytic Avatar asked Sep 07 '19 08:09

bawytic


People also ask

What is ws2_32 lib?

The library ws2_32. lib is an import library. It consist of small stubs that will redirect to the actual implementation in ws2_32. dll. The DLL will be loaded at program load time.

What is #pragma comment Lib ws2_32 lib?

The #pragma comment(lib, "Ws2_32. lib") is an way of telling the VC++ compiler to link with library ws2_32.


1 Answers

The library ws2_32.lib is an import library. It consist of small stubs that will redirect to the actual implementation in ws2_32.dll. The DLL will be loaded at program load time. It's called Load-Time Dynamic Linking.

You can verify this by comparing the size of both files. Additional you can use dumpbin -symbols ws2_32.lib. It shows you none of the functions you might expect from the ws2.h prototypes.

Well, you already load the DLL dynamically, at load time. It would also be possible to do this at run-time with LoadLibrary and GetProcAddress. But this is cumbersome and don't give you any benefit.

Edit: You also link other import libraries without explicitly specifying it. The project inherits from property sheets (see menu: View -> Other Windows -> Property Manager ) as well as from built-in rules. You can see the resulting linker command line in the projects properties. Select the project and press alt-Enter to open the project properties and navigate then to Configuration Properties -> Linker -> Input to display the linker command line. With VS2017 Win32 C++ projects you find these import libraries:

"kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"

like image 110
harper Avatar answered Oct 22 '22 08:10

harper