473,327 Members | 2,016 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,327 software developers and data experts.

EXE with MFC statically linked loads DLL with MFC linked as shared

Is it generally OK for an EXE that has MFC linked statically to load an use
another DLL wich has MFC linked as shared DLL ?

To be more specific:

I have an EXE that links a lib. Let's call it mylib.lib. That Lib as well as
the EXE have MFC linked statically.

Then in a completely different project I have a regular DLL, wich is
compiled with the /CLR switch and therefore needs the special CRT libs as
well as MFC linked as shared library. This DLL links mylib.lib too only that
this time mylib.lib links MFC as shared library too.

Both exe and dll need need to link against mylib.lib because the dll exports
a function that returns an interface whose methods contain types defined in
mylib.lib.

Now - is this scenario problematic in any way? If so why ?

Mar 17 '06 #1
8 2041
Hi bonk!
Is it generally OK for an EXE that has MFC linked statically to load an use
another DLL wich has MFC linked as shared DLL ?


As long as you do not pass *any* CRT/MFC types from your EXE to the DLL,
then there should be no problem...
For example: you must not pass "CString" as a parameter to a DLL-function...
Greetings
Jochen
Mar 17 '06 #2
"Jochen Kalmbach [MVP]" <no********************@holzma.de> schrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi bonk!
Is it generally OK for an EXE that has MFC linked statically to load an
use
another DLL wich has MFC linked as shared DLL ?


As long as you do not pass *any* CRT/MFC types from your EXE to the DLL,
then there should be no problem...
For example: you must not pass "CString" as a parameter to a
DLL-function...


can you explain why ? Do CRT/MFC types differ between dynamically linked and
statically linked versions?

also, what about the rest of the above described scenario. The DLL exports a
function wich returns a pointer to an abstract class CMyInterface.
CMyInterface is defined in mylib.lib. Now CMyInterface is derived from CWnd
and also contains Methodes whose parameters are MFC types. I guess that fits
within "pass *any* CRT/MFC types from your EXE to the DLL" ... ?
Mar 17 '06 #3
Hi bonk!
Hi bonk!
Is it generally OK for an EXE that has MFC linked statically to load an
use
another DLL wich has MFC linked as shared DLL ? As long as you do not pass *any* CRT/MFC types from your EXE to the DLL,
then there should be no problem...
For example: you must not pass "CString" as a parameter to a
DLL-function...


can you explain why ? Do CRT/MFC types differ between dynamically linked and
statically linked versions?


The reason is simple: The use different heaps for allocating memory...

If you pass a CString (or any other stuff from CRT/MFC/ATL/STL) and the
caller changes this string an freeing and allocation may occur and will
lead to freeing from the wrong heap and allocation from the wrong heap...
also, what about the rest of the above described scenario. The DLL exports a
function wich returns a pointer to an abstract class CMyInterface.
CMyInterface is defined in mylib.lib.
If the DLL is using classes from a staticaly linked LIB, you will get a
warning that you are using different CRTs...
Now CMyInterface is derived from CWnd
and also contains Methodes whose parameters are MFC types. I guess that fits
within "pass *any* CRT/MFC types from your EXE to the DLL" ... ?


Yes. This is also not allowed...

You must share the same CRT/MFC/ATL in *all* EXE/DLLs.

Greetings
Jochen
Mar 17 '06 #4
I there any source where I can get more details on what you just described ?
How come there are two different heaps ? Who is responsibe for those heaps?
Which mfc dll (mfc lib ?) and crt lib are used when linking MFC as shared
library an wich when liniking as static?
What crt lib is used when compliling with /CLR?

"Jochen Kalmbach [MVP]" <no********************@holzma.de> schrieb im
Newsbeitrag news:Os*************@TK2MSFTNGP12.phx.gbl...
Hi bonk!
Hi bonk!
Is it generally OK for an EXE that has MFC linked statically to load an
use
another DLL wich has MFC linked as shared DLL ?
As long as you do not pass *any* CRT/MFC types from your EXE to the DLL,
then there should be no problem...
For example: you must not pass "CString" as a parameter to a
DLL-function...


can you explain why ? Do CRT/MFC types differ between dynamically linked
and statically linked versions?


The reason is simple: The use different heaps for allocating memory...

If you pass a CString (or any other stuff from CRT/MFC/ATL/STL) and the
caller changes this string an freeing and allocation may occur and will
lead to freeing from the wrong heap and allocation from the wrong heap...
also, what about the rest of the above described scenario. The DLL
exports a function wich returns a pointer to an abstract class
CMyInterface. CMyInterface is defined in mylib.lib.


If the DLL is using classes from a staticaly linked LIB, you will get a
warning that you are using different CRTs...
Now CMyInterface is derived from CWnd and also contains Methodes whose
parameters are MFC types. I guess that fits within "pass *any* CRT/MFC
types from your EXE to the DLL" ... ?


Yes. This is also not allowed...

You must share the same CRT/MFC/ATL in *all* EXE/DLLs.

Greetings
Jochen

Mar 17 '06 #5
Hi bonk!
I there any source where I can get more details on what you just described ?


See: Potential Errors Passing CRT Objects Across DLL Boundaries
http://msdn2.microsoft.com/ms235460

And because MFC/STL uses new/malloc the same article applies to these as
well.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 17 '06 #6
Thank you, Jochen for the detailed info.

So the reason, why I can't pass MFC Types across DLL boundaries if MFC is
linked differently has nothing to do with MFC itself but with the fact that
different copies of the CRT libraries are be used. Is that correct?

Are there different copies of the MFC libraries used as well ?

If linking MFC statically WOULDN'T require a special version of the CRT that
is different from the one used when lnking as shared library, I would be
fine. (yes, thats highly hypothetical).

"Jochen Kalmbach [MVP]" <no********************@holzma.de> schrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi bonk!
I there any source where I can get more details on what you just
described ?


See: Potential Errors Passing CRT Objects Across DLL Boundaries
http://msdn2.microsoft.com/ms235460

And because MFC/STL uses new/malloc the same article applies to these as
well.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Mar 17 '06 #7
Hi bonk!
So the reason, why I can't pass MFC Types across DLL boundaries if MFC is
linked differently has nothing to do with MFC itself but with the fact that
different copies of the CRT libraries are be used. Is that correct?
Yes.
Are there different copies of the MFC libraries used as well ?
Yes.
If linking MFC statically WOULDN'T require a special version of the CRT that
is different from the one used when lnking as shared library, I would be
fine. (yes, thats highly hypothetical).


The MFC depends on the CRT. And you need to select the correct CRT
version (DLL or static) if you want to use the DLL/static MFC...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 18 '06 #8
Mh ..., this might cause some unexpected problems:

When I use the /CLR switch for my MFC-DLL, msvcmrt.lib (msvcm80.dll) is used
.. Now this lib is not used for all other DLLs and EXEs that have not been
compiled with /CLR (regardless of wether they are linked with MFC as shared
or static).

Now since in this case different versions (copies) of the CRT Library are
used as well, I will not be able to pass pointer to a CRT-Object between a
DLL, that is compiled with /CLR (and with MFC linked as shared) and a DLL
that is compiled without /CLR (and with MFC linked as shared).

This would kind of suck, since I would not be able to implement a wrapper
that wraps some .NET (i.e. WinForms) controls to be used in a plain
unmanaged MFC-EXE.

"Jochen Kalmbach [MVP]" <no********************@holzma.de> schrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi bonk!
So the reason, why I can't pass MFC Types across DLL boundaries if MFC is
linked differently has nothing to do with MFC itself but with the fact
that different copies of the CRT libraries are be used. Is that correct?


Yes.
Are there different copies of the MFC libraries used as well ?


Yes.
If linking MFC statically WOULDN'T require a special version of the CRT
that is different from the one used when lnking as shared library, I
would be fine. (yes, thats highly hypothetical).


The MFC depends on the CRT. And you need to select the correct CRT version
(DLL or static) if you want to use the DLL/static MFC...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Mar 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rickard Lind | last post by:
Is there any way to build the python executable statically and still be able to load modules built as shared libraries? I'm trying to run python scripts on a stripped down FreeBSD (4.9) machine...
7
by: sug | last post by:
Hi, I am running a C program on Unix which loads a shared object of mine. I need to get the path of my .so from inside it. I want to do this because from my .so I want to open a text file...
0
by: zztlhb | last post by:
Hi, I currently hit an issue that I would like to use a function statically linked to a shared library but my program use the same function from another shared library. Here is what I do: 1. I...
5
by: David T. Ashley | last post by:
I've occasionally had trouble compiling and linking programs that use shared libraries. That never made a lot of sense to me, because I thought the operating system went hunting for the symbols...
1
by: Gurdeep Toor via AccessMonster.com | last post by:
Hi Folks, I am building an application in VB.NET... It connects to 2 databases MS SQL SERVER 2005 and MS ACCESS 2003 .. It works all fine with SQL but It only reads the record from the ACCESS...
36
by: Martin Larsen | last post by:
Hi, When a PHP program links to a library using include or require (or their _once variations), is the library then linked dynamically or statically? While it might seem irrelevant from a...
3
by: Micah Elliott | last post by:
Are there instructions somewhere on how to build Python (on linux) statically? This seems like a common thing to do want to do, but my searching isn't turning up much. If it is common, it would...
1
by: viraj.kadakia | last post by:
I observe a behavior with shared libraries (.so) and global variables that I cannot understand ... I'd appreciate if someone can explain the behavior .. Scenario 1: aTest is an executable on...
0
by: uonedang | last post by:
Hi folks, In Linux, when an application is started up .. and its dependent shared libraries are loaded into memory automatically at startup time ... I wonder what part of kernel/OS determines and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.