- 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;
}
}
slugster@gmail.com wrote:[color=blue]
> 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 :)
>
> --------------------------------
>[/color]