DLL Loader for MicroStation v8
Part of product |
Research |
Date |
March 2003 |
Programming languages |
C |
Company |
E3.2A |
Description |
DLL loader enables load of any native written DLL or even .NET assemblies which resides in DLL. |
Download source and release code
We used key-in mdl load...for years, now why there is missing in MicroStation v8 something like dll load ??? - It could be due to the mdl descriptor so we still need MA application wrapper... I made simple dll loader which enables direct loading of any dll code (including .NET assemblies) and it allows you to run exported void function from dll :
1. load the loader :mdl l loader.ma
2. load dll : dll l smaz4.dll
3. run exported function from dll: dll run smaz4.dll MdlMain
- that is all for loading dll without need of any additional MA file with some resource definition for loading dll....
- note that mdlSystem_getMdlTaskID(mdlSystem_getCurrMdlDesc() ); called from dll will return "LOADER" because it is its base MA application.
/* declaration of dll function */
typedef
int (*DLLPROC)(void*);/* structure for DLL name storage*/
typedef
struct dllModule{
short used;HMODULE hModule;
char dllName[80];}DLLModule;
/* counter*/
int
dllCounterQ = 0;
DLLModule dllModQ[MAX_CLIENTS];
/*---------------------------------------------------------------- */
DLLEXPORT
void LoadDll(
char *libName // => library Name)
/*---------------------------------------------------------------- */
/* Loads DLL by its name into MicroStation. The name of loaded DLL and library handler is stored into
dllModQ structure */
{
HMODULE lib = NULL;
int i;lib = LoadLibrary(libName);
if (!lib) return;dllCounterQ++;
for (i = 0; i < MAX_CLIENTS;i++) { if (!dllModQ[i].used) {dllModQ[i].used = dllCounterQ ;
dllModQ[i].hModule = lib;
strcpy(dllModQ[i].dllName,libName);
break;}
}
return;}
/*---------------------------------------------------------------- */
DLLEXPORT
int FreeDll // <= 0 ~ SUCCESS(
char *dllName // => Dll Name)
/*---------------------------------------------------------------- */
//Decrements the reference count of the loaded dynamic-link library (DLL) module
//When the reference count reaches zero, the module is unmapped from the address
// space of the calling process and the handle is no longer valid.
// Ret: Nonzero indicates success. Zero indicates failure
{
int i; if (!dllName) return ERROR; for ( i = 0; i < MAX_CLIENTS;i++) { if ((dllModQ[i].used) && (dllModQ[i].hModule) &&(!strcmpi(dllName,dllModQ[i].dllName))) {
int retVal = FreeLibrary(dllModQ[i].hModule);dllModQ[i].used = 0;
dllModQ[i].hModule = NULL;
strcpy(dllModQ[i].dllName,
"\0");
}
}
return ERROR;}
/*---------------------------------------------------------------- */
int
CallDllFunc // <= return value from dll function(
char *dllName, // => Dll Name char *funcName, // => function name void *userP // => user data pointer)
/*---------------------------------------------------------------- */
//calls dll function from DLL library
{
HMODULE dllLib = LoadLibrary(dllName);
DLLPROC dllProc;
int retVal; if (!dllLib) return ERROR;dllProc = (DLLPROC)GetProcAddress(dllLib, funcName);
if (!dllProc)return ERROR;
retVal = dllProc(userP);
FreeLibrary(dllLib);
return retVal;}
/*---------------------------------------------------------------- */
DLLEXPORT
void RunDllFunc(
char *params // => dll name, function name)
/*---------------------------------------------------------------- */
/* Key-in wrapper for CallDllFunc, first parameter is dll name, second is function name*/
{
CallDllFunc(GetStrParam(params,0),GetStrParam(params,1),NULL);
}
/* ----------- DLL ENTRY POINT --------------------------------------*/
BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
return TRUE;}
Private MdlCommandNumber commandNumbers[] =
{
{LoadDll ,CMD_DLL_LOAD},
{FreeDll ,CMD_DLL_UNLOAD},
{RunDllFunc ,CMD_DLL_RUN},
0
};
/* ----------- MDL ENTRY POINT --------------------------------------*/
DLLEXPORT
int MdlMain(
int argc, char *argv[])
/*----------------------------------------------------------------------*/
{
RscFileHandle Rsc_fil;
mdlResource_openFile(&Rsc_fil, NULL, RSC_READONLY);
mdlParse_loadCommandTable (NULL);
mdlSystem_registerCommandNumbers (commandNumbers);
return 0;}
/* Finish main function */