Importing dll functions

This is a discussion on Importing dll functions within the C++ Programming forums, part of the General Programming Boards category; I'm trying to import a function exported by a dll. So far: Code: #pragma comment(lib, "hookHop.lib") extern "C" void hhPostMessageA(HWND ...

  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,246
    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,246
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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-06-2007 at 11:50 PM.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    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, 09:09 AM
  2. Importing DLL / Library Files
    By magic.mike in forum Windows Programming
    Replies: 5
    Last Post: 03-22-2005, 04: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

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