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

bool parameter problem in DLL function

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

Jun 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 15 Jun, 15:07, "k.sahici" <k.sah...@gmail.comwrote:
__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

Jun 15 '07 #2

P: n/a
On 15 Jun, 16:06, dasjotre <dasjo...@googlemail.comwrote:
typedef void(*pICFUNC) (lpfnGetProcessID);
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

Jun 15 '07 #3

P: n/a
k.sahici wrote:
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.
Jun 15 '07 #4

P: n/a
On Jun 15, 6:16 pm, dasjotre <dasjo...@googlemail.comwrote:
On 15 Jun, 16:06, dasjotre <dasjo...@googlemail.comwrote:
typedef void(*pICFUNC) (lpfnGetProcessID);

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?

Jun 18 '07 #5

P: n/a
On 18 Jun, 07:52, "k.sahici" <k.sah...@gmail.comwrote:
On Jun 15, 6:16 pm, dasjotre <dasjo...@googlemail.comwrote:
On 15 Jun, 16:06, dasjotre <dasjo...@googlemail.comwrote:
typedef void(*pICFUNC) (lpfnGetProcessID);
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?
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

Jun 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.