473,395 Members | 1,623 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,395 software developers and data experts.

What's the best way to make a managed .dll that uses unmanaged classes and code?

I'm trying to create a .dll with VS.NET 2003 Architect that contains a math
computational component. I need the guts of the thing to be in native code,
as performance is key for that part. But, I need a .net wrapper because
this "calculator" is actually a component used by an enterprise app written
entirely in .net.

What I want to do is have one project. I've created an MC++ .dll project.
Its got a __gc class (the wrapper) and a handful of __nogc classes. I'm
having a number of problems with this. First off, as soon as I put a set a
pointer to the __nogc class from within my managed wrapper class'
constructor, the linker complains of unresolved external symbols for __cdecl
operator new and delete.

Here is the source code so far, its as basic as you can get:

// Calculator.h

#pragma once

using namespace System;

class InnerClass;

namespace Calculator
{
public __gc class Class1
{
public:
Class1();

private:
InnerClass* inner;
};
}

__nogc class InnerClass
{
public:
InnerClass();
~InnerClass();
};

// Calculator.cpp

#include "stdafx.h"

#include "Calculator.h"

Calculator::Class1::Class1()
{
inner = new InnerClass();
}

InnerClass::InnerClass()
{}

InnerClass::~InnerClass()
{}

Again, this project was created with File->New->Project, choosing VC++ class
library (.net). As for project properties, I haven't changed them from what
VS gave me.
Nov 16 '05 #1
4 2481
"0to60" <ho****************@yahoo.com> wrote in message
news:pU******************@newssvr33.news.prodigy.c om...
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I need a .net wrapper because
this "calculator" is actually a component used by an enterprise app written entirely in .net.

What I want to do is have one project. I've created an MC++ .dll project.
Its got a __gc class (the wrapper) and a handful of __nogc classes. I'm
having a number of problems with this. First off, as soon as I put a set a pointer to the __nogc class from within my managed wrapper class'
constructor, the linker complains of unresolved external symbols for __cdecl operator new and delete.


Glibly, I'd suggest that an easier solution would be had by putting the
native code in "free" functions called by the member functions of your
managed classes than by trying to mix managed and non-managed object models.
In that way you can take advantage of the "it just works" (IJW) facility to
have managed and unmanaged functions reside in the same module as long as
you define the boundaries between the two environments with

#pragma managed

and

#pragma unmanaged

Of course, if your project is complex, the more elegant solution may require
proceeding in the way that you outlined. I suggest that you start reading
here for more information on mixed-mode applications:

http://msdn.microsoft.com/library/de...us/vcmex/html/
vcconconvertingmanagedextensionsforcprojectsfrompu reintermediatelanguagetomi
xedmode.asp

I used that link to get a mixed-mode project of mine off the ground. I had
the same problem that you did. I defined new and delete operators for my
classes. And because it wasn't necessary for me to squeeze every last ounce
of performance out, I used LocalAlloc() and LocalFree() to implement them.

Regards,
Will


Nov 16 '05 #2
"William DePalo [MVP VC++]" <wi***@mvps.org> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...

Glibly, I'd suggest that an easier solution would be had by putting the
native code in "free" functions called by the member functions of your
managed classes than by trying to mix managed and non-managed object models. In that way you can take advantage of the "it just works" (IJW) facility to have managed and unmanaged functions reside in the same module as long as
you define the boundaries between the two environments with

#pragma managed

and

#pragma unmanaged

Of course, if your project is complex, the more elegant solution may require proceeding in the way that you outlined. I suggest that you start reading
here for more information on mixed-mode applications:


Well as it turns out, there are HUGE performance implications when using
#pragma unmanaged to make certain methods unmanaged. I did a test of this
calculator thing that I need, and I'm finding that coding a regular ol'
Win32 .dll with free functions, and then calling those functions from my
__nogc class and using System.Interopservices.DLLImport is on the order of
5x faster than putting the equivalent code right in my project and compiling
it with #pragma unmanaged.

Nov 16 '05 #3
0to60 <ho****************@yahoo.com> wrote in message
news:W9******************@newssvr32.news.prodigy.c om...
Well as it turns out, there are HUGE performance implications when using
#pragma unmanaged to make certain methods unmanaged. I did a test of this
calculator thing that I need, and I'm finding that coding a regular ol'
Win32 .dll with free functions, and then calling those functions from my
__nogc class and using System.Interopservices.DLLImport is on the order of
5x faster than putting the equivalent code right in my project and compiling it with #pragma unmanaged.


OK,not knowing your application, I'm willing to take your numbers on faith.

Of course, you want to minimize the transitions. What I was suggesting was
syntactic sugar - thin .Net wrappers around the functions that do the real
work without a lot of back and forth. Otherwise, the marshalling and
unmarshalling of arguments gets expensive.

Regards,
Will
Nov 16 '05 #4
The default MC++ class library wizard are generated managed code only, it
doesn't link to CRT/MFC/ATL, to support unmanaged code ,read the following
about how to build a mixed mode dll
http://support.microsoft.com/?id=814472
and to resolve the link error of operator new and delete, add back _DLL
preprocessor definitions

Ben

"0to60" <ho****************@yahoo.com> wrote in message
news:pU******************@newssvr33.news.prodigy.c om...
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I need a .net wrapper because
this "calculator" is actually a component used by an enterprise app written entirely in .net.

What I want to do is have one project. I've created an MC++ .dll project.
Its got a __gc class (the wrapper) and a handful of __nogc classes. I'm
having a number of problems with this. First off, as soon as I put a set a pointer to the __nogc class from within my managed wrapper class'
constructor, the linker complains of unresolved external symbols for __cdecl operator new and delete.

Here is the source code so far, its as basic as you can get:

// Calculator.h

#pragma once

using namespace System;

class InnerClass;

namespace Calculator
{
public __gc class Class1
{
public:
Class1();

private:
InnerClass* inner;
};
}

__nogc class InnerClass
{
public:
InnerClass();
~InnerClass();
};

// Calculator.cpp

#include "stdafx.h"

#include "Calculator.h"

Calculator::Class1::Class1()
{
inner = new InnerClass();
}

InnerClass::InnerClass()
{}

InnerClass::~InnerClass()
{}

Again, this project was created with File->New->Project, choosing VC++ class library (.net). As for project properties, I haven't changed them from what VS gave me.

Nov 16 '05 #5

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

Similar topics

7
by: sachin | last post by:
Hi All, I have an application which is web based. I am hosting it on VB.Net container, ie winform. Now i am facing a problem. If i make an exe of that, then i require .net framework installed on...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
22
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
5
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain...
3
by: Takeshi | last post by:
Hi All, I am impressed with the aesthetic (UI) appeal of DotNet, but not by much else, like it's new fangled languages etc... I have a legacy C++ 3tier application, which uses C++ DLLs. I would...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
1
by: bonk | last post by:
Hello, I am trying to create a c++ dll or lib (not COM) that contains a little bit of managed code and a lot of unmanged code using c++/CLI. Basically some classes rely on managed bits...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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
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,...
0
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...

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.