ever wantted to make your own function but dont want the random charters ?myfunction@YBX?@YYZ that come with it? then this is the tutorial for you..
PART 1 Interger functions
STEP 1 creting the empty DLL project
1. open visual studio c++ 2008 Express SP1
2. click: file > new > project
3. click: Win32 on the left pane then click Win32 Project from the right pane
4. click name enter myfistdll and click ok
5. click next click DLL then click empty project click finish
congradulations you just created a blank DLL project
STEP 2 add a cpp file and set compiler options
1. right click the scources folder
2. add > new item
3. click code in the left pane then click C++ File(.cpp) from the right pane
4. click the name box from the bottom and enter myfirstdll then click add
5. click build > Configuration Manager
6. click the configuration dropdown box in the lower window and select release
7. click close
step 3 writting the code and compilling
1. copy paste the following into C++
// Interger function example by EDGECOM
#include <stdexcept>
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
// __declspec(dllexport) char __cdecl MYCOMMAND(void);
extern "C" __declspec( dllexport ) int Math1(void);
extern "C" __declspec( dllexport ) int Math2(int input1 , int input2);
int Math1( void )
{
int tmp1 = 4 ;
int tmp2 = 6 ;
int tmp3 = tmp1 + tmp2 ;
return tmp3;
}
int Math2(int input1 , int input2)
{
int tmp4 = input1 + input2;
return tmp4;
}
once you have copied the code click build then build solution or you can press hotkey F7
now copy the compiled dll located in the release foler to your dark basic project folder and copy paste the code below into darkbasic
sync off
load dll "myfirstdll.dll",1
DLL=CALL DLL (1,"Math1",)
print "The Value of dll function should 6 + 4 = 10 "
print "DLL return value is : "+str$(DLL)
DLL=CALL DLL (1,"Math2",7,4)
print "The Value of dll function should 7 + 4 = 11 "
print "DLL return value is : "+str$(DLL)
print " "
print "Done press any key to exit "
wait key
end
note: you can add a 3rd function to tell the dll to add subtract etc.... like this
extern "C" __declspec( dllexport ) int Math2(int input1 , int input2 , int input3);
int Math2(int input1 , int input2 ,int input3)
{
if (input3 == 1)
{
// add input1 and input2
int tmp4 = input1 + input2;
return tmp4;
}
if (input3 == 2)
{
// subtract input1 and input2
int tmp4 = input1 - input2;
return tmp4;
}
return 0;
}
PART 2 String and Char's
thanks to lee's SDK tutoral I figurd out string well for the most part
copy paste this C++ code below
// String function example by EDGECOM
#include <stdexcept>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
using namespace std;
extern "C" __declspec( dllexport ) void Stringtest(LPSTR pString);
void Stringtest( LPSTR pString)
{
if(pString)
{
MessageBoxA(NULL, pString, "", MB_OK);
}
}
the code above will create a messagebox with your text included in it
Darkbasic pro code
load dll "myfirstdll.dll",1
DLL=CALL DLL (1,"Stringtest","OMG! a popup box AHH!! >.< ")
print "DONE PRESS ANY KEY TO EXIT"
wait key
end
ok I figured its time to update this tutorial a little
the above code is great if you only want to send one huge string but what if you want to send more than one string.
here is a C++ code example of how to build and return a string
// String2 function example by EDGECOM
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
extern "C" __declspec( dllexport ) char* STRINGS2(char* string1, char* string2);
char* STRINGS2(char* string1, char* string2)
{
// Temp string to hold the incomming strings and assemble them into one string
// the [1024] is the total number of character the string can hold after that it will eather error or cut off the string
char str1[1024];
strcpy_s (str1,"String1: ");
strcat_s (str1,string1);
strcat_s (str1,"String2: ");
strcat_s (str1,string2);
return str1;
}
Darkbasic pro code
load dll "myfirstdll.dll",1
DLL$=CALL DLL (1,"STRINGS2","EDGE","COM")
print "The return value of inpu1 and input2: "+DLL$
print "DONE PRESS ANY KEY TO EXIT"
wait key
== Free usefull tools ==
anywhere viewer lets you view external functions in dlls and exe files so no more notepad searches
http://www.ucware.com/apev/index.htm#apevdl
just install it then open it drag and drop a dll click on the exports table and you should see functions that any program or exe can use
ResEdit is a free Resource Editor for Win32 programs if your using the VS2008 Express edition it does not come with a resource editor
http://www.resedit.net/