1 /*    Win32CORE.c
2  *
3  *    Copyright (C) 2007 by Larry Wall and others
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9 
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
12 
13 #if defined(__CYGWIN__) && !defined(USEIMPORTLIB)
14   #undef WIN32
15 #endif
16 #define PERL_NO_GET_CONTEXT
17 #include "EXTERN.h"
18 #if defined(__CYGWIN__) && !defined(USEIMPORTLIB)
19   #define EXTCONST extern const
20 #endif
21 #include "perl.h"
22 #include "XSUB.h"
23 
24 
25 XS(w32_CORE_all){
26     /* I'd use dSAVE_ERRNO() here, but it doesn't save the Win32 error code
27      * under cygwin, if that changes this code should change to use that.
28      */
29     int saved_errno = errno;
30     DWORD err = GetLastError();
31     /* capture the XSANY value before Perl_load_module, the CV's any member will
32      * be overwritten by Perl_load_module and subsequent newXSes or pure perl
33      * subs
34      */
35     const char *function  = (const char *) XSANY.any_ptr;
36     Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn("Win32",5), newSVnv(0.27));
37     SetLastError(err);
38     errno = saved_errno;
39     /* mark and SP from caller are passed through unchanged */
40     call_pv(function, GIMME_V);
41 }
42 
43 XS_EXTERNAL(boot_Win32CORE)
44 {
45     /* This function only exists because writemain.SH, lib/ExtUtils/Embed.pm
46      * and win32/buildext.pl will all generate references to it.  The function
47      * should never be called though, as Win32CORE.pm doesn't use DynaLoader.
48      */
49 }
50 #if !defined(__CYGWIN__) || defined(USEIMPORTLIB)
51 __declspec(dllexport)
52 #endif
53 void
54 init_Win32CORE(pTHX)
55 {
56     /* This function is called from init_os_extras().  The Perl interpreter
57      * is not yet fully initialized, so don't do anything fancy in here.
58      */
59 
60     static const struct {
61 	char Win32__GetCwd [sizeof("Win32::GetCwd")];
62 	char Win32__SetCwd [sizeof("Win32::SetCwd")];
63 	char Win32__GetNextAvailDrive [sizeof("Win32::GetNextAvailDrive")];
64 	char Win32__GetLastError [sizeof("Win32::GetLastError")];
65 	char Win32__SetLastError [sizeof("Win32::SetLastError")];
66 	char Win32__LoginName [sizeof("Win32::LoginName")];
67 	char Win32__NodeName [sizeof("Win32::NodeName")];
68 	char Win32__DomainName [sizeof("Win32::DomainName")];
69 	char Win32__FsType [sizeof("Win32::FsType")];
70 	char Win32__GetOSVersion [sizeof("Win32::GetOSVersion")];
71 	char Win32__IsWinNT [sizeof("Win32::IsWinNT")];
72 	char Win32__IsWin95 [sizeof("Win32::IsWin95")];
73 	char Win32__FormatMessage [sizeof("Win32::FormatMessage")];
74 	char Win32__Spawn [sizeof("Win32::Spawn")];
75 	char Win32__GetTickCount [sizeof("Win32::GetTickCount")];
76 	char Win32__GetShortPathName [sizeof("Win32::GetShortPathName")];
77 	char Win32__GetFullPathName [sizeof("Win32::GetFullPathName")];
78 	char Win32__GetLongPathName [sizeof("Win32::GetLongPathName")];
79 	char Win32__CopyFile [sizeof("Win32::CopyFile")];
80 	char Win32__Sleep [sizeof("Win32::Sleep")];
81     } fnname_table = {
82 	"Win32::GetCwd",
83 	"Win32::SetCwd",
84 	"Win32::GetNextAvailDrive",
85 	"Win32::GetLastError",
86 	"Win32::SetLastError",
87 	"Win32::LoginName",
88 	"Win32::NodeName",
89 	"Win32::DomainName",
90 	"Win32::FsType",
91 	"Win32::GetOSVersion",
92 	"Win32::IsWinNT",
93 	"Win32::IsWin95",
94 	"Win32::FormatMessage",
95 	"Win32::Spawn",
96 	"Win32::GetTickCount",
97 	"Win32::GetShortPathName",
98 	"Win32::GetFullPathName",
99 	"Win32::GetLongPathName",
100 	"Win32::CopyFile",
101 	"Win32::Sleep"
102     };
103 
104     static const unsigned char fnname_lens [] = {
105 	sizeof("Win32::GetCwd"),
106 	sizeof("Win32::SetCwd"),
107 	sizeof("Win32::GetNextAvailDrive"),
108 	sizeof("Win32::GetLastError"),
109 	sizeof("Win32::SetLastError"),
110 	sizeof("Win32::LoginName"),
111 	sizeof("Win32::NodeName"),
112 	sizeof("Win32::DomainName"),
113 	sizeof("Win32::FsType"),
114 	sizeof("Win32::GetOSVersion"),
115 	sizeof("Win32::IsWinNT"),
116 	sizeof("Win32::IsWin95"),
117 	sizeof("Win32::FormatMessage"),
118 	sizeof("Win32::Spawn"),
119 	sizeof("Win32::GetTickCount"),
120 	sizeof("Win32::GetShortPathName"),
121 	sizeof("Win32::GetFullPathName"),
122 	sizeof("Win32::GetLongPathName"),
123 	sizeof("Win32::CopyFile"),
124 	sizeof("Win32::Sleep")
125     };
126     const unsigned char * len = (const unsigned char *)&fnname_lens;
127     const char * function = (char *)&fnname_table;
128     while (function < (char *)&fnname_table + sizeof(fnname_table)) {
129 	const char * const file = __FILE__;
130 	CV * const cv = newXS(function, w32_CORE_all, file);
131 	XSANY.any_ptr = (void *)function;
132 	function += *len++;
133     }
134 
135 
136     /* Don't forward Win32::SetChildShowWindow().  It accesses the internal variable
137      * w32_showwindow in thread_intern and is therefore not implemented in Win32.xs.
138      */
139     /* newXS("Win32::SetChildShowWindow", w32_SetChildShowWindow, file); */
140 }
141