Thread: Call procedure in DLL

  1. #1

    Call procedure in DLL

    Ok, I'm writing a generic viewer for my custom controls but I'm a little stuck ...

    The problem is that I don't know how to call a function which I obtained the location of with GetProcAddress() ...

    Code:
    FreeLibrary(lib);
           lib = LoadLibrary(fpath);
           FARPROC InitProc = GetProcAddress(lib,"InitClass");
    I gathered that was correct, but how do I now call InitProc and pass a variable (in this case it's GetModuleHandle(NULL) ...

    Cheers,
    Michael.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    InitProc(GetModuleHandle(NULL));

    gg

  3. #3
    I tried that and got - "too many arguments to function" ..

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    How is InitClass declared in the DLL, and how did you declare your function pointer to it?.

    gg

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Oh.....you declared it as "FARPROC".
    You need to declare InitProc as a pointer to a function that has the same signature (and calling convention) as InitClass.

    Read up function pointers here.

    Here's a basic example.

    Code:
    #include <iostream>
    using namespace std;
    
    int foo(int a)
    {
        cout << "foo(" << a << ")" << endl;
        return 0;
    }//foo
    
    int bar(int a)
    {
        cout << "bar(" << a << ")" << endl;
        return 0;
    }//bar
    
    // declare a type that can point to a function that returns int and takes a 
    // single int parameter
    typedef int (*fn_ptr_t)(int);
    
    int main()
    {
        fn_ptr_t fnptr = NULL;
    
        fnptr = foo; // "fnptr = &foo" is ok too
    
        fnptr(1); // calls foo(1)
    
        fnptr = &bar;
    
        fnptr(2); // calls bar(2)
    	
    	return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create DLL file in C language and call from C and VB
    By karthickbabu in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 07:50 AM
  2. need help converting c dll to cpp
    By *DEAD* in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 10:22 PM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. Function Call From DLL
    By (TNT) in forum Windows Programming
    Replies: 5
    Last Post: 05-05-2002, 09:33 PM