bool parameter problem in DLL function

K

k.sahici

I have a DLL written in C++ which has a function shown below.

/**********************************
//MYDLL.cpp

__declspec(dllexport)
void Pos(MyStruc &r)
{
r.var = true;
}
/**********************************

and I export it in Export.def
/**********************************
LIBRARY MYDLL
EXPORTS
Pos
/**********************************

MyStruc is also declared as
/**********************************
Struc MyStruc{
bool var;
MyStruc( ){
var = false;
};
/**********************************
As you see above, MyStruc has a default constructor which sets var to
false.

When I use this dll in another C++ source file as

/**********************************
HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
"Pos");

if (lpfnGetProcessID == NULL)
{
return;
}
typedef void(*pICFUNC) (lpfnGetProcessID);

pICFUNC pfunc = pICFUNC(lpfnGetProcessID);

MyStruc r;

pfunc(r);
/**********************************
This code(in other words, Pos function in MYDLL.dll) is supposed to
change r.var to true, but somehow it doesn't and when I check its
value I see that it's still false. When I change the default
constructor to set var to true and change the Pos function to set
r.var to false, I see that var is still true.

To sum up, the default constructor overwrites what my Pos(MyStruc &r )
does. The interesting thing is, I write the DLL and the program which
uses this DLL in the same programming language and with the same IDE,
Visual C++ 6.0. I should also point out that I change none of the
project settings. I think it is mainly about using bool type in a DLL.

Do you have any idea about this weird behavior?
 
D

dasjotre

__declspec(dllexport)
void Pos(MyStruc &r)
{
r.var = true;}
HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
"Pos");

i think that HMODULE is just an alias
for HINSTANCE, no need to cast
if (lpfnGetProcessID == NULL)
{
return;}

typedef void(*pICFUNC) (lpfnGetProcessID);

so pICFUNC is a pointer to a function returning void
with one argument of type FARPROC. like:

void weird_fun(FARPROC);
pICFUNC pfunc = pICFUNC(lpfnGetProcessID);

typedef void (*pICFUNC)(MyStruc &);
pICFUNC pfunc = (pICFUNC)(lpfnGetProcessID);
MyStruc r;

pfunc(r);

I don't believe this should compile in the first place.
you can't convert MyStruc to FARPROC
Do you have any idea about this weird behavior?

it is called 'undefined behaviour'

regards

DS
 
D

dasjotre

ups almost missed this one!
surely that won't compile, lpfnGetProcessID is not a type

VC6 is very old and not even supported by MS any more.
you can download free community edition of VC8 from
MS. also, using lib files in conjunction with DLLs makes
it much easier than LoadLibrary/GetProcAddress and
you can't make the kind of the errors you made.

regards

DS
 
G

Glen Dayton

k.sahici said:
I have a DLL written in C++ which has a function shown below.

/**********************************
//MYDLL.cpp

__declspec(dllexport)
void Pos(MyStruc &r)
{
r.var = true;
}
/**********************************

and I export it in Export.def
/**********************************
LIBRARY MYDLL
EXPORTS
Pos
/**********************************

MyStruc is also declared as
/**********************************
Struc MyStruc{
bool var;
MyStruc( ){
var = false;
};
/**********************************
As you see above, MyStruc has a default constructor which sets var to
false.

When I use this dll in another C++ source file as

/**********************************
HINSTANCE hGetProcIDDLL = LoadLibrary("MYDLL.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL),
"Pos");

if (lpfnGetProcessID == NULL)
{
return;
}
typedef void(*pICFUNC) (lpfnGetProcessID);

pICFUNC pfunc = pICFUNC(lpfnGetProcessID);

MyStruc r;

pfunc(r);
/**********************************
This code(in other words, Pos function in MYDLL.dll) is supposed to
change r.var to true, but somehow it doesn't and when I check its
value I see that it's still false. When I change the default
constructor to set var to true and change the Pos function to set
r.var to false, I see that var is still true.

To sum up, the default constructor overwrites what my Pos(MyStruc &r )
does. The interesting thing is, I write the DLL and the program which
uses this DLL in the same programming language and with the same IDE,
Visual C++ 6.0. I should also point out that I change none of the
project settings. I think it is mainly about using bool type in a DLL.

Do you have any idea about this weird behavior?

This is really a C++ newsgroup, but maybe someday C++ will have
standard features for shareable and dynamic link libraries.

Check that you're also exporting the entire MyStruc class. If
you're merely defining it in a header the Microsoft DLL and the
application that loads the DLL will have their own copies rather
than a common copy.
 
K

k.sahici

ups almost missed this one!
surely that won't compile, lpfnGetProcessID is not a type

VC6 is very old and not even supported by MS any more.
you can download free community edition of VC8 from
MS. also, using lib files in conjunction with DLLs makes
it much easier than LoadLibrary/GetProcAddress and
you can't make the kind of the errors you made.

regards

DS

First of all, I'd like to thank all of you for your replies.

I've just realized that I've mistyped my question.
typedef void(*pICFUNC) (lpfnGetProcessID); //I know this is wrong and
actually I wrote
typedef void(*pICFUNC) (MyStruc &); in my source file

However, last time I checked it, I saw it was working. But I'm sure it
was not working when I posted this question a few days ago. Is it
possible that there is undefined behavior about using bool parameter
type in a dll function?
 
D

dasjotre

First of all, I'd like to thank all of you for your replies.

I've just realized that I've mistyped my question.
typedef void(*pICFUNC) (lpfnGetProcessID); //I know this is wrong and
actually I wrote
typedef void(*pICFUNC) (MyStruc &); in my source file

However, last time I checked it, I saw it was working. But I'm sure it
was not working when I posted this question a few days ago. Is it
possible that there is undefined behavior about using bool parameter
type in a dll function?

It is possible that there is a bug in your compiler causing this. My
experience is that whenever I try to blame the compiler it turns out
that the mistake was mine :)

regards.

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top