January 9th, 2003, 05:42 AM
Running a Object file
I am working on Turbo C for DOS .
First I have made a sample.c which contains sample() function and i have generated the objected file sample.obj.
In another file ( test.c) i have opened this file with fopen and this address is casted to funcp function pointer and called the that function pointer.
Is this the correct way of doing? Is this the way how DLLs are work in core level ( Actually i dont know how DLLs work in background )
Following is the my source code
First File :
Second File :
fp = fopen("sample.obj","rb");
if ( !fp )
printf("File Cannot be opened");
funcp = ( void (*)()) fp;
anyway this program is running but going in infinite loop
Please clarify me .
January 9th, 2003, 11:09 AM
I am not sure if I fully understand your question.
If I get it right, you're asking how to call a function in an object file. I have no knowledge of Turbo C, but I think you should specify the name of the object file on the command line. I assume Turbo C offers a flag through which the linker knows what object file(s) to include in the executable. Perhaps it has the form of something like this:
dosprompt> [name of your compiler] [your C source].c -l[name of the object file]
(check the Turbo C documentation for the correct notation).
This means that you can simply call sample() in your main(), without the need to cast it to a function pointer first.
Hope this helps.
January 9th, 2003, 04:31 PM
Well, you do have the general idea correctly. However, I don't think that DOS ever supported the idea of DLLs. IIRC, the closest that it came to was using overlay functions. Search google if you want more information on overlay functions. Frankly, if you're just learning C, it's probably easier to just statically link the two files together like the previous poster mentioned. That way, you don't have to indirect through a pointer.
As to why your code is hanging, you aren't really reading the code from the file into memory, so you can't indirect to the function via the file pointer. You probably need to read the file first into a memory buffer and then point the pointer to it. Also, an OBJ file contains machine code, but it also contains some stub headers for the linker to use, so you can't arbitrarily point a function pointer to the first byte of the OBJ file buffer, you need to find the area in the file where the function's code starts. Then again, like I said above, DOS never really supported DLLs, so you might want to see how it is done under Windows or *Nix. The reason why most people used overlays was because DOS was limited to 640K and when you're using overlays, you could unload the overlay library when you were done using it.
Under Windows or *Nix, the process is pretty much close to what you described. In Windows, it would go something like this:
Under *nix, the logic for shared libraries is similar and the functions corresponding to LoadLibrary, GetProcAddress and FreeLibrary are dlopen(), dlsym() and dlclose(). Hope this helps!
/* Load the library into memory */
HINSTANCE *hLib = LoadLibrary("mylib");
/* Find address of the function we want, in the dynamic library */
pFunc = GetProcAddress(hLib, "MyFunctionName");
/* Execute the function */
/* Free the library */
Last edited by Scorpions4ever; January 9th, 2003 at 04:35 PM.