By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,357 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,357 IT Pros & Developers. It's quick & easy.

using a DLL function in a header file

P: n/a
AK

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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
AK
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

Nov 17 '05 #3

P: n/a
AK
Tom,

Yes - I understand & have changed this. I have created a new class
'parallelport' that I intend to use inside the Form1 class (specifically
inside the function for the event handler of radial button #1). I wrote this
class in a new header file : "parallel port class.h" . It contains a single
member function 'setup' that defines ProcAdd2. I include this parallel port
class.h file in the Form1.h file, and this should give accessability of
parallelport class methods & variables to the Form1 class, right ?

Next, in Form1.h, I create an instance of the parallelport class called
'pp' & use pp.setup to define ProcAdd2 inside the Form1.h class. I still get
ProcAdd2 as undeclared identifier and identifier not found; compiler errors
C2065 & C3861. I have tried different codes & this code gives the least
amount of trouble & is one which makes sense to me, but I still can't execute
the simple instruction :
(ProcAdd2) (0x378, 0);

Below is the part of the code - I guess we can't attach a project file in
these discussions . I can't find the problem - maybe there is a better way
to implement this event handler ?

Regards,
Arie

****************parallel port class.h :

#include "stdafx.h"
#include <windows.h>
#include "Windows.h"
typedef void (_stdcall *MyOut32)(short,short);
typedef short (_stdcall *MyInp32)(short);

class parallelport
{
public :
void setup(void)
{
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");
}

private :
MyInp32 ProcAdd1;
MyOut32 ProcAdd2;

};
***************inside Form1.h :

#include "parallel port class.h"

parallelport pp;

public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
....

private: System::Void radioButton1_Click(System::Object * sender,
System::EventArgs * e)
{
bool r1=radioButton1->Checked;
bool r2=radioButton2->Checked;

// X in & Y in leads to 0000 0000 out the parallel port
if (r1==0 && r2==0)
{
pp.setup();
(ProcAdd2)(0x378, 0);}


"Tamas Demjen" wrote:
AK wrote:
I don't understand why the compiler understands the creation of ProcAdd2,
yet complains about it's use in the header file ??


You've put the declaration in the .cpp file, but use the type/variable
from the .h file. It should be the other way around. Just put these in
the header file:

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

// private data members:
MyInp32 ProcAdd1;
MyOut32 ProcAdd2;

The declaration must go before the usage.

Tom

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.