Post by tkorrovi on Jun 6, 2004 11:31:41 GMT -5
As compiling dll files is often a problem, I describe here how to
compile DI training programs with open source c/c++ compiler
mingw. Unfortunately I don't use other compilers like ms visual
c, I guess that the same facilities must be there as well, but
one must know the compiler to find them. Still I recommend using
mingw, as this is open source and more standard. In the example
below I used of what was the latest version of mingw, version
3.1.0.
If you don't have mingw, then install it, you can get it from
www.mingw.org I used to install it in directory \mingw.
Then add \mingw\bin to Path environment variable, this variable
consists of paths separated by semicolon. In windows 2000 you
can change it at start > settings > control panel > system >
advanced > environment variables > system variables, in older
systems you must add set path= in autoexec.bat. This is all what is
necessary for installing mingw. The other very useful thing is
(almost) public domain programmer's editor vim, often considered
the best programmer's editor (you cannot do many things with,
say, wordpad), and it can be downloaded at www.vim.org
I usually open the windows explorer and start to edit files
there, so vim is usually open. So if you want to see the export
symbols in dll file, write something like !pexports dt.dll in the
vim command mode. The biggest problem is that different versions
of mingw generate differen export symbols for dll, so we must
remap them into symbols what the program imports, this can be
done with = in def file. So create a text file (file > new > txt
file) and name it dt.def Then copy the following into that file:
EXPORTS
attach
attach__FPUl=attach
detach
detach__Fv=detach
teach
teach__FPcUiPUlN32=teach
Then create makefile named mingw.mak and copy the following into
that file (replace \t with tab, this is *very* important):
CC= gcc
CFLAGS= -W -Wall -std=c89
OBJ= dthi.o
SRC= $(OBJ:.o=.c)
DLL= dthi.dll
DEF= dt.def
all: $(DLL)
$(DLL): $(OBJ)
\t$(CC) -shared -o $(DLL) $(OBJ) $(DEF)
$(OBJ): $(SRC)
An example dthi.c file is like this:
/* dthi.c */
#include <windows.h>
/* static variables remain intact between function calls */
static LPSTR instring, gather;
static INT noccount = 0;
/*
* called once in every cycle or once for every output
* character, you may just ignore 4 last parameters
*/
LPSTR teach (LPSTR outchar, UINT noc, ULONG* oldbegin,
ULONG* newbegin, ULONG* freebegin, ULONG* freelength)
{
if (noc == 3) /* number of characters per last cycle */
{
if (noccount == 3)
{
noccount = 0;
lstrcpy (gather, "");
}
lstrcat (gather, outchar); /* add to the end */
lstrcpy (instring, "");
noccount++; /* wait until last character in cycle */
if (noccount == 3 && lstrcmp (gather, "HI ") == 0)
lstrcpy (instring, "HI "); /* reaction to output */
}
else
lstrcpy (instring, "");
return instring;
}
/* called when dll is attached and when program starts */
VOID attach (ULONG* sknot)
{
/* allocate memory for strings and arrays */
instring = (LPSTR) GlobalAlloc (GPTR, 4);
gather = (LPSTR) GlobalAlloc (GPTR, 4);
lstrcpy (gather, "");
}
/* called when dll is detached and when program terminates */
VOID detach (VOID)
{
GlobalFree (instring);
GlobalFree (gather);
}
You can compile with:
redir -e make.txt mingw32-make -f mingw.mak all
In order to make it quickly, I used to write it in bat file
called build.bat. This generates make.txt where it writes
compiler errors. You can also use this file to go to the error
locations with vim, for that write cfile make.txt in vim command
mode. Then, cnext in vim command mode shows you the next error,
you can edit, save with ctrl-s (or w in command mode), and give
the command cnext again. Don't notice the warnings that some
parameters were not used, we didn't intend to use them. But it's
better that all warnings are on, as it sometimes may give a very
useful warning, what it shall not give otherwise.
This is all what I thought would be necessary to know about
compiling a training program. When you learn how to do that, it's
very fast and simple. if you have problems or questions, please
ask.
#nosmileys#nosmileys
compile DI training programs with open source c/c++ compiler
mingw. Unfortunately I don't use other compilers like ms visual
c, I guess that the same facilities must be there as well, but
one must know the compiler to find them. Still I recommend using
mingw, as this is open source and more standard. In the example
below I used of what was the latest version of mingw, version
3.1.0.
If you don't have mingw, then install it, you can get it from
www.mingw.org I used to install it in directory \mingw.
Then add \mingw\bin to Path environment variable, this variable
consists of paths separated by semicolon. In windows 2000 you
can change it at start > settings > control panel > system >
advanced > environment variables > system variables, in older
systems you must add set path= in autoexec.bat. This is all what is
necessary for installing mingw. The other very useful thing is
(almost) public domain programmer's editor vim, often considered
the best programmer's editor (you cannot do many things with,
say, wordpad), and it can be downloaded at www.vim.org
I usually open the windows explorer and start to edit files
there, so vim is usually open. So if you want to see the export
symbols in dll file, write something like !pexports dt.dll in the
vim command mode. The biggest problem is that different versions
of mingw generate differen export symbols for dll, so we must
remap them into symbols what the program imports, this can be
done with = in def file. So create a text file (file > new > txt
file) and name it dt.def Then copy the following into that file:
EXPORTS
attach
attach__FPUl=attach
detach
detach__Fv=detach
teach
teach__FPcUiPUlN32=teach
Then create makefile named mingw.mak and copy the following into
that file (replace \t with tab, this is *very* important):
CC= gcc
CFLAGS= -W -Wall -std=c89
OBJ= dthi.o
SRC= $(OBJ:.o=.c)
DLL= dthi.dll
DEF= dt.def
all: $(DLL)
$(DLL): $(OBJ)
\t$(CC) -shared -o $(DLL) $(OBJ) $(DEF)
$(OBJ): $(SRC)
An example dthi.c file is like this:
/* dthi.c */
#include <windows.h>
/* static variables remain intact between function calls */
static LPSTR instring, gather;
static INT noccount = 0;
/*
* called once in every cycle or once for every output
* character, you may just ignore 4 last parameters
*/
LPSTR teach (LPSTR outchar, UINT noc, ULONG* oldbegin,
ULONG* newbegin, ULONG* freebegin, ULONG* freelength)
{
if (noc == 3) /* number of characters per last cycle */
{
if (noccount == 3)
{
noccount = 0;
lstrcpy (gather, "");
}
lstrcat (gather, outchar); /* add to the end */
lstrcpy (instring, "");
noccount++; /* wait until last character in cycle */
if (noccount == 3 && lstrcmp (gather, "HI ") == 0)
lstrcpy (instring, "HI "); /* reaction to output */
}
else
lstrcpy (instring, "");
return instring;
}
/* called when dll is attached and when program starts */
VOID attach (ULONG* sknot)
{
/* allocate memory for strings and arrays */
instring = (LPSTR) GlobalAlloc (GPTR, 4);
gather = (LPSTR) GlobalAlloc (GPTR, 4);
lstrcpy (gather, "");
}
/* called when dll is detached and when program terminates */
VOID detach (VOID)
{
GlobalFree (instring);
GlobalFree (gather);
}
You can compile with:
redir -e make.txt mingw32-make -f mingw.mak all
In order to make it quickly, I used to write it in bat file
called build.bat. This generates make.txt where it writes
compiler errors. You can also use this file to go to the error
locations with vim, for that write cfile make.txt in vim command
mode. Then, cnext in vim command mode shows you the next error,
you can edit, save with ctrl-s (or w in command mode), and give
the command cnext again. Don't notice the warnings that some
parameters were not used, we didn't intend to use them. But it's
better that all warnings are on, as it sometimes may give a very
useful warning, what it shall not give otherwise.
This is all what I thought would be necessary to know about
compiling a training program. When you learn how to do that, it's
very fast and simple. if you have problems or questions, please
ask.
#nosmileys#nosmileys