ISInterface:RegisterLibrary
Syntax
- bool RegisterLibrary(const char *Name,fGetAPI GetAPI)
fGetAPI is defined as follows
typedef void * (__stdcall *fGetAPI)(const char *Name, unsigned int Version);
Purpose
Registers a library that can be dynamically linked at runtime. This is just one of many possible ways to expose API to .NET programs. This method allows individual functions to bind at runtime, without exporting DLL functions or introducing COM objects. In this way, a .NET class can be mapped directly to a set of API in your C/C++ code. Stub functions in the .NET code can be used to automatically bind and call the API, or fail if the API could not be retrieved -- use a delegate instance pointing to the stub, and assign the GetAPI return value when the API is successfully retrieved.
The GetAPI function passed will be linked to the library name given. The function will then be called by LavishVM.GetAPI when an API bind is requested for the given library. The GetAPI function returns a pointer to a function matching the function request (like GetProcAddress).
Usage
Parameters
- const char *Name
- [in] Name of the library. Name the library however you please, this value just has to match the value used in the .NET code that calls GetAPI to retrieve the library's API.
- fGetAPI GetAPI
- [in] Function that maps the API name and version to the actual function.
Return Value
- bool
- true if the library was successfully registered.
Examples
- Example code
namespace InnerSpaceAPI { public: static void * __stdcall Base_GetAPI(const char *Name, unsigned int Version) { static map<string,void*> APIMap; static bool bInitialized=false; if (!bInitialized) { APIMap["Echo"]=&Echo; bInitialized=true; } map<string,void*>::iterator i=APIMap.find(Name); if (i==APIMap.end()) return 0; return i->second; } static void __stdcall Echo(const char *Output) { pISInterface->FrameLock(); printf("%s",Output); pISInterface->FrameUnlock(); } }; . . pISInterface->RegisterLibrary("Inner Space",&InnerSpaceAPI::Base_GetAPI);
Remarks
Care should be used when providing API. If the DLL containing your API is unloaded, references to your API are not automatically detached. Care should be taken in your API design to properly detach the API references in .NET. For example, a callback can be used to remove API references in .NET and replace them with stubs.