44local ffi = require " ffi"
55local lfs = require " lfs"
66
7+ -- Load C standard library
8+ local ok , libc = pcall (ffi .load , " ucrtbase" )
9+ if not ok then
10+ ok , libc = pcall (ffi .load , " msvcrt" )
11+ end
12+ if not ok then
13+ error (" Could not load C standard library (ucrtbase.dll or msvcrt.dll not found)." )
14+ end
15+
716-- Declaration of utilized C-functions
817-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crt-alphabetical-function-reference
918-- std lib functions
1019ffi .cdef ([[
1120 void srand(unsigned int seed);
1221 int rand(void);
1322 size_t strlen(const char *str);
23+ int64_t _time64(int64_t *destTime);
1424 char *_strtime(char *timestr);
15- time_t time(time_t *destTime);
1625]] )
1726-- National Instruments DAQmx fubnctions (ffi.cdef may be called not just one time)
1827ffi .cdef ([[
@@ -28,9 +37,9 @@ print(" C-declaration: int rand(void);")
2837for i = 1 , 10 do
2938 if i == 1 then
3039 print (" Setting random seed to 12345." )
31- ffi . C .srand (12345 )
40+ libc .srand (12345 )
3241 end
33- io.write (string.format (" %d" ,ffi . C .rand ()))
42+ io.write (string.format (" %d" ,libc .rand ()))
3443end
3544print (" \n Done.\n " )
3645
@@ -40,7 +49,7 @@ print(" C-declaration: size_t strlen(const char *str);")
4049local teststr = " The quick brown fox jumps over the lazy dog."
4150print (string.format (" The test string is '%s'" ,teststr ))
4251print (string.format (" Lua: #teststr = %d" ,# teststr ))
43- print (string.format (" C: strlen(..) = %d" ,ffi . C .strlen (teststr )))
52+ print (string.format (" C: strlen(..) = %d" ,libc .strlen (teststr )))
4453print (" Done.\n " )
4554
4655-- https://github.com/q66/cffi-lua/blob/master/tests/cast.lua
@@ -82,15 +91,15 @@ print("Done.\n")
8291print (" Testing char[] passing to _strtime() ..." )
8392print (" C-declaration: char *_strtime(char *timestr);" )
8493local buf = ffi .new (" char[]" ,9 )
85- print (string.format (" Current time is %s" ,ffi .string (ffi . C ._strtime (buf ))))
94+ print (string.format (" Current time is %s" ,ffi .string (libc ._strtime (buf ))))
8695print (" Done.\n " )
8796
8897-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/time-time32-time64
8998print (" Testing seconds elapsed since midnight (00:00:00), January 1, 1970 ..." )
90- print (" C-declaration: time_t time (time_t *destTime);" )
91- print (string.format (" Using function return value : %d" ,ffi . C . time (ffi .nullptr )))
92- local t = ffi .new (" time_t " )
93- ffi . C . time (ffi .addressof (t ))
99+ print (" C-declaration: int64_t _time64 (time_t *destTime);" )
100+ print (string.format (" Using function return value : %d" ,libc . _time64 (ffi .nullptr )))
101+ local t = ffi .new (" int64_t " )
102+ libc . _time64 (ffi .addressof (t ))
94103print (string.format (" Using destTime argument : %d" ,ffi .tonumber (t )))
95104print (" Done.\n " )
96105
138147 print (string.format (" DAQmxGetSysNIDAQUpdateVersion = %d" ,ffi .tonumber (arg )))
139148end
140149print (" Done.\n " )
150+
151+ print (" All tests PASSED." )
0 commit comments