Hey, folks. I hope you don't mind me asking here, but on the (currently somewhat inactive) subject of advanced TARGET programming:
Has anyone managed to load a DLL and call an exported function in it? I've been banging my head on this for a while. I have a Visual Studio C++/CLI project that exports a function to display joystick mode information, and I'm just trying to call that function with a T.16000M FCS joystick button. I haven't had any luck so far.
I appear to be able to load the DLL and get the address of the exported function:
int LoadMyLibrary()
{
int lib = LoadLibrary("UnmanagedLink.dll");
if (lib == 0)
{
printf("Bad library address\xa");
return 0;
}
int procAddr = GetProcAddress(lib, "_ShowAxisMessage@0"); // found with >dumpbin /EXPORTS
if (procAddr == 0)
{
printf("Bad proc address\xa");
return 0;
}
lib and procAddr seem to have good numbers in them, and no error is generated. However, I haven't been able to figure out how to call the darned thing. I can call existing TARGET functions with an alias, like so:
int printMsg () {
printf("This is a message.\xa");
}
int aliasMsg () {
alias repeat = &printMsg;
repeat(); // this works
}
It's getting the loaded procedure address into the alias and calling it that's got me stumped. I've tried various combinations of Map(), MakeProcInstance(), realloc(), setmem(), execute(), eval(), and have reached the desperation point. For example, this seems like it should work in LoadMyLibrary(), but it doesn't:
alias ShowMsg;
&ShowMsg = procAddr;
ShowMsg(); // Bad alias
It doesn't work any better if ShowMsg() is defined outside LoadMyLibrary():
int ShowMsg() {}
int LoadMyLibrary() {
// get procAddr as above
&ShowMsg = procAddr; // Bad alias
ShowMsg();
}
I don't really know what constitutes a good or bad alias. There's a lot of this pattern in target.tmh: Map(&CHAIN, MakeProcInstance(&_CHAIN), MAP_IPTR_VPN), but MakeProcInstance(procAddr) doesn't work because procAddr isn't an alias, and so would generate another "bad alias" runtime error on that line.
I feel like someone somewhere must know how to use these TARGET library-related functions. Any ideas?