From: Dmitry B. <db...@ma...> - 2001-10-23 16:53:51
|
"John Hanna" <jh...@cp...> writes: > Ok, so now I'm trying to write a dll (for Mercury) that uses a dll (perlez). > I got the thing to compile into a dll, but it's working. Basically I need to > export a single function, daemon(). I wrote this test program: > > #include <stdio.h> > #include <windows.h> > > HINSTANCE dllHandle; > #define debug(args...) printf(args) > #define d(r,t,p,args...) typedef r (CALLBACK* t)(args); t p; > d(short,daemont,daemonp,int *a,int *b, char *address, char *par); > int main(int argc, char *argv[]) > { > debug("starting\n"); > dllHandle = LoadLibrary("perleztst.dll")); > if(! dllHandle) { > debug("Couldn't load perlez.dll"); > exit(-1); > } > debug("mapping\n"); > #define map(t,p,f) \ > if(!(p=(t)GetProcAddress(dllHandle, f))) \ > {debug("couldn't find function " f "\n"); exit(-1);} \ > else debug(f " has address %lx\n",p); > map(daemont,daemonp,"daemon"); IMHO that is not the very good programming style. If you're going to use "daemon" function from "perleztst.dll", you should use the same headers for building the DLL and for using it (something like perleztst.h). Otherwise you cannot be sure that you are using DLL's functions properly (correct parameters etc.) Show me the actual daemon() definition, and I think I will tell you where is the bug :-) > debug("calling\n"); > daemonp(NULL,NULL,"address","param"); > debug("done\n"); > return 0; > } > > (note that 614c10f0 is the value of the pointer daemonp.) > > It goes fine until Calling, then gives "This program has performed an > illegal operation" > PEZTST caused an invalid page fault in module PERLEZTST.DLL at > 0177:614c110e. > Registers: > EAX=00000000 CS=0177 EIP=614c110e EFLGS=00010202 > EBX=614c10f0 SS=017f ESP=0253f950 EBP=0253fda8 > ECX=0000001d DS=017f ESI=81944398 FS=21d7 > EDX=bffc9490 ES=017f EDI=00000000 GS=0000 > Bytes at CS:EIP: > 80 78 04 01 7e 18 a1 10 30 4c 61 80 78 04 02 75 > Stack dump: > 614c10f0 bff7a3a0 818ba000 819457a4 00073860 00000000 818ba00c 818ba000 > 819455b8 bff713ee 00000177 bff7eaf9 000614c0 00000001 00000009 05520000 > > In perleztst.dll daemon is declared like this: > short _export daemon (void *job, M_INTERFACE *m, char *address, char > *parameter) Oh yes. Your are using daemon() as short CALLBACK daemon(int *a,int *b, char *address, char *par); i.e. short __stdcall daemon(int*,int*,char*,char*); but in fact it's defined as short daemon(void*,M_INTERFACE*,char*,char*) At least calling conventions do not match. > How do I figure out what's wrong with this? > > By the way, the libperlez.a worked once and then stopped working, so I'm > loading the subs manually. It's not a big deal, so I don't care if it > works. It's a pity that you didn't care. Obviously it was your bug. You had to fix it, not work around it :-) Hope to hear from you soon, Dmitry |