diff --git a/CMakeLists.txt b/CMakeLists.txt
index 11b3ee66..5a27e442 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,9 +3,13 @@
 ########################################################################
 cmake_minimum_required(VERSION 2.6)
 # Fix behavior of CMAKE_C_STANDARD when targeting macOS.
-if (POLICY CMP0025)
+if(POLICY CMP0025)
     cmake_policy(SET CMP0025 NEW)
-endif ()
+endif()
+# Only interpret if() arguments as variables or keywords when unquoted.
+if(POLICY CMP0054)
+    cmake_policy(SET CMP0054 NEW)
+endif()
 
 project(rtl433 C)
 
@@ -40,7 +44,7 @@ endif()
 ########################################################################
 set(CMAKE_C_EXTENSIONS OFF)
 set(CMAKE_C_STANDARD 99)
-if (("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang") AND NOT WIN32)
+if(("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang") AND NOT WIN32)
     ADD_DEFINITIONS(-Wall)
     ADD_DEFINITIONS(-Wextra)
     ADD_DEFINITIONS(-Wsign-compare)
@@ -59,11 +63,18 @@ if (("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES
     # CMake Debug default for GCC/Clang is "-g -DNDEBUG"
     # set(CMAKE_C_FLAGS_DEBUG -g3 -O0)
 endif()
-if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
     # make sure we don't accidentally copy more than an int
     ADD_DEFINITIONS(-Wlarge-by-value-copy=8)
 endif()
 
+# Shut MSVC up about strdup and strtok
+if(MSVC)
+    ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
+    ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
+    ADD_DEFINITIONS(-DNOMINMAX)
+endif()
+
 ########################################################################
 # Find build dependencies
 ########################################################################
@@ -104,7 +115,7 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "Profile")
     ADD_DEFINITIONS(-fno-builtin-free)
 endif()
 
-if(MINGW)
+if(MINGW OR MSVC)
 list(APPEND NET_LIBRARIES ws2_32 mswsock)
 endif()
 
diff --git a/include/util.h b/include/util.h
index 68259700..04e474b6 100644
--- a/include/util.h
+++ b/include/util.h
@@ -13,7 +13,7 @@
 
 #include <stdint.h>
 
-// Helper macros
+// Helper macros, collides with MSVC's stdlib.h unless NOMINMAX is used
 #ifndef max
 #define max(a,b) ((a) > (b) ? (a) : (b))
 #endif
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 150794fe..e685b85e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -125,13 +125,18 @@ add_executable(rtl_433
 	devices/x10_sec.c
 )
 
-if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
+if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
 	SET_SOURCE_FILES_PROPERTIES(mongoose.c PROPERTIES COMPILE_FLAGS "-Wno-format")
 endif()
-if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
 	SET_SOURCE_FILES_PROPERTIES(mongoose.c PROPERTIES COMPILE_FLAGS "-Wno-format-pedantic -Wno-large-by-value-copy")
 endif()
 
+if(MSVC)
+	# needs CMake 3.1 but Windows builds should have that
+	target_sources(rtl_433 PRIVATE getopt/getopt.c)
+endif()
+
 add_library(data data.c abuf.c term_ctl.c mongoose.c)
 target_link_libraries(data ${NET_LIBRARIES})
 
diff --git a/src/data.c b/src/data.c
index 5a2ea284..af575800 100644
--- a/src/data.c
+++ b/src/data.c
@@ -766,6 +766,8 @@ static void data_output_csv_start(struct data_output *output, const char **field
     csv->separator = ",";
 
     allowed = calloc(num_fields, sizeof(const char *));
+    if (!allowed)
+        goto alloc_error;
     memcpy(allowed, fields, sizeof(const char *) * num_fields);
 
     qsort(allowed, num_fields, sizeof(char *), compare_strings);
diff --git a/src/rtl_433.c b/src/rtl_433.c
index 01efbcbc..d6ef352c 100644
--- a/src/rtl_433.c
+++ b/src/rtl_433.c
@@ -50,13 +50,17 @@
 #include <io.h>
 #include <fcntl.h>
 #ifdef _MSC_VER
-#include "getopt/getopt.h"
 #define F_OK 0
 #endif
 #endif
 #ifndef _MSC_VER
 #include <unistd.h>
+#endif
+
+#ifndef _MSC_VER
 #include <getopt.h>
+#else
+#include "getopt/getopt.h"
 #endif
 
 #ifdef GIT_VERSION
diff --git a/tests/baseband-test.c b/tests/baseband-test.c
index 8f3cf45d..11834ed7 100644
--- a/tests/baseband-test.c
+++ b/tests/baseband-test.c
@@ -15,7 +15,23 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <sys/types.h>
+#ifdef _MSC_VER
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#endif
+
+#ifdef _WIN32
+#include <io.h>
+#include <fcntl.h>
+#ifdef _MSC_VER
+#define F_OK 0
+#define R_OK (1 << 2)
+#endif
+#endif
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif
+
 #include <time.h>
 
 #include "baseband.h"