winapi - Calling Win32 DLL from C++ -
i new dll world. have been given win32 dll has lot of functions. need call these dll functions c++
i want call createnewscanner
creates new scanner object , results in c++. function mentioned in dll is:
bool createnewscanner(newscanner *newscan);
and newscanner
struct
, below,
// structure newscanner defined in "common.h" . typedef struct{ byte host_no; // <- host_no =0 long time; // <- command timeout (in seconds) byte status; // -> host adapter status handle obj; // -> object handle scanner }newscanner;
how call function? started c++ , here managed,
#include <iostream> #include <windows.h> using namespace std; int main(){ hinstance hinstance; if(!(hinstance=loadlibrary("winscanner.dll"))){ cout << "could not load library" << endl; } /* pointer function in dll*/ farproc handle = getprocaddress(hmodule(hinstance), "createnewscanner"); if(!handle){ // handle error freelibrary(hinstance); return "-1"; }else{ // call function //how call here?? } }
first of all, return "-1"
no good. expected return integer. surely mean return -1
.
now question. instead of declaring function pointer farproc
, it's easier declare function pointer type.
typedef bool (*createnewscannerproc)(newscanner*);
then call getprocaddress this:
hmodule hlib = loadlibrary(...); // loadlibrary returns hmodule , not hinstance // check hlib null createnewscannerproc createnewscanner = (createnewscannerproc) getprocaddress(hlib, "createnewscanner"); if (createnewscanner == null) // handle error // can call function newscanner newscan; bool retval = createnewscanner(&newscan);
having said of that, library come header file (yours should include it) , .lib file load-time linking. make sure pass .lib file linker , can this:
#include "nameoftheheaderfilegoeshere.h" .... newscanner newscan; bool retval = createnewscanner(&newscan);
no need mess around loadlibrary
, getprocaddress
, on.
Comments
Post a Comment