473,569 Members | 2,676 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 2221
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
2802
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...
9
2575
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++...
11
3810
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...
8
3637
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...
6
3338
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
1773
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
2338
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...
55
6174
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
6074
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
7697
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...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
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...
0
5219
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...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
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
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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...

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.