diff options
Diffstat (limited to 'winsup/mingw/samples/dlltest')
-rw-r--r-- | winsup/mingw/samples/dlltest/dll.c | 22 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/dll.def | 3 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/dll.h | 4 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/exe.c | 23 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/exe.exp | 8 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/expexe.c | 17 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/expexe.def | 2 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/jamfile | 46 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/loaddll.c | 40 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/loadexe.c | 47 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/readme.txt | 39 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/silly.cpp | 55 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/silly.def | 11 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/silly.exp | 8 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/silly.h | 27 | ||||
-rw-r--r-- | winsup/mingw/samples/dlltest/sillydll.cpp | 107 |
16 files changed, 459 insertions, 0 deletions
diff --git a/winsup/mingw/samples/dlltest/dll.c b/winsup/mingw/samples/dlltest/dll.c new file mode 100644 index 000000000..ba00fab3e --- /dev/null +++ b/winsup/mingw/samples/dlltest/dll.c @@ -0,0 +1,22 @@ +/*
+ * Source code of the functions inside our test DLL. Note that DllMain is
+ * not required (it will be provided by the stub in libmingw32.a).
+ */
+
+#if 0
+#include <windows.h>
+#endif
+
+int Add (int x, int y)
+{
+ printf ("In add!\nx = %d\ny = %d\n", x, y);
+ return (x + y);
+}
+
+
+double __attribute__((stdcall)) Sub (double x, double y)
+{
+ printf ("In sub!\nx = %f\ny = %f\n", x, y);
+ return (x - y);
+}
+
diff --git a/winsup/mingw/samples/dlltest/dll.def b/winsup/mingw/samples/dlltest/dll.def new file mode 100644 index 000000000..4572319b3 --- /dev/null +++ b/winsup/mingw/samples/dlltest/dll.def @@ -0,0 +1,3 @@ +EXPORTS
+Add
+Sub@16
diff --git a/winsup/mingw/samples/dlltest/dll.h b/winsup/mingw/samples/dlltest/dll.h new file mode 100644 index 000000000..35faa4c32 --- /dev/null +++ b/winsup/mingw/samples/dlltest/dll.h @@ -0,0 +1,4 @@ +
+int Add (int x, int y);
+double __attribute__((stdcall)) Sub (double x, double y);
+
diff --git a/winsup/mingw/samples/dlltest/exe.c b/winsup/mingw/samples/dlltest/exe.c new file mode 100644 index 000000000..06a580a67 --- /dev/null +++ b/winsup/mingw/samples/dlltest/exe.c @@ -0,0 +1,23 @@ +#include <stdio.h>
+
+#include "dll.h"
+
+int main()
+{
+ int i, j, k;
+ double dk;
+
+ i = 10;
+ j = 13;
+
+ k = Add(i, j);
+
+ printf ("%d + %d = %d\n", i, j, k);
+
+ dk = Sub(i, j);
+
+ printf ("%d - %d = %f\n", i, j, dk);
+
+ return 0;
+}
+
diff --git a/winsup/mingw/samples/dlltest/exe.exp b/winsup/mingw/samples/dlltest/exe.exp new file mode 100644 index 000000000..584b5306b --- /dev/null +++ b/winsup/mingw/samples/dlltest/exe.exp @@ -0,0 +1,8 @@ +In add!
+x = 10
+y = 13
+10 + 13 = 23
+In sub!
+x = 10
+y = 13
+10 - 13 = -3
diff --git a/winsup/mingw/samples/dlltest/expexe.c b/winsup/mingw/samples/dlltest/expexe.c new file mode 100644 index 000000000..10a8f08b1 --- /dev/null +++ b/winsup/mingw/samples/dlltest/expexe.c @@ -0,0 +1,17 @@ +
+#include <stdio.h>
+
+int
+ExportedFromExe ()
+{
+ printf ("This output produced by ExportedFromExe.\n");
+ return 0;
+}
+
+int main()
+{
+ printf ("Hello, world\n");
+
+ return 0;
+}
+
diff --git a/winsup/mingw/samples/dlltest/expexe.def b/winsup/mingw/samples/dlltest/expexe.def new file mode 100644 index 000000000..fa0aa2856 --- /dev/null +++ b/winsup/mingw/samples/dlltest/expexe.def @@ -0,0 +1,2 @@ +EXPORTS
+ExportedFromExe
diff --git a/winsup/mingw/samples/dlltest/jamfile b/winsup/mingw/samples/dlltest/jamfile new file mode 100644 index 000000000..5278d0379 --- /dev/null +++ b/winsup/mingw/samples/dlltest/jamfile @@ -0,0 +1,46 @@ +
+# This option is required to successfully return doubles via STDCALL as in
+# Sub function in dll.c.
+CCFLAGS = -mno-fp-ret-in-387 ;
+
+Main exe.exe : exe.c ;
+
+LinkLibraries exe.exe : libdll.a ;
+
+DEPENDS exe.exe : dll.dll ;
+
+LINKFLAGS on exe.exe = $(LINKFLAGS) -L. ;
+
+
+Main loaddll.exe : loaddll.c ;
+
+DEPENDS loaddll.exe : dll.dll ;
+
+
+Dll dll.dll : dll.c ;
+
+ImportLib libdll.a : dll.def ;
+
+
+Main expexe.exe : expexe.c ;
+
+# Force the executable to include the expexe.def file.
+Exports expexe.exe : expexe.def ;
+
+Main loadexe.exe : loadexe.c ;
+
+DEPENDS loadexe.exe : expexe.exe ;
+
+
+Main silly.exe : silly.cpp ;
+
+LinkLibraries silly.exe : libsilly.a ;
+
+DEPENDS silly.exe : silly.dll ;
+
+LINKFLAGS on silly.exe += -L. ;
+
+Dll silly.dll : sillydll.cpp ;
+
+ImportLib libsilly.a : silly.def ;
+
diff --git a/winsup/mingw/samples/dlltest/loaddll.c b/winsup/mingw/samples/dlltest/loaddll.c new file mode 100644 index 000000000..bbb34a3c9 --- /dev/null +++ b/winsup/mingw/samples/dlltest/loaddll.c @@ -0,0 +1,40 @@ +/*
+ * This version attempts to load dll.dll dynamically, get the address of the
+ * Add function, and then call it.
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+int (*Add)(int x, int y);
+
+int main()
+{
+ HINSTANCE hDll;
+ int i, j, k;
+
+ hDll = LoadLibrary ("dll.dll");
+ if (!hDll)
+ {
+ printf ("Error %d loading dll.\n", GetLastError());
+ exit (-1);
+ }
+
+ if (!(Add = GetProcAddress (hDll, "Add")))
+ {
+ printf ("Error %d getting Add function.\n", GetLastError());
+ exit (-1);
+ }
+
+ i = 10;
+ j = 13;
+
+ k = Add(i, j);
+
+ printf ("i %d, j %d, k %d\n", i, j, k);
+
+ FreeLibrary (hDll);
+
+ return 0;
+}
+
diff --git a/winsup/mingw/samples/dlltest/loadexe.c b/winsup/mingw/samples/dlltest/loadexe.c new file mode 100644 index 000000000..c5a84dc01 --- /dev/null +++ b/winsup/mingw/samples/dlltest/loadexe.c @@ -0,0 +1,47 @@ +/*
+ * This program attempts to load expexe.exe dynamically, get the address of the
+ * ExportedFromExe function, and then call it.
+ *
+ * This example DOES NOT WORK! I don't know exactly what can be done, but
+ * it simply seems that LoadLibrary refuses to load executables.
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+int (*ExportedFromExe)();
+
+int main()
+{
+ HINSTANCE hDll;
+ int i, j, k;
+
+ hDll = LoadLibrary ("expexe.exe");
+ if (!hDll)
+ {
+ printf ("Error %d loading exe.\n", GetLastError());
+ exit (-1);
+ }
+
+ if (!(ExportedFromExe = GetProcAddress (hDll, "ExportedFromExe")))
+ {
+ printf ("Error %d getting ExportedFromExe function.\n",
+ GetLastError());
+ exit (-1);
+ }
+ else
+ {
+ ExportedFromExe ();
+ }
+
+ /* NOTE: Unlike a DLL the exe doesn't have an entry point which
+ * initializes global objects and adds __do_global_dtors to
+ * the atexit list. Thus it should be safe(?) to free the
+ * library. Of course, this also makes it unsafe to use
+ * executables at all in this manner.
+ */
+ FreeLibrary (hDll);
+
+ return 0;
+}
+
diff --git a/winsup/mingw/samples/dlltest/readme.txt b/winsup/mingw/samples/dlltest/readme.txt new file mode 100644 index 000000000..a46dd23a5 --- /dev/null +++ b/winsup/mingw/samples/dlltest/readme.txt @@ -0,0 +1,39 @@ +This directory contains two examples of building DLLs. The exe.c and dll.c
+files are used to build a very simple example DLL with a function that
+adds two numbers together (and prints some text at the same time). The
+exe.c program links to the DLL and prints the results of the function
+call.
+
+The C++ example "silly" is more interesting because it involves a DLL which
+contains the code for a C++ class. The CSilly class has all of its code in
+the sillydll.cpp source file, which is used to build the silly.dll. The
+silly.cpp source code builds the main silly.exe executable which makes a
+dynamic instance of the object and calls its member functions.
+
+The C++ silly.def file was generated by doing a nm of sillydll.o after it
+was generated and then getting the symbol names from that. Removing the
+leading underscore produces the appropriate name to include in the EXPORTS
+section. Notice there are a few weird functions.
+
+Since there are now several different versions of the GNU compiler capable
+of doing this, and they each seem to have different requirements for exports
+for classes, it has gotten kind of messy. The silly.def file here is for
+use with the native Mingw32 build of the EGCS version of GCC. The silly.def.old
+file was the def file I used when I was using Jan-Jaap's Mingw32 native port
+of GCC. The Cygnus version is different again, if I recall correctly, but I
+don't have it hanging around anymore.
+
+The jamfile builds all the components from the raw sources.
+
+The expected output of exe.exe and silly.exe are in the files exe.exp
+and silly.exp.
+
+
+The source code in this directory is in the PUBLIC DOMAIN and can be
+used or abused as you see fit. There is NO WARRANTY for this code,
+including (but not limited to) implied warranties of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE.
+
+
+Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+
diff --git a/winsup/mingw/samples/dlltest/silly.cpp b/winsup/mingw/samples/dlltest/silly.cpp new file mode 100644 index 000000000..70f5af5c7 --- /dev/null +++ b/winsup/mingw/samples/dlltest/silly.cpp @@ -0,0 +1,55 @@ +//
+// C++ test of a dll which contains a C++ class.
+//
+
+#include <stdlib.h>
+#include <stdio.h>
+
+// Interface of class.
+#include "silly.h"
+
+#ifdef DERIVED_TEST
+// Here is a derived class too.
+class CMoreSilly : public CSilly
+{
+ public:
+ CMoreSilly (char* szNewName) : CSilly (szNewName) {};
+ ~CMoreSilly ();
+
+ WhatsYourName();
+};
+
+CMoreSilly::
+~CMoreSilly ()
+{
+ printf ("In CMoreSilly \"%s\" destructor!\n", szName);
+}
+
+CMoreSilly::
+WhatsYourName ()
+{
+ printf ("I'm more silly and my name is \"%s\"\n", szName);
+}
+#endif
+
+int
+main ()
+{
+ CSilly* psilly = new CSilly("silly");
+
+ psilly->WhatsYourName();
+ psilly->Poke(); // Poke him, he should say "Ouch!"
+ psilly->Stab(4); // Stab him four times he should say "Ugh!!!!"
+
+ delete psilly;
+
+#ifdef DERIVED_TEST
+ psilly = new CMoreSilly("more silly");
+ psilly->WhatsYourName();
+ psilly->Stab(5);
+ delete psilly;
+#endif
+
+ return 0;
+}
+
diff --git a/winsup/mingw/samples/dlltest/silly.def b/winsup/mingw/samples/dlltest/silly.def new file mode 100644 index 000000000..6733e5df7 --- /dev/null +++ b/winsup/mingw/samples/dlltest/silly.def @@ -0,0 +1,11 @@ +EXPORTS
+DllMain@12
+Poke__6CSilly
+Stab__6CSillyi
+WhatsYourName__6CSilly
+_$_6CSilly
+__6CSilly
+__6CSillyPc
+__tf6CSilly
+__ti6CSilly
+_vt$6CSilly
diff --git a/winsup/mingw/samples/dlltest/silly.exp b/winsup/mingw/samples/dlltest/silly.exp new file mode 100644 index 000000000..386e81c1c --- /dev/null +++ b/winsup/mingw/samples/dlltest/silly.exp @@ -0,0 +1,8 @@ +I'm silly.
+Ouch!
+Ugh!!!!
+In CSilly destructor.
+I'm more silly and my name is "more silly"
+Ugh!!!!!
+In CMoreSilly "more silly" destructor!
+In CSilly destructor.
diff --git a/winsup/mingw/samples/dlltest/silly.h b/winsup/mingw/samples/dlltest/silly.h new file mode 100644 index 000000000..007b0f5d7 --- /dev/null +++ b/winsup/mingw/samples/dlltest/silly.h @@ -0,0 +1,27 @@ +
+#define DERIVED_TEST 1
+
+class CSilly
+{
+ protected:
+ char* szName;
+
+ public:
+ CSilly();
+ CSilly(char* szName);
+#ifdef DERIVED_TEST
+ virtual ~CSilly();
+#else
+ ~CSilly();
+#endif
+
+ Poke ();
+ Stab (int nTimes);
+#ifdef DERIVED_TEST
+ virtual WhatsYourName ();
+#else
+ WhatsYourName ();
+#endif
+
+};
+
diff --git a/winsup/mingw/samples/dlltest/sillydll.cpp b/winsup/mingw/samples/dlltest/sillydll.cpp new file mode 100644 index 000000000..bd5ccea30 --- /dev/null +++ b/winsup/mingw/samples/dlltest/sillydll.cpp @@ -0,0 +1,107 @@ +
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <windows.h>
+
+
+#if 0
+#define STREAMS_VERSION
+#endif
+
+#if defined(STREAMS_VERSION)
+#include <iostream.h>
+#endif
+
+#include "silly.h"
+
+extern "C"
+BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
+{
+ return TRUE;
+}
+
+CSilly::
+CSilly()
+{
+ szName = NULL;
+}
+
+CSilly::
+CSilly(char* new_szName)
+{
+ szName = new char[strlen(new_szName)+1];
+
+ if (szName)
+ {
+ strcpy (szName, new_szName);
+ }
+}
+
+CSilly::
+~CSilly()
+{
+ printf ("In CSilly destructor.\n");
+ if (szName)
+ {
+ delete szName;
+ }
+}
+
+CSilly::
+Poke ()
+{
+#ifndef STREAMS_VERSION
+ printf ("Ouch!\n");
+#else
+ cout << "Ouch!" << endl;
+#endif
+}
+
+CSilly::
+Stab (int nTimes)
+{
+#ifndef STREAMS_VERSION
+ printf ("Ugh");
+#else
+ cout << "Ugh";
+#endif
+
+ int i;
+ for (i = 0; i < nTimes; i++)
+ {
+#ifndef STREAMS_VERSION
+ putchar('!');
+#else
+ cout << '!' ;
+#endif
+ }
+
+#ifndef STREAMS_VERSION
+ putchar('\n');
+#else
+ cout << endl;
+#endif
+}
+
+CSilly::
+WhatsYourName ()
+{
+ if (szName)
+ {
+#ifndef STREAMS_VERSION
+ printf ("I'm %s.\n", szName);
+#else
+ cout << "I'm " << szName << "." << endl;
+#endif
+ }
+ else
+ {
+#ifndef STREAMS_VERSION
+ printf ("I have no name.\n");
+#else
+ cout << "I have no name." << endl;
+#endif
+ }
+}
+
|