473,847 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[repost] Static initialization of native code in CLI/native mixed environment

I posted this recently in microsoft.publi c.vc.language and was
redirected
here.

I also searched this ng and found some relavant threads.

The most relavent I found was:

http://groups.google.com/group/micro...919fdc1ee07313

(static unmanaged object in mixed exe)

The solution seems to be to either set the linker's /entry to the
mangled
name of int main(array<Syst em::String^> ^)) or to have an int WinMain
that calls the managed style main. The latter actually seems
preferable
since I don't want to have to memorize the manged name, but this still
seems like a bug to me (you could also default to console app but that
ususally isn't acceptable).

Is this still the consensus on the state of this issue?

This still seems like a bug to me. Either in the compiler or the
wizard,
but which is it? Should I be expected to know that I have to have a
WinMain for a mixed exe? Where is this documented?

[original post follows.]
I have recently been sentenced to the task of getting a Windows
Forms application to work with static libraries that contain native
code. This isn't working.

What is happening is that the first statically allocated object with a
nontrivial destructor is being registered with atexit. However, the
atexit function utilizes a pair of pointers that aren't setup
correctly. In debug it breaks at the following line of code in
dbgheap.c:

/* validation section */
_VALIDATE_RETUR N(pUserData != NULL, EINVAL, -1);

If I "pop the stack" I can see that __onexitbegin and __onexitend are
0. Elsewhere, in comments in the sources that come with MSC
(e.g. mcrtexe.c) I read that in a main program these should be
initialized to -1.

I have been able to work around this with the following code:

typedef void (__cdecl *_PVFV)(void);
extern "C"
{
_PVFV *__onexitbegin= (_PVFV*)-1;
_PVFV *__onexitend=(_ PVFV*)-1;
}

in the same .cpp file that has a C++/CLI main function implemented
with the Pascal style pointer/reference whatever they are ("^")
thingies.

This works in Win2k but apparently not on XP.

Which leads me to believe that perhaps native and managed code can be
mixed but that they must reside in separate DLLs? One of the articles
I have found in MSDN seems to suggest that problems of this kind are
resolved via DLLmain which would seem to support this suggestion.

Has anyone else experienced this problem? Is it not possible to link
a main program compiled with /clr with static libraries that aren't?

Thanks in advance for any insight.

Apr 26 '06 #1
1 2234
Hi,

as far as I can tell now, the VC project Wizard for a "Windows Forms
Application" creates a project that does not depend on the CRT, because
assemblies with CRT initialization can not use certain features of the
Visual Studio 2005 component model. I am currently researching this because
I am currently writing a book about C++/CLI and one chapter is about CLR and
CRT initialization of assemblies. I will blog about this soon. So far, I can
only recomment you to split your application into the EXE project that calls
Application.Run and a DLL project compiled with /clr:safe that provides the
forms and controls.

Marcus Heege

<bv*******@bell south.net> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
I posted this recently in microsoft.publi c.vc.language and was
redirected
here.

I also searched this ng and found some relavant threads.

The most relavent I found was:

http://groups.google.com/group/micro...919fdc1ee07313

(static unmanaged object in mixed exe)

The solution seems to be to either set the linker's /entry to the
mangled
name of int main(array<Syst em::String^> ^)) or to have an int WinMain
that calls the managed style main. The latter actually seems
preferable
since I don't want to have to memorize the manged name, but this still
seems like a bug to me (you could also default to console app but that
ususally isn't acceptable).

Is this still the consensus on the state of this issue?

This still seems like a bug to me. Either in the compiler or the
wizard,
but which is it? Should I be expected to know that I have to have a
WinMain for a mixed exe? Where is this documented?

[original post follows.]
I have recently been sentenced to the task of getting a Windows
Forms application to work with static libraries that contain native
code. This isn't working.

What is happening is that the first statically allocated object with a
nontrivial destructor is being registered with atexit. However, the
atexit function utilizes a pair of pointers that aren't setup
correctly. In debug it breaks at the following line of code in
dbgheap.c:

/* validation section */
_VALIDATE_RETUR N(pUserData != NULL, EINVAL, -1);

If I "pop the stack" I can see that __onexitbegin and __onexitend are
0. Elsewhere, in comments in the sources that come with MSC
(e.g. mcrtexe.c) I read that in a main program these should be
initialized to -1.

I have been able to work around this with the following code:

typedef void (__cdecl *_PVFV)(void);
extern "C"
{
_PVFV *__onexitbegin= (_PVFV*)-1;
_PVFV *__onexitend=(_ PVFV*)-1;
}

in the same .cpp file that has a C++/CLI main function implemented
with the Pascal style pointer/reference whatever they are ("^")
thingies.

This works in Win2k but apparently not on XP.

Which leads me to believe that perhaps native and managed code can be
mixed but that they must reside in separate DLLs? One of the articles
I have found in MSDN seems to suggest that problems of this kind are
resolved via DLLmain which would seem to support this suggestion.

Has anyone else experienced this problem? Is it not possible to link
a main program compiled with /clr with static libraries that aren't?

Thanks in advance for any insight.


the poblem that you describe is related to
Apr 27 '06 #2

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

Similar topics

6
2818
by: mflanagan | last post by:
I have unmanaged C++ program that will load a managed C++ dll and then call a function in that dll. The managed C++ routine will call some C# routines. The unmanaged C++ main program will make these calls (to the managed C++ dll) many times, from many different threads. Each thread will do a LoadLibrary, call one C++ routine in the managed dll, and upon return, unload the library and exit. I'm concerned about class variables (static)...
9
2599
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
11
3855
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
8
3657
by: quortex | last post by:
Hi all, I have a native class which has a single instance controlled via the singleton pattern. I need to call this from both native C++ and from mixed mode visual studio 2005 c++ CLI. At the moment I have a MC++ unit test project which accesses the singleton. What I seem to be finding is that the singleton accessed directly from the unit test project is not the same instance as the singleton accessed from native code.
6
3349
by: shu | last post by:
Is it legal to declare a static instance of an object compiled in a "No CLR support" .obj? I have the next .cpp, in which I mix definition of CTest and declarion of the static var in the same .cpp to make the example shorter : class CTest { public: CTest();
5
1787
by: Jesper Schmidt | last post by:
Hi, I have a relatively large C++ code base, which requires that static initializers are called when a dll is loaded. This seems to work well for native code, but it does not work for files compiled with the /CLR option enabled. I will try to give a simple example that illustrates my problem. Library code: -----------
4
2363
by: DaTurk | last post by:
Hi, I've noticed that when trying to create a managed, auto_gcrooted variable in a antive class, the compiler throws linker exceptions. While searching some forums I found out that this is a bug? I couldn't find the work around, just numerous references to the fact there is one. But apparently there are issues as to how to destroy said objects. Can anyone shed some more light on this topic for me? It would be much appreciated.
55
6279
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
20
6115
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10653
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10347
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4129
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.