DLLs and C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firecore
    New Member
    • Jul 2007
    • 114

    #1

    DLLs and C

    Does anyone know how to call DLL functions using C?
    eg. PlaySound
    MessageBox
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    1) Call LoadLibrary() to load the DLL.
    2) Call GetProcAddress( ) to get the address of your function from the DLL. You will need the HMODULE from LoadLibrary() and a string with the function name. This address will be returned as a FARPROC.
    3) Declare a function pointer of the correct arguments and return type.
    4) Assign the FARPROC to this function pointer using a typecast.
    5) Call your function using the pointer.
    6) Call FreeLibrary() when you are done with the DLL and want to unload it. Use the HMODULE from LoadLibrary()

    Comment

    • Firecore
      New Member
      • Jul 2007
      • 114

      #3
      Could you please explain a bit more?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Firecore
        Could you please explain a bit more?
        I don't even speak Microsoft C, and even I could understand what WFC's
        explanation is all about:

        [code=C]
        // 1) Call LoadLibrary() to load the DLL.
        HMODULE myLib= LoadLibrary("my DLL" /* maybe more params here */);

        // 2) Call GetProcAddress( ) to get the address of your function from the DLL.
        // You will need the HMODULE from LoadLibrary() and a string with the function
        // name. This address will be returned as a FARPROC.
        FARPROC myAddr= LoadLibrary(myL ib, "myFunc");

        // 3) Declare a function pointer of the correct arguments and return type.
        typedef int (*foo_t)(double ); // or whatever

        // 4) Assign the FARPROC to this function pointer using a typecast.
        foo_t foo= (foo_t)myAddr;

        // 5) Call your function using the pointer.
        printf("%d\n", foo(42.54);

        // 6) Call FreeLibrary() when you are done with the DLL and want to unload it.
        // Use the HMODULE from LoadLibrary()
        FreeLibrary(myL ib);
        [/code]

        kind regards,

        Jos

        Comment

        • Firecore
          New Member
          • Jul 2007
          • 114

          #5
          Originally posted by JosAH
          I don't even speak Microsoft C, and even I could understand what WFC's
          explanation is all about:
          Oh Ok............. .
          Maybe you coulkd but I could not.
          I guess I need a lot more work eh?
          And is Microsoft C the same as Turbo C from Borland?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Firecore
            Oh Ok............. .
            Maybe you coulkd but I could not.
            I guess I need a lot more work eh?
            And is Microsoft C the same as Turbo C from Borland?
            I guess not; that LoadLibrary() function is a Microsoft-ism as far as I know; If the
            Borland folks are a bit clever they'd use the POSIX standard dlopen(), dlclose()
            and dlsym() method names.

            kind regards,

            Jos

            Comment

            Working...