Importing dll functions
I'm trying to import a function exported by a dll. So far:
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:
#pragma comment(lib, "hookHop.lib")
extern "C" void hhPostMessageA(HWND Hwn, UINT Msg, WPARAM WParam, LPARAM LParam);
extern "C" LPARAM CursorPosTolParam(VOID);
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)
You need to add the .lib to your library imports in the project linker settings.
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.
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".
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 >.<
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.
There's a sample image of how it's done to import ^
It's under right-click solution -> properties.
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!!
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.
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
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?
Yeah unfortunately the person who did it only uses assembly lol. Here's what's in the .def file:
he did release the source. here's hhPostMessageA's func defintion:
hhPostMessageA proc hWnd:HWND, Msg:UINT, wParam:WPARAM, lParam:LPARAM
cmp dword ptr ds:[boolVersion], 01h
mov edi, edi
mov ebp, esp
jmp dword ptr ds:[adrPostMessage]
mov dword ptr [eax], 01234h
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.
Be sure to define PROJECT_IMPORT in your project to import the function and then call the asm function.
#define DllExport extern "C" __declspec(dllimport)
#define DllExport extern "C" __declspec(dllexport)
DllExport DWORD __cdecl hhPostMessageA(HWND hWnd, UINT, Msg, WPARAM wParam, LPARAM lParam);
I can't gaurantee anything, but I think it might do the trick.
Elysia's method is the standard way of using dllimport and dllimport. If you follow that pattern your code should link with no trouble.