Results 1 to 11 of 11

Thread: Calling a Dll

  1. #1
    DaoK
    Guest

    Calling a Dll

    Hello!
    I made a dll :

    PHP Code:
    #include "MYDLL.h"
    __declspecdllexport int doubleInt(int iNumber)
    {
        return (
    iNumber 2);

    and a .DEF
    Code:
    LIBRARY        MYDLL
       DESCRIPTION    "Simply double an integer"
       EXPORTS
                doubleInt  @1
    but now that's what I just found in some thread here but that doesn't work and I know that I miss something but I really do not have a clue how to call my DLL in an other C++ project

    Here is what I got :
    PHP Code:
    #include <iostream>

    //Prototype
    void LoadDll(void);

    void main(void)
    {
        
    LoadDll();
    }
    //End main

    void LoadDll(void)
    {
        
    HMODULE hDLL LoadLibrary("MYDLL");
        if (
    hDLL)
            
    MYFUNC function = (MYFUNC)GetProcAddress(hDLL"MyFunction");
        else
            
    std::cout << "Error loading DLL";
    }
    //End LoadDll 

    Any one can help me ?

  2. #2
    DaoK
    Guest
    Here is all error :
    Code:
    error C2065: 'HMODULE' : undeclared identifier
    
    error C2146: syntax error : missing ';' before identifier 'hDLL'
    
    error C2065: 'hDLL' : undeclared identifier
    
    error C2065: 'LoadLibrary' : undeclared identifier
    
    error C2065: 'MYFUNC' : undeclared identifier
    
    error C2146: syntax error : missing ';' before identifier 'function'
    
    error C2065: 'function' : undeclared identifier
    
    error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
    
    error C2065: 'GetProcAddress' : undeclared identifier
    
    error C2181: illegal else without matching if

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Try this...

    Your DLL source file:
    PHP Code:
    #include "MYDLL.h"
    int __declspecdllexport doubleInt(int iNumber)
    {
        return (
    iNumber 2);

    Your .DEF file:
    PHP Code:
    LIBRARY        "MYDLL.dll"
       
    DESCRIPTION    "Simply double an integer"
       
    EXPORTS
                doubleInt 
    Almost make sure your DLL header file is also right.

    Now, the code you're testing your DLL with:
    PHP Code:
    #include <iostream>
    #include <windows.h> 'Must if you're using LoadLibrary, etc
    //Prototype
    void LoadDll(void);
    //Must declare a function pointer type you want
    typedef void (*MYFUNCTION)(int);
    void main(void)
    {
        
    LoadDll();
    }
    //End main

    void LoadDll(void)
    {
    'HMODULE is old.  Use HINSTANCE instead
        HINSTANCE hDLL = LoadLibrary("MYDLL.dll"); '
    Extension
        
    if (hDLL)
          
    MYFUNCTION myfunc= (MYFUNCTION)GetProcAddress(hDLL"MyFunction"); //Shouldn't it be doubleInt?
    //You can free the DLL here just for performance
    //FreeLibrary(hDLL);
        
    else
            
    std::cout << "Error loading DLL";
    }
    //End LoadDll 
    Make sure the path to DLL is correct and naming of all the files is also correct.
    Might be some typos in the above code though.
    Baaaaaaaaah

  4. #4
    DaoK
    Guest
    Yes but how can i use the doubleInt function ?

    I tryed that in the main :

    PHP Code:
    void main(void)
    {
        
    LoadDll();
        
    int a 5;
        
    myfunc(a);
        
    std::cout << a;
    }
    //End main 
    but that doesn't work

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You have to assign it to a function pointer, then call through the pointer:
    Code:
    (*myfunc)(a);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    DaoK
    Guest
    Why that doesn't work ?

    PHP Code:
    #include <iostream>
    #include <windows.h> //Must if you're using LoadLibrary, etc
    //Prototype
    void LoadDll(void);
    //Must declare a function pointer type you want
    typedef int (*MYFUNCTION)(int);
    void main(void)
    {
        
    LoadDll();
        
    int a 5;
        
    = (*myfunc)(a);
        
    std::cout << a;
    }
    //End main

    void LoadDll(void)
    {
        
    HINSTANCE hDLL LoadLibrary("deuxDll.dll");
        if (
    hDLL)
          
    MYFUNCTION myfunc = (MYFUNCTION)GetProcAddress(hDLL"doubleInt");
    //You can free the DLL here just for performance
    //FreeLibrary(hDLL);
        
    else
            
    std::cout << "Error loading DLL";
    }
    //End LoadDll 
    Error:
    Code:
    error C2065: 'myfunc' : undeclared identifier
    
    error C2100: illegal indirection

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Declare "myfunc" on the top of your code.
    PHP Code:
    #include <iostream>
    #include <windows.h> //Must if you're using LoadLibrary, etc
    //Prototype
    void LoadDll(void);
    //Must declare a function pointer type you want
    typedef int (*MYFUNCTION)(int);
    MYFUNCTION myfunc//Declare it
    void main(void)
    {
        
    LoadDll();
        
    int a 5;
        
    = (*myfunc)(a);
        
    std::cout << a;
    }
    //End main

    void LoadDll(void)
    {
        
    HINSTANCE hDLL LoadLibrary("deuxDll.dll");
        if (
    hDLL)
          
    myfunc = (MYFUNCTION)GetProcAddress(hDLL"doubleInt");
    //You can free the DLL here just for performance
    //FreeLibrary(hDLL);
        
    else
            
    std::cout << "Error loading DLL";
    }
    //End LoadDll 
    Last edited by abdul; Jul 16th, 2002 at 03:02 PM.
    Baaaaaaaaah

  8. #8
    DaoK
    Guest
    Ok but I have a speacial msgbox with an error when I execute, maybe I have an error when is time to load the DLL in the pointer.

    ...thx for debugging me I appreciate.

  9. #9
    DaoK
    Guest
    When I pass the program in debug I have RED hex code in the debugger screen... I think I do something wrong

  10. #10
    DaoK
    Guest
    Ok I found out what was wrong THX all!!!!

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by DaoK
    When I pass the program in debug I have RED hex code in the debugger screen... I think I do something wrong
    It's highlighted in red if the value's changed


    Everything working now?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width