Programming Tips - How can I do a SSL / TLS connection in my win32 application?

Date: 2017jan26 OS: Windows Platform: win32 Language: C/C++ Keywords: intro, introduction Q. How can I do a SSL / TLS connection in my win32 application? A. There are two ways. Use WinInet like this:
#include "Wininet.h" HINTERNET m_hInternet = InternetOpen(szId, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET m_hConnection = InternetConnect( m_hInternet, szServerName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, dwFlags, 0);
But it only works for http and ftp (and gopher). Or you can use Microsoft SecureChannel. Your code will begin:
#include <winsock2.h> #include <wincrypt.h> #include <wintrust.h> #include <schannel.h> #include <security.h> #include <sspi.h> #include <deque>
This uses regular winsock2 sockets and you call some (complicated) functions to upgrade the connection to secure. So this is more low-level than the method above. There are other non-Microsoft libraries like curl and openssl.