473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

bool parameter problem in DLL function

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
5 1711
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
3
by: Mochuelo | last post by:
Hi, In the constructor of a class, I am taking as a parameter the reference to a "bool", as in this code: ----------------------- public class TCP_synch_client { public string address...
2
by: tim | last post by:
We've started to use a coding standards checker at work. The following code results in a warning: 38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool bSaveLast) 39: ...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have the method below that returns a bool, true or false depending on if the conversion to date tiem works. It takes a string input. I am only returning the bool but would also like to...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
15
by: ssylee | last post by:
I am using mikroC to program some microcontroller code in C. However I have come across of potential problem of bool type not being supported from the compiler complaint on how I declared type bool...
1
by: Thorsten Dittmar | last post by:
Hi there, we have this C++ DLL we wrote some time ago to do some Win32 native stuff for us. The function we're calling in the DLL returns a boolean value (type bool) indicating success of the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.