473,770 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling managed function from unmanaged function in mixed mode dll

Hi,
i originally posted this via another portal, but after giving it time
to propagate it still hasn't shown up. My apologies for the
multiposting.

This might be a very simple question, but i am still getting my head
round this stuff.

I have a mixed mode dll. In it i have a managed class with a function,
i want to call that function from a function in an unmanaged class.
Here is the code:

namespace MyLib {

template <typename U>
inline U unbox(System::O bject* o) {
return *(dynamic_cast< __box U*>(o));
}
__gc class ManagedSupportL ib {
public:
int returnStoredVal ue (String* keyword, int defaultValue);

private:
static Hashtable* _kwHashTable;
};
}

namespace MyLib {

int ManagedSupportL ib::returnStore dValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keywo rd));
}
return defaultValue;
}
}
//unmanaged class:
__nogc class Blah {
protected:
int getStoredValue( );
};

int Blah::getStored Value() {
//this function needs to be commented to compile:
//return ManagedSupportL ib::returnStore dValue("myStrin gKey", 123);

return 123;
}
So, my questions are:
- i would like to have the managed function returnStoredVal ue()
declared as static so that i don't have to keep creating instances of
the class, is this possible in this scenario?
- how should i pass a string value from getStoredValue( ) to
returnStoredVal ue(), this string is used as a key for the hashtable
lookup? (it is fine for me to mess with these parameters if necessary)
- how should i declare the hashtable so that it is static? Is it as
simple as putting the static keyword in front of it when it is
declared?

All this compiles fine without the call to returnStoredVal ue(), i just
can't seem to put the call together correctly. It is reasonably
important (but not totally essential) that the managed function is
static because it is going to be called *lots* of times and i don't
want to be creating a new instance of the class each time. The
hashtable does need to be static as it is going to be loaded with
values upon dll load and must be available until the dll is unloaded.
Splitting the managed and unmanaged code into separate dlls is also not
an option, i want to deploy this as a single dll.

Thanks for any help, it is much appreciated :)

--------------------------------

Nov 17 '05 #1
1 1484
- you forgot the namespace around class Blah and the static keyword for the declaration of
returnStoredVal ue

- the const char[] in the call to returnStoredVal ue is automagically converted
- you have to create an instance of your static Hashtable
this compiles and runs:

namespace MyLib {

template <typename U>
inline U unbox(System::O bject* o) {
return *(dynamic_cast< __box U*>(o));
}
__gc class ManagedSupportL ib {
public:
static int returnStoredVal ue (String* keyword, int defaultValue);

private:
static Hashtable* _kwHashTable = new Hashtable();
};
}

namespace MyLib {

int ManagedSupportL ib::returnStore dValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keywo rd));
}
return defaultValue;
}
}

namespace MyLib {

//unmanaged class:
__nogc class Blah {
protected:
int getStoredValue( );
};

int Blah::getStored Value() {
//this function needs to be commented to compile:
return ManagedSupportL ib::returnStore dValue("myStrin gKey", 123);

return 123;
}

}
sl******@gmail. com wrote:
Hi,
i originally posted this via another portal, but after giving it time
to propagate it still hasn't shown up. My apologies for the
multiposting.

This might be a very simple question, but i am still getting my head
round this stuff.

I have a mixed mode dll. In it i have a managed class with a function,
i want to call that function from a function in an unmanaged class.
Here is the code:

namespace MyLib {

template <typename U>
inline U unbox(System::O bject* o) {
return *(dynamic_cast< __box U*>(o));
}
__gc class ManagedSupportL ib {
public:
int returnStoredVal ue (String* keyword, int defaultValue);

private:
static Hashtable* _kwHashTable;
};
}

namespace MyLib {

int ManagedSupportL ib::returnStore dValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keywo rd));
}
return defaultValue;
}
}
//unmanaged class:
__nogc class Blah {
protected:
int getStoredValue( );
};

int Blah::getStored Value() {
//this function needs to be commented to compile:
//return ManagedSupportL ib::returnStore dValue("myStrin gKey", 123);

return 123;
}
So, my questions are:
- i would like to have the managed function returnStoredVal ue()
declared as static so that i don't have to keep creating instances of
the class, is this possible in this scenario?
- how should i pass a string value from getStoredValue( ) to
returnStoredVal ue(), this string is used as a key for the hashtable
lookup? (it is fine for me to mess with these parameters if necessary)
- how should i declare the hashtable so that it is static? Is it as
simple as putting the static keyword in front of it when it is
declared?

All this compiles fine without the call to returnStoredVal ue(), i just
can't seem to put the call together correctly. It is reasonably
important (but not totally essential) that the managed function is
static because it is going to be called *lots* of times and i don't
want to be creating a new instance of the class each time. The
hashtable does need to be static as it is going to be loaded with
values upon dll load and must be available until the dll is unloaded.
Splitting the managed and unmanaged code into separate dlls is also not
an option, i want to deploy this as a single dll.

Thanks for any help, it is much appreciated :)

--------------------------------

Nov 17 '05 #2

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

Similar topics

5
2957
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++ libraries. We are using managed C++ as a bridge between managed .NET code and unmanaged C++ code, which I'm sure is a fairly common practice. The managed C++ library is compiled with /CLR whereas all other libraries are compiled without /CLR because...
1
2914
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
9
2075
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
9
3122
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for this, I want to use it. Is it possible to call Managed class functions from Unmanaged class? How to do it? I did something like this. I declared a managed class (in C++ CLI) called as MyManagedClass whose
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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...
1
10037
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
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
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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
3
2849
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.