Call procedure in DLL

This is a discussion on Call procedure in DLL within the Windows Programming forums, part of the Platform Specific Boards category; Ok, I'm writing a generic viewer for my custom controls but I'm a little stuck ... The problem is that ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    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,702
    InitProc(GetModuleHandle(NULL));

    gg

  3. #3
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    I tried that and got - "too many arguments to function" ..

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    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,702
    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, 08:50 AM
  2. need help converting c dll to cpp
    By *DEAD* in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 11:22 PM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 08:26 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 06:24 AM
  5. Function Call From DLL
    By (TNT) in forum Windows Programming
    Replies: 5
    Last Post: 05-05-2002, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21