Re: using a DLL function in a header file



Hi Tom,

Thanks for the help. But I have this code you suggest already in the
Form1.cpp file :

typedef void (_stdcall *MyOut32)(short,short);
typedef short (_stdcall *MyInp32)(short);

HINSTANCE hinstLib;
MyInp32 ProcAdd1;
MyOut32 ProcAdd2;
//LoadLibrary loads the DLL which is in the systems folder & in the same
folder as qq.exe
hinstLib = LoadLibrary("Inpout32.dll");
//the Inp32 & Out32 functions defined in the DLL are now called ProcAdd1 &
ProcAdd2
ProcAdd1 = (MyInp32) GetProcAddress(hinstLib, "Inp32");
ProcAdd2 = (MyOut32) GetProcAddress(hinstLib, "Out32");

(ProcAdd2)(0x378, 0);


The only difference is all this code except for the last line :
(ProcAdd2)(0x378, 0);
is placed in the Form1.cpp, while the last line is placed in the event
handler of a button function which is located in Form1.h

I don't understand why the compiler understands the creation of ProcAdd2,
yet complains about it's use in the header file ??

AK

"Tamas Demjen" wrote:

> You can't use the header file directly. You have to define a function
> pointer type, and then create a function pointer yourself. For example,
> if your function's prototype is
>
> int Proc2(int, int);
>
> Then you have to do the following:
>
> typedef int (*PtrProc2)(int, int);
> PtrProc2 Proc2 = (PtrProc2)GetProcAddress(DllHandle, "Proc2");
> int result = Proc2(0x378, 12);
>
> Tom
>
> AK wrote:
> > I'm using a .NET Windows Forms Applications project.
> > I'm using LoadLibrary & GetProcAddress to use a DLL function.
> > This is all done in the main cpp file.
> > I would like to use this DLL function in a .h file. So I code :
> > (Proc2) (0x378, 12)....... (where Proc2 is the function)
> > I get a compiler error : C2065 : 'Proc2' : undeclared identifier.
> > I'm guessing I'm not calling this function properly - how can I fix this
> > problem ?
> >
> > When I write a program that contains a main cpp file that loads the DLL
> > function &
> > defines Proc2 via GetProcAddress, & finally uses it in this same file, then
> > Proc2 runs correctly.
> > So I'm guessing when this code is split into a cpp & a .h file, I need to
> > call Proc2 differently ??
> > The reason I'm splitting the use of the Proc2 function into a header file,
> > is back that's where the event hamdler for a button click from the GUI is
> > located.
> > Any advice is appreciated.
> > Thanks,
> > AK
>
.