473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixed-Mode library assembly bug

I've found some rather worrying articles (Mixed Mode Library Assembly bug,
Richard Grimes, Windows Developer Network Sept 2003 ; and Knowledge Base
Article 814472 ) that point out the need to use the /noentry switch and an
explicit initialisation procedure, to be able to safely use Managed C++
assemblies that call down into standard C library functions.

Could someone confirm that I've understood this properly ? I'm writing some
Managed C++ code that provides a wrapper around some existing native C++
code that makes use of the standard C library and also invokes routines in a
3rd-party LIB. This code will be built into an assembly that will be called
from a front-end GUI written in C#. Therefore, I *do* need to follow all the
guidelines in 814472, and explicitly call an initialisation function from my
GUI app. Yes ?

Given that this is the case, is there a way of having the initialisation
code automatically invoked when the assembly is loaded ? Relying on client
code to call the initialisation function seems rather ugly :-(

Steve.
Nov 17 '05 #1
1 1700
Steve Terepin wrote:
Could someone confirm that I've understood this properly ? I'm writing some
Managed C++ code that provides a wrapper around some existing native C++
code that makes use of the standard C library and also invokes routines in a
3rd-party LIB. This code will be built into an assembly that will be called
from a front-end GUI written in C#. Therefore, I *do* need to follow all the
guidelines in 814472, and explicitly call an initialisation function from my
GUI app. Yes ?
That is indeed what it says in KB 814472.
Given that this is the case, is there a way of having the initialisation
code automatically invoked when the assembly is loaded ? Relying on client
code to call the initialisation function seems rather ugly :-(


You can avoid all of this ugliness by not using mixed DLLs in the first
place. You can write a pure native Win32 DLL that wraps your C++ code
and exposes it as a P/Invoke-friendly flat API. For example:

==== CppLibrary.DLL ================================================== ==

__declspec(dllexport) class MyClass {
public:
MyClass(int x) { ... }
~MyClass();
void DoStuff();
};

==== End CppLibrary.DLL ================================================

==== Wrapper.DLL ================================================== =====

extern "C" __declspec(dllexport) MyClass *CreateMyClass(int x) {
return new MyClass(x);
}

extern "C" __declspec(dllexport) void DeleteMyClass(MyClass *myClass) {
delete myClass;
}

extern "C" __declspec(dllexport) void MyClassDoStuff(MyClass *myClass) {
myClass->DoStuff();
}

==== End Wrapper.DLL ================================================== =

==== C# Client ================================================== =======

class MyClass : IDisposable {
[DllImport("Wrapper.DLL")]
extern static IntPtr CreateMyClass(int x);

[DllImport("Wrapper.DLL")]
extern static void DeleteMyClass(IntPtr myClass);

[DllImport("Wrapper.DLL")]
extern static void MyClassDoStuff(IntPtr myClass);

IntPtr handle;

public MyClass(int x) {
handle = CreateMyClass(x);
}

public void Dispose() {
DeleteMyClass(handle);
}

public void DoStuff() {
MyClassDoStuff(handle);
}
}

==== End C# Client ================================================== ===

Advantages of this approach:
- no mixed DLLs, ergo no mixed DLL bugs
- no need for explicit initialization
- transparency: no It Just Works magic or surprises
- easier to see where managed-unmanaged transitions occur, which is
important for performance

Advantages of mixed DLLs (a.k.a. It Just Works):
- type fidelity between managed and unmanaged worlds:
- static type checking
- native data structures can be accessed as usual in managed functions

Good luck!

Bart Jacobs

Nov 17 '05 #2

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

Similar topics

3
2557
by: Perttu Pulkkinen | last post by:
No questions, but just consider if this is useful to you:-) but of course feedback & corrections are welcome. function php_mixed_to_js_value($jsname, $mixed) { if(is_null($mixed)) { return "\n...
0
1753
by: Swaroop Kumar | last post by:
Hi: I'm trying to write a schema that contains information as described below: 1. The first element is a mandatory fixed string. 2. The second element is a mixed content element that can...
2
1744
by: Paul A. Hoadley | last post by:
Hello, I am trying to convert a RELAX NG schema to DTD using Trang. I am currently trying to add some inline elements to the schema, such as <emph> for marking emphasised text. Here is an...
15
5375
by: Bill Cohagan | last post by:
I'm trying to generate class definitions from an XSD that contains something like: <xs:complexType name="foo" mixed="true"> <xs:choice minOccurs = "0" maxOccurs="unbounded"> <xs:element name =...
46
2663
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
5
5738
by: Jeff | last post by:
I am trying to crete a method that will convert an improper fraction to a mixed number... I am not sure how about how to acomplish this. I know I can get the remainder with the modulus operator...
2
1587
by: bearophileHUGS | last post by:
Notes: - This email is about Mark Dufour's Shed Skin (SS) (http://shed-skin.blogspot.com), but the errors/ingenuousness it contains are mine. My experience with C++ is limited still. - The...
4
10261
by: natG | last post by:
Hi; I am transferring data from MySql to db2 using my own java/jdbc program. Working out ok, except for the fact that our apps use mixed-case names for tables and columns. Although my CREATE TABLE...
1
2212
by: bvisscher | last post by:
I posted this recently in microsoft.public.vc.language and was redirected here. I also searched this ng and found some relavant threads. The most relavent I found was: ...
1
1569
by: andrew_nuss | last post by:
Hi, Lets say I have a MIXED tag in my XML DTD with content that is going to be rendered as HMTL, as well as a <boldand <italicstag. What about whitespace? Specifically, does the whitespace in...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
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,...
1
7069
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...
0
7505
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...
0
4730
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...
0
3216
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...
0
1570
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 ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
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...

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.