I've seen a lot of questions in the forums regarding using 3rd party dll's to extend the functionality of 3d Rad.
Since I'm on a c++ writing spree for a change, I thought I'd knock up a little demo in hopes that it may help.
The idea is to write a dll that acts as a bridge between 3D Rad, and the 3rd party dll, and wrap the 3rd party dll in your own dll, and then use a script to wrap your own dll... hope this makes sense..
Anyway, here's some sample code
A 3D Rad sample script
A sample 3d Rad wrapper dll in c
A Sample - Possible 3rd party dll - in c
Since I'm on a c++ writing spree for a change, I thought I'd knock up a little demo in hopes that it may help.
The idea is to write a dll that acts as a bridge between 3D Rad, and the 3rd party dll, and wrap the 3rd party dll in your own dll, and then use a script to wrap your own dll... hope this makes sense..
Anyway, here's some sample code
A 3D Rad sample script
Code: [Select]
//Example project for setting up a dll bridge call to another dll
//global dll handle
int DLLHandle = 0;
//------------------------------------------
//return the ascii value of a character
int ascii(string s)
{
string ASCIISET = " !'#$%&`(*+'-./0123456789;:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_'abcdefghijklmnopqrstuvwxyz{|}~";
int p = iStringFind(ASCIISET,s,0,true);
return p+32;
}
//------------------------------------------
//set string in dll
void DllSetString(string txt)
{
int i, asc;
string s;
for(i=0;i<iStringLen(txt);i++)
{
iStringMid(s,txt,i+1,1);
asc = ascii(s);
iDLLArraySet(i,asc);
}
iDLLArraySet(iStringLen(txt),0); //null terminate string
iDLLCall(DLLHandle,"SetString",0);
}
//------------------------------------------
//set int in dll
void DllSetInt(int n)
{
iDLLArraySet(n,0);
iDLLCall(DLLHandle,"SetInt",0);
}
//------------------------------------------
//get string from dll
string DllGetString()
{
int i;
string str="";
float f;
bool done = false;
i=0;
while(!done)
{
f=iDLLArrayGet(i);
i++;
if(f != 0)
{
str+=" ";
str[i] = f;
}
else
break;
if(i>1000) break; //limit release
}
return str;
}
//------------------------------------------
void DllDosomething()
{
iDLLCall(DLLHandle,"ExecSomething",0);
}
//========================================
void Main()
{
if (iInitializing())
{
DLLHandle = iDLLLoad(".\\3DRad_res\\objects\\Script\\raddllbridge.dll");
if (DLLHandle != 0)
{
string txt = "This is some text to write to a file";
int len = iStringLen(txt);
DllSetString(txt);
DllSetInt(len);
DllDosomething();
}
}
else if (iDeinitializing())
{
if (DLLHandle != 0) iDLLUnload(DLLHandle);
}
}
A sample 3d Rad wrapper dll in c
Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//reference to function in worker dll (remember to set the lib/a link)
extern int DoSomething(char* name,int length);
//3D Rad dll wrapper for DoSomething function above
__declspec(dllexport) void ExecSomething(float* myData);
//export for 3D Rad dll calls
__declspec(dllexport) void SetString(float* myData);
__declspec(dllexport) void GetString(float* myData);
__declspec(dllexport) void SetInt(float* myData);
__declspec(dllexport) void getInt(float* myData);
__declspec(dllexport) void SetFloat(float* myData);
__declspec(dllexport) void GetFloat(float* myData);
__declspec(dllexport) void SetChar(float* myData);
__declspec(dllexport) void GetChar(float* myData);
char bridgeStr[1000];
float bridgeF;
int bridgeI;
char bridgeC;
//----------------------------------------
//Accepts float array from 3D Rad, and stores it as a char array
void SetString(float* myData)
{
int i;
char c;
for(i=0;i<1000;i++)
bridgeStr[i]=0;
for(i=0;i<1000;i++){
c = myData[i];
if(c)
bridgeStr[i]=c;
else
break;
}
}
//----------------------------------------
//returns a char array as a float array to 3D Rad
void GetString(float* myData)
{
int i,L;
char c;
L = strlen(bridgeStr);
for(i=0;i<L;i++){
c = bridgeStr[i];
myData[i] = c;
}
myData[L]=0;
}
//----------------------------------------
void SetInt(float* myData)
{
bridgeI = myData[0];
}
//----------------------------------------
void GetInt(float* myData)
{
myData[0] = bridgeI;
}
//----------------------------------------
void SetFloat(float* myData)
{
bridgeF = myData[0];
}
//----------------------------------------
void GetFloat(float* myData)
{
myData[0] = bridgeF;
}
//----------------------------------------
void SetChar(float* myData)
{
bridgeC = myData[0];
}
//----------------------------------------
void GetChar(float* myData)
{
myData[0] = bridgeC;
}
//----------------------------------------
void ExecSomething(float* myData)
{
//int not retuened in this example, but simple
//to return to 3D Rad with GetInt()
int res;
res = DoSomething(bridgeStr,bridgeI);
}
A Sample - Possible 3rd party dll - in c
Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
__declspec(dllexport) int DoSomething(char* name,int length);
int DoSomething(char* name,int length)
{
FILE *fp;
fp = fopen("test.dat","w");
fprintf(fp,"I received %s,%d\n",name,length);
fclose(fp);
return 0;
}