Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bool parameter problem in DLL function

Reply
Thread Tools

bool parameter problem in DLL function

 
 
k.sahici
Guest
Posts: n/a
 
      06-15-2007
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?

 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      06-15-2007
On 15 Jun, 15:07, "k.sahici" <(E-Mail Removed)> wrote:
> __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

 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      06-15-2007
On 15 Jun, 16:06, dasjotre <(E-Mail Removed)> wrote:
> > 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

 
Reply With Quote
 
Glen Dayton
Guest
Posts: n/a
 
      06-15-2007
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.
 
Reply With Quote
 
k.sahici
Guest
Posts: n/a
 
      06-18-2007
On Jun 15, 6:16 pm, dasjotre <(E-Mail Removed)> wrote:
> On 15 Jun, 16:06, dasjotre <(E-Mail Removed)> wrote:
>
> > > 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?

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      06-18-2007
On 18 Jun, 07:52, "k.sahici" <(E-Mail Removed)> wrote:
> On Jun 15, 6:16 pm, dasjotre <(E-Mail Removed)> wrote:
>
>
>
> > On 15 Jun, 16:06, dasjotre <(E-Mail Removed)> wrote:

>
> > > > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
problem in passing parameter to dll function andy C++ 4 02-21-2004 05:25 PM
Pass bool parameter to web method Baohua Li ASP .Net Web Services 3 01-20-2004 07:18 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments