473,394 Members | 2,071 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,394 software developers and data experts.

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::Object* o) {
return *(dynamic_cast<__box U*>(o));
}
__gc class ManagedSupportLib {
public:
int returnStoredValue (String* keyword, int defaultValue);

private:
static Hashtable* _kwHashTable;
};
}

namespace MyLib {

int ManagedSupportLib::returnStoredValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keyword));
}
return defaultValue;
}
}
//unmanaged class:
__nogc class Blah {
protected:
int getStoredValue();
};

int Blah::getStoredValue() {
//this function needs to be commented to compile:
//return ManagedSupportLib::returnStoredValue("myStringKey" , 123);

return 123;
}
So, my questions are:
- i would like to have the managed function returnStoredValue()
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
returnStoredValue(), 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 returnStoredValue(), 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 1460
- you forgot the namespace around class Blah and the static keyword for the declaration of
returnStoredValue

- the const char[] in the call to returnStoredValue 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::Object* o) {
return *(dynamic_cast<__box U*>(o));
}
__gc class ManagedSupportLib {
public:
static int returnStoredValue (String* keyword, int defaultValue);

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

namespace MyLib {

int ManagedSupportLib::returnStoredValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keyword));
}
return defaultValue;
}
}

namespace MyLib {

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

int Blah::getStoredValue() {
//this function needs to be commented to compile:
return ManagedSupportLib::returnStoredValue("myStringKey" , 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::Object* o) {
return *(dynamic_cast<__box U*>(o));
}
__gc class ManagedSupportLib {
public:
int returnStoredValue (String* keyword, int defaultValue);

private:
static Hashtable* _kwHashTable;
};
}

namespace MyLib {

int ManagedSupportLib::returnStoredValue (String* keyword, int
defaultValue) {
if (_kwHashTable->ContainsKey( keyword)) {
return unbox<int>( _kwHashTable->get_Item(keyword));
}
return defaultValue;
}
}
//unmanaged class:
__nogc class Blah {
protected:
int getStoredValue();
};

int Blah::getStoredValue() {
//this function needs to be commented to compile:
//return ManagedSupportLib::returnStoredValue("myStringKey" , 123);

return 123;
}
So, my questions are:
- i would like to have the managed function returnStoredValue()
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
returnStoredValue(), 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 returnStoredValue(), 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
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++...
1
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...
9
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
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
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...

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.