diff --git a/dulwich/_objects.c b/dulwich/_objects.c
index c16e5cc..124bf85 100644
--- a/dulwich/_objects.c
+++ b/dulwich/_objects.c
@@ -25,6 +25,13 @@
 typedef int Py_ssize_t;
 #endif
 
+#ifdef __MINGW_H
+size_t strnlen(char *text, size_t maxlen) {
+	const char *last = memchr(text, '\0', maxlen);
+	return last ? (size_t) (last - text) : maxlen;
+}
+#endif
+
 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
 
 static PyObject *sha_to_pyhex(const unsigned char *sha)
