Initial commit of newLISP.
[newlisp.git] / win32dll.c
blobb2840cb4bef4310697ae92684cffa7c9b9abd2db
1 /* win32dll.c - make the newlisp.exe usable as a DLL
3 Copyright (C) 2008 Lutz Mueller
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "newlisp.h"
21 #include "protos.h"
23 /* note that DLL compile with MinGW is still in development */
24 #ifdef WIN_32
25 #define EXPORT __declspec(dllexport) __stdcall
26 #define DLLCALL EXPORT
27 #endif
29 #include <winsock2.h>
31 extern void loadStartup(char *name);
32 extern LPSTR getLibname(void);
33 extern int evalSilent;
34 extern int opsys;
35 extern SYMBOL * mainArgsSymbol;
38 int dllInitialized = 0;
39 WSADATA WSAData;
41 char libName[MAX_LINE] = "newlisp.dll";
43 void initializeMain(void)
45 char name[MAX_LINE + 1];
47 WSAStartup(MAKEWORD(1,1), &WSAData);
49 opsys += 64;
51 #ifdef SUPPORT_UTF8
52 opsys += 128;
53 #endif
55 initLocale();
56 initialize();
57 mainArgsSymbol->contents = (UINT)getCell(CELL_EXPRESSION);
58 initStacks();
60 GetModuleFileName(GetModuleHandle(libName), name, MAX_LINE);
62 loadStartup(name);
64 dllInitialized = 1;
65 reset();
68 /* ------------ initialize DLL (gets called by Windows) ----------------- */
70 extern STREAM errorStream;
72 STREAM libStrStream;
74 int CALLBACK LibMain(HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine)
76 return 1;
80 /* called automatically from nl-import when DLL is loaded from newLISP */
82 int DLLCALL dllName(LPSTR name)
84 strncpy(libName, name, MAX_LINE);
85 return(1);
89 /* ---- imported and called from a client using newlisp.dll ---- */
91 LPSTR DLLCALL newlispEvalStr(LPSTR cmd)
93 if(!dllInitialized) initializeMain();
95 if(setjmp(errorJump))
97 reset();
98 initStacks();
99 if(errorReg)
101 executeSymbol(errorEvent, NULL);
102 return((LPSTR)libStrStream.buffer);
104 else
105 return((LPSTR)errorStream.buffer);
108 openStrStream(&libStrStream, MAX_STRING, 1);
109 executeCommandLine(cmd, (UINT)&libStrStream, NULL);
111 if(evalSilent) evalSilent = 0;
113 return((LPSTR)libStrStream.buffer);
117 LPSTR DLLCALL dllEvalStr(LPSTR cmd)
119 return(newlispEvalStr(cmd));
124 /* ------------ called from Windows when unloading DLL ------------------ */
126 int DLLCALL WEP (int bSystemExit)
128 return(1);
131 /* eof */