Problem with GetProcAddress
-
Hey Guys, I got a piece of Unix Code but I have to make it run under Windows. Unfortunately I contains the function mmap (a Unix Function). Fortunately there is this nice DLL called cygwin1.dll which gives you some Unix Functions.
HINSTANCE hDll = LoadLibrary("cygwin1.dll"); if(hDll) { printf("DLL Loaded !\r\n"); FARPROC mmap = GetProcAddress(hDll,"mmap"); if(mmap) { printf("Function found !\r\n"); mydata= mmap(mydata, myLength,PROT_READ,MAP_PRIVATE,fd,0); } else { printf("Function not found!\r\n"); } } else { printf("Failed to load DLL!\r\n"); }
This is the code I use. But when I then try to call the mmap function my compiler tells me that I'm passing to many arguments to mmap. :wtf: Normally I don't load libraries at runtime. So please help me :sigh: -
Hey Guys, I got a piece of Unix Code but I have to make it run under Windows. Unfortunately I contains the function mmap (a Unix Function). Fortunately there is this nice DLL called cygwin1.dll which gives you some Unix Functions.
HINSTANCE hDll = LoadLibrary("cygwin1.dll"); if(hDll) { printf("DLL Loaded !\r\n"); FARPROC mmap = GetProcAddress(hDll,"mmap"); if(mmap) { printf("Function found !\r\n"); mydata= mmap(mydata, myLength,PROT_READ,MAP_PRIVATE,fd,0); } else { printf("Function not found!\r\n"); } } else { printf("Failed to load DLL!\r\n"); }
This is the code I use. But when I then try to call the mmap function my compiler tells me that I'm passing to many arguments to mmap. :wtf: Normally I don't load libraries at runtime. So please help me :sigh:Hey, You're getting a function pointer back, but GetProcAddress can't possibly know what the function definition of mmap is so you have to tell it. Assuming that you are using the results from "man mmap"
#include < sys/mman.h >
void* mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
then you would do something like this
#include < windows.h >
typedef void* (CALLBACK* PFNMMAP)(void*, size_t, int, int, int, off_t);
//blah -- open the DLL, etc
PFNMMAP pmmap;
if( (pmmap = GetProcAddress(dllHandle, "mmap"))) {
//do something here
}
now pmmap is a function pointer with the right signature -- ie of type PFNMMAP. Oh yes, modify CALLBACK based on the way the dll export table was declared (it should work for __declexport). Also, there are two types of libs on windows. One, called a .lib, is code made to be statically linked into a file. The other, for your convenience also called a .lib, is code that is statically linked at compile time but just thunks to a dll. The latter is often more convenient as it will handle the LoadLibrary and GetProcAddress stuff for you. The downside to using import libs is that windows tries to load the dll before your main / WinMain is called. If it can't find the dll you never get control. At least with manual LoadLibrary calls you can be intelligent about telling the user just what you can't find and what he or she ought to do about it. PS: I don't have a compiler on this computer so I can't promise any of this works. HTH earl
-
Hey Guys, I got a piece of Unix Code but I have to make it run under Windows. Unfortunately I contains the function mmap (a Unix Function). Fortunately there is this nice DLL called cygwin1.dll which gives you some Unix Functions.
HINSTANCE hDll = LoadLibrary("cygwin1.dll"); if(hDll) { printf("DLL Loaded !\r\n"); FARPROC mmap = GetProcAddress(hDll,"mmap"); if(mmap) { printf("Function found !\r\n"); mydata= mmap(mydata, myLength,PROT_READ,MAP_PRIVATE,fd,0); } else { printf("Function not found!\r\n"); } } else { printf("Failed to load DLL!\r\n"); }
This is the code I use. But when I then try to call the mmap function my compiler tells me that I'm passing to many arguments to mmap. :wtf: Normally I don't load libraries at runtime. So please help me :sigh:How many parameter in this function
mmap
and usemmap
with correct parameter_**
**_
whitesky