#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Hyderabad
    Posts
    17
    Rep Power
    0

    Running a Object file


    Hi,

    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 :

    #include <stdio.h>
    void main()
    {
    printf("Hai Suri");
    return;
    }

    Second File :

    #include <stdio.h>

    FILE *fp;

    void main()
    {
    void (*funcp)();
    fp = fopen("sample.obj","rb");
    if ( !fp )
    printf("File Cannot be opened");
    else
    {
    funcp = ( void (*)()) fp;
    funcp();
    }
    fclose(fp);
    }

    anyway this program is running but going in infinite loop


    Please clarify me .
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    18
    Rep Power
    0
    Hi,

    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.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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:
    Code:
    int (*pFunc)();
    /* 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 */
    if (pFunc)
       (*pFunc)();
    
    /* Free the library */
    FreeLibrary(hLib);
    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!
    Last edited by Scorpions4ever; January 9th, 2003 at 03:35 PM.

IMN logo majestic logo threadwatch logo seochat tools logo