Thread: Importing dll functions

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    20

    Exclamation Importing dll functions

    I'm trying to import a function exported by a dll. So far:

    Code:
    #pragma comment(lib, "hookHop.lib")
    
    extern "C"  void hhPostMessageA(HWND Hwn, UINT Msg, WPARAM WParam, LPARAM LParam); 
    extern "C"  LPARAM CursorPosTolParam(VOID);
    The lib and dll are in my project directory and my dll is in my debug directory. I get the following linker error when I compile:

    Code:
    main.obj : error LNK2019: unresolved external symbol _hhPostMessageA referenced in function "void __cdecl SendKey(char)" (?SendKey@@YAXD@Z)
    main.obj : error LNK2019: unresolved external symbol _CursorPosTolParam referenced in function "long __cdecl getCursPos(void)" (?getCursPos@@YAJXZ)
    Help?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to add the .lib to your library imports in the project linker settings.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    Umm I'm not 100% familiar with VC++ GUI, but I added it Tools->Options->Projects and Solutions->and added the folder that contains the lib, but it still didn't work.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Right click your project, and select "properties".

    From the tree view on the left you should see "Configuration properties". Expand that, then expand "Linker". Finally, click on "input". Add your .lib file to "additional dependencies".

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    I tried that and it didn't work. My lib file is referenced with #pragma and is in my current directory. This is so frustrating >.<

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not importing them.
    Easiest is to put those function names into a .def file in your DLL and use a header to see those functions. Then add a dependency to your DLL. It's found in your Solution Settings.

    http://i201.photobucket.com/albums/a...pendencies.jpg

    There's a sample image of how it's done to import ^
    It's under right-click solution -> properties.
    Last edited by Elysia; 12-07-2007 at 12:50 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    I do have those functions in a .def file(not my dll), so when I get home i'm gonna search google and see how to import from a .def file and try changing my solution properties instead of my project properties. thanks Elysia!!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to do that. Export your functions from a .dll using a .def file, then add a dependency to that project in the solution and you can use the functions as much as you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    The library was coded in asm. Someone told me the extern C isn't necessary. Is that right? Do i need to include anything else? I wish I was home so i could try all this right now xD

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why is the darn thing compiled in asm? That complicates things.
    If it exports the functions using assembly names (not inline assembly), then obviously it's going to be trickier. extern C will absolutely be necessary if the thing is done in asm.
    Just do tell - or maybe a sample - is the entire thing in asm or is the function declared as normal C++ functions with inline assembly?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    Yeah unfortunately the person who did it only uses assembly lol. Here's what's in the .def file:
    Code:
    LIBRARY  hookHop
    EXPORTS  CursorPosTolParam
    EXPORTS  hhPostMessageA
    he did release the source. here's hhPostMessageA's func defintion:

    Code:
    hhPostMessageA proc    hWnd:HWND, Msg:UINT, wParam:WPARAM, lParam:LPARAM
        cmp     dword ptr ds:[boolVersion], 01h
        jz      @f
        mov     edi, edi
        push    ebp
        mov     ebp, esp
        jmp     dword ptr ds:[adrPostMessage]
    
        @@:
        mov     dword ptr [eax], 01234h
        ret     
    hhPostMessageA endp

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't be sure, but that looks like __cdecl.
    I'm not an expert on calling asm functions, but here goes a try anyway.
    Declaration:
    Code:
    #ifdef PROJECT_IMPORT
    	#define DllExport extern "C" __declspec(dllimport)
    #else
    	#define DllExport extern "C" __declspec(dllexport)
    #endif
    DllExport DWORD __cdecl hhPostMessageA(HWND hWnd, UINT, Msg, WPARAM wParam, LPARAM lParam);
    Be sure to define PROJECT_IMPORT in your project to import the function and then call the asm function.
    I can't gaurantee anything, but I think it might do the trick.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Elysia's method is the standard way of using dllimport and dllimport. If you follow that pattern your code should link with no trouble.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    okay thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Importing DLL / Library Files
    By magic.mike in forum Windows Programming
    Replies: 5
    Last Post: 03-22-2005, 05:47 AM
  3. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  4. DLL's2
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-10-2002, 06:48 PM
  5. Function Calling From A DLL In a DLL
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 06-03-2002, 08:27 AM