Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Compiling a DLL from a .c file

Hello,

I have a DLL and the source code for it in a .c file. I did some modifications to the source file and I'd like to know how to compile it into a DLL.
Which compiler should I use? How should I proceed?

Here's the source code, originally written by poccil:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#define BOUND(a,l,h) ( ((a)<(l)) ?(l):\
( ((a)>(h)) ?(h):(a) )\
)
#define EXPORTDLL __declspec(dllexport) __stdcall

////////////////////////////////////////////////////////

static void DebugOut(char *fmt,...){
char s[1024];
va_list arg;
va_start(arg,fmt);
vsnprintf(s,1024,fmt,arg);
va_end(arg);
OutputDebugStringA(s);
OutputDebugStringA("\r\n");
}

typedef struct{
BYTE s[256];
BYTE i,j;
BYTE valid;
} RC4;
void RandomInit(RC4 *rc4, LPBYTE seed, DWORD seedsize){
BYTE *seedp = (BYTE *) &seed;
int i;
BYTE j;
BYTE tmp;
for (i = 0; i < 256; i++)
rc4->s = i;
for (i = j = 0; i < 256; i++){
j += rc4->s + seedp[i % seedsize];
tmp=*(rc4->s+i);
*(rc4->s+i)=*(rc4->s+j);
*(rc4->s+j)=tmp;
}
rc4->i = rc4->j = 0;
rc4->valid=TRUE;
}

void RandomInitSimple(RC4 *rc4){
DWORD tc=GetTickCount();
RandomInit(rc4,&tc,sizeof(tc));
}
void RandomGetBytes(RC4 *rc4, void *buf_, size_t size)
{
BYTE *buf;
if (!rc4->valid){
DWORD seed=0;
RandomInit(rc4,&seed,sizeof(seed));
}
for (buf = buf_; size-- > 0; buf++){
BYTE tmp, s_k;
rc4->i++;
rc4->j += rc4->s[rc4->i];
tmp=*(rc4->s+rc4->i);
*(rc4->s+rc4->i)=*(rc4->s+rc4->j);
*(rc4->s+rc4->j)=tmp;
s_k = rc4->s[rc4->i]+rc4->s[rc4->j];
*buf = rc4->s[s_k];
}
}
DWORD RandomNext(RC4 *rc4, DWORD count){
DWORD rnd=0;
if(count==0)return 0;
RandomGetBytes(rc4,&rnd,sizeof(rnd));
return rnd%count;
}

////////////////////////////////////////////////////////

typedef struct{
DWORD flags;
DWORD klass;
void (*dmark)(void*);
void (*dfree)(void*);
double *data;//red is index 1, green is index 2, blue 3, alpha 4
} RGSSCOLOR;

typedef struct{
DWORD unk1;
DWORD unk2;
BITMAPINFOHEADER *infoheader;
RGBQUAD *firstRow;
RGBQUAD *lastRow;
} RGSSBMINFO;

typedef struct{
DWORD unk1;
DWORD unk2;
RGSSBMINFO *bminfo;
} BITMAPSTRUCT;

typedef struct{
DWORD flags;
DWORD klass;
void (*dmark)(void*);
void (*dfree)(void*);
BITMAPSTRUCT *bm;
} RGSSBITMAP;

#define ASSERT(x) if(!x){DebugOut("Failed: %s: %d",#x,__LINE__);}

void BlendColors(DWORD crDst, DWORD crSrc, BYTE alpha, BYTE *colors){
int r,g,b,a;
r=GetRValue(crDst);
g=GetGValue(crDst);
b=GetBValue(crDst);
a=(crDst>>24);
colors[0]=(alpha*((int)GetRValue(crSrc)-r)/255)+r;
colors[1]=(alpha*((int)GetGValue(crSrc)-g)/255)+g;
colors[2]=(alpha*((int)GetBValue(crSrc)-b)/255)+b;
colors[3]=(alpha*((int)(crDst>>24)-a)/255)+a;
}

inline LONG BlurReflect(LONG x, LONG d){
if(((abs(x) / d) & 1) == 0)
return (x < 0) ? (-x % d) : (x % d);
else
return (x > 0) ? (d - (x % d)) : (d - (-x % d));
}

BOOL EXPORTDLL BitmapRadialBlur(LONG object, LONG ang, LONG samples){
int x, y, i, count;
LONG *ct, *st;
float angle, theta, offset;
LPBYTE firstRow;
LPBYTE destbuf,dest;
RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<1))->bm->bminfo;
DWORD rowsize;
DWORD width,height;
LPBYTE row;
DWORD halfwidth,halfheight;
if(!bitmap)return FALSE;
width=bitmap->infoheader->biWidth;
height=bitmap->infoheader->biHeight;
halfwidth=(width>>1);
halfheight=(height>>1);
rowsize=width*4;
firstRow=bitmap->firstRow;
samples=max(samples,2);
angle=BOUND(ang,0,360)*0.0174532;
theta=angle/(samples-1);
ct=malloc(samples*sizeof(LONG));
if(!ct)return FALSE;
st=malloc(samples*sizeof(LONG));
if(!st){free(ct);return FALSE;}
offset = theta*(samples-1)/2;
for (i = 0; i < samples; i++) {
ct = cos(theta*i-offset)*4096.0;
st = sin(theta*i-offset)*4096.0;
}
destbuf = malloc(4*width*height);
if(!destbuf){free(ct);free(st);return FALSE;}
row=firstRow;
dest=destbuf;
for (y=0; y<height; y++) {
LPBYTE thisrow=row;
for (x=0;x<width;x++) {
LONG xr = x-halfwidth;
LONG yr = y-halfheight;
LONG sumR=0, sumG = 0, sumB = 0, sumA=0;
for (i = 0; i < samples; i++) {
LPBYTE src;
LONG xx = halfwidth + ((xr*ct)>>12) - ((yr*st)>>12);
LONG yy = halfheight + ((xr*st)>>12) + ((yr*ct)>>12);
if ((yy < 0) || (yy >= height) ||
(xx < 0) || (xx >= width)){
xx=BlurReflect(xx,width);
yy=BlurReflect(yy,height);
xx=BOUND(xx,0,width-1);
yy=BOUND(yy,0,height-1);
}
src=firstRow-(yy*rowsize)+(xx<<2);
sumB += src[0];
sumG += src[1];
sumR += src[2];
sumA += src[3];
}
*dest++ = sumB/samples;
*dest++ = sumG/samples;
*dest++ = sumR/samples;
*dest++ = sumA/samples;
thisrow+=4;
}
row-=rowsize;
}
row=firstRow;
dest=destbuf;
for (y = 0; y < height; y++) {
LPBYTE thisrow=row;
CopyMemory(thisrow,dest,rowsize);
dest+=rowsize;
row-=rowsize;
}
free(destbuf);
free(ct);
free(st);
return TRUE;
}

BOOL EXPORTDLL BitmapGradientFillRect(
LONG object, LONG x, LONG y, LONG cx, LONG cy, DWORD color1, DWORD color2, BOOL vertical){
DWORD tmp=0;
RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<1))->bm->bminfo;
DWORD rowsize;
DWORD width,height;
LPBYTE row;
LONG left,top,right,bottom;
BYTE blend[4];
LONG xStart=x;
LONG yStart=y;
if(!bitmap)return FALSE;
width=bitmap->infoheader->biWidth;
height=bitmap->infoheader->biHeight;
rowsize=width*4;
right=min(x+cx,width);
bottom=min(y+cy,height);
left=max(x,0);
top=max(y,0);
if(right==left||bottom==top)return TRUE;
row=bitmap->firstRow-top*rowsize+left*4;
if(vertical){
for (y=top; y<bottom; y++) {
LPBYTE thisrow=row;
BlendColors(color1,color2,(y-yStart)*255/(cy),blend);
for (x=left; x<right; x++) {
thisrow[0]=blend[2];
thisrow[1]=blend[1];
thisrow[2]=blend[0];
thisrow[3]=blend[3];
thisrow+=4;
}
row-=rowsize;
}
} else {
LONG ptr=0;
for (x=left; x<right; x++) {
LPBYTE thisrow=row+ptr;
BlendColors(color1,color2,(x-xStart)*255/(cx),blend);
for (y=top; y<bottom; y++) {
thisrow[0]=blend[2];
thisrow[1]=blend[1];
thisrow[2]=blend[0];
thisrow[3]=blend[3];
thisrow-=rowsize;
}
ptr+=4;
}
}
return TRUE;
}

BOOL EXPORTDLL BitmapInvert(LONG object){
RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<1))->bm->bminfo;
DWORD x,y,i;
DWORD rowsize;
DWORD width,height;
LPBYTE row;
if(!bitmap)return FALSE;
width=bitmap->infoheader->biWidth;
height=bitmap->infoheader->biHeight;
rowsize=width*4;
row=bitmap->firstRow;
for (y=0; y<height; y++) {
LPBYTE thisrow=row;
for (x=0;x<width;x++) {
thisrow[0]=~thisrow[0];
thisrow[1]=~thisrow[1];
thisrow[2]=~thisrow[2];
thisrow+=4;
}
row-=rowsize;
}
return TRUE;
}

BOOL EXPORTDLL BitmapBlur(LONG object){
RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<1))->bm->bminfo;
DWORD x,y,i;
DWORD rowsize;
LPBYTE row;
LPBYTE buffer1;
LPBYTE buffer2;
LPBYTE otherbuffer=NULL;
LPBYTE lastrow=NULL;
DWORD width,height;
if(!bitmap)return FALSE;
width=bitmap->infoheader->biWidth;
height=bitmap->infoheader->biHeight;
rowsize=width*4;
row=bitmap->firstRow;
buffer1=malloc(rowsize);
buffer2=malloc(rowsize);
for (y=0; y<height; y++) {
LPBYTE prevrow=(y==0)?row:row+rowsize;
LPBYTE nextrow=(y==height-1)?row:row-rowsize;
LPBYTE thisrow=row;
LPBYTE bufferptr=(y&1) ? buffer2 : buffer1;
otherbuffer=(y&1) ? buffer1 : buffer2;
LONG xptr=0;
for (x=0;x<width;x++) {
LONG prev=(x==0)?0:-4;
LONG next=(x==width-1)?0:4;
for(i=0;i<4;i++){
int c=0;
c+=thisrow[prev+i+xptr]+thisrow[i+xptr]+thisrow[next+i+xptr];
c+=prevrow[prev+i+xptr]+prevrow[i+xptr]+prevrow[next+i+xptr];
c+=nextrow[prev+i+xptr]+nextrow[i+xptr]+nextrow[next+i+xptr];
*bufferptr++=c/9;
}
xptr+=4;
}
if(y>0){
CopyMemory(lastrow,otherbuffer,rowsize);
}
lastrow=row;
row-=rowsize;
}
if(otherbuffer){
CopyMemory(lastrow,otherbuffer,rowsize);
}
free(buffer1);
free(buffer2);
return TRUE;
}

BOOL EXPORTDLL BitmapSteelEffect(LONG object){
RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<1))->bm->bminfo;
DWORD rowsize;
LPBYTE row;
LPBYTE noise;
DWORD width,height;
DWORD x,y,i;
RC4 rc4;
if(!bitmap)return FALSE;
#define RANGE 100
#define RANGE2 50
width=bitmap->infoheader->biWidth;
height=bitmap->infoheader->biHeight;
rowsize=width*4;
row=bitmap->firstRow;
noise=malloc(width*height);
RandomInitSimple(&rc4);
i=0;
for (y=0; y<height; y++) {
for (x=0;x<width;x++) {
noise[i++]=256-RANGE+RandomNext(&rc4,RANGE);
}
}
for (y = 0; y < height; y++){
LPBYTE thisRow=row;
for (x =0; x < width; x++){
int sum =0;
for (i =0; i < RANGE2; i++){
int x1 =x - i;
if (x1 < 0) x1 =width + x1;
sum+=noise[y*width+x1];
}
sum =sum/RANGE2;
thisRow[0]=sum;
thisRow[1]=sum;
thisRow[2]=sum;
thisRow[3]=0xFF;
thisRow+=4;
}
row-=rowsize;
}
free(noise);
return TRUE;
}


Thanks in advance!
-Dargor
 
Depends on what the source code you have looks like. The first thing you should do is download Microsoft Visual C++ 2008 Express. Other than that, I'd have to see the source code. You might need to have MSVC generate some code, or you may just need to start a project and set it to compile as a DLL.
 
I think somewhere there is an option to download it as in iso, and then you'd need something like daemon tools to mount the virtual CD drive.

Once you have it installed, you want to create a Win32 Dll project, and you will see that the wizard will generate an API entry point, and it will also generate an example function. You can look at that example function to see how to export the functions you have.

Alternatively, you could do it all by hand, using this tutorial: http://www.flipcode.com/archives/Creati ... DLLs.shtml
 
I just got Code::Blocks which seems to be fairly straight forward and relatively user-friendly. However I'm unable to compile the code, it says unable do convert DWORD* to BYTE*. Amongst other things. It compiles using GNU GCC Compiler.

I just basically throw the source code into the project (in main.cpp) and try to compile this. It's probably plain bad to do this like that, I'M sure of it. :P
The problem is I have no idea what to do to make it work. Do I need other libraries or anything of the like? Or maybe it's a compatibility problem between my C file and C++?
 
lol, well why don't you show us the "other things"

You might be having a problem because DWORD is not a standard data dtype, it's a Windows data type. I am not sure how much Mingw's Windows stuff works.
 
Here's the build message log
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `void RandomInitSimple(RC4*)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|78|error: cannot convert `DWORD*' to `BYTE*' for argument `2' to `void RandomInit(RC4*, BYTE*, DWORD)'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `void RandomGetBytes(RC4*, void*, size_t)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|85|error: cannot convert `DWORD*' to `BYTE*' for argument `2' to `void RandomInit(RC4*, BYTE*, DWORD)'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|87|error: invalid conversion from `void*' to `BYTE*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapRadialBlur(LONG, LONG, LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|175|error: cannot convert `RGBQUAD*' to `BYTE*' in assignment|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|179|error: invalid conversion from `void*' to `LONG*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|181|error: invalid conversion from `void*' to `LONG*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|185|warning: converting to `long int' from `double'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|186|warning: converting to `long int' from `double'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|188|error: invalid conversion from `void*' to `BYTE*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|192|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|194|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|202|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|203|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|206|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|207|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|225|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|159|warning: unused variable 'count'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapGradientFillRect(LONG, LONG, LONG, LONG, LONG, DWORD, DWORD, BOOL)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|252|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|253|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|257|error: cannot convert `RGBQUAD*' to `BYTE*' in assignment|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|239|warning: unused variable 'tmp'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapInvert(LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|299|error: cannot convert `RGBQUAD*' to `BYTE*' in assignment|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|291|warning: unused variable 'i'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapBlur(LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|327|error: cannot convert `RGBQUAD*' to `BYTE*' in assignment|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|328|error: invalid conversion from `void*' to `BYTE*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|329|error: invalid conversion from `void*' to `BYTE*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapSteelEffect(LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|377|error: cannot convert `RGBQUAD*' to `BYTE*' in assignment|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|378|error: invalid conversion from `void*' to `BYTE*'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|44|warning: 'void DebugOut(char*, ...)' defined but not used|
||=== Build finished: 14 errors, 15 warnings ===|

I have added the source code in the first post.
 
Ok, I fixed all the compile errors, but all I did was a crapload of casting, so you might still get segfault errors when you try to actually run it:

C++:
<span style="color: #339900;">#include <stdio.h>

<span style="color: #339900;">#include <stdlib.h>

<span style="color: #339900;">#include <string.h>

<span style="color: #339900;">#include <windows.h>

<span style="color: #339900;">#include <math.h>

<span style="color: #339900;">#define BOUND(a,l,h) ( ((a)<(l)) ?(l):\

    ( ((a)>(h)) ?(h):(a) )\

    )

<span style="color: #339900;">#define EXPORTDLL __declspec(dllexport) __stdcall

 

////////////////////////////////////////////////////////

 

static void DebugOut(char *fmt,...){

    char s[<span style="color: #0000dd;">1024];

    va_list arg;

    <span style="color: #0000dd;">va_start(arg,fmt);

    vsnprintf(s,<span style="color: #0000dd;">1024,fmt,arg);

    <span style="color: #0000dd;">va_end(arg);

    OutputDebugStringA(s);

    OutputDebugStringA(<span style="color: #666666;">"<span style="color: #666666; font-weight: bold;">\r<span style="color: #666666; font-weight: bold;">\n");

}

 

typedef struct{

    BYTE s[<span style="color: #0000dd;">256];

    BYTE i,j;

    BYTE valid;

} RC4;

void RandomInit(RC4 *rc4, LPBYTE seed, DWORD seedsize){

    BYTE *seedp = (BYTE *) &seed;

    int i;

    BYTE j;

    BYTE tmp;

    for (i = <span style="color: #0000dd;">0; i < <span style="color: #0000dd;">256; i++)

        rc4->s[i] = i;

    for (i = j = <span style="color: #0000dd;">0; i < <span style="color: #0000dd;">256; i++){

        j += rc4->s[i] + seedp[i % seedsize];

        tmp=*(rc4->s+i);

        *(rc4->s+i)=*(rc4->s+j);

        *(rc4->s+j)=tmp;

    }

    rc4->i = rc4->j = <span style="color: #0000dd;">0;

    rc4->valid=TRUE;

}

 

void RandomInitSimple(RC4 *rc4){

    DWORD tc=GetTickCount();

    RandomInit(rc4,(LPBYTE)&tc,<span style="color: #0000dd;">sizeof(tc));

}

void RandomGetBytes(RC4 *rc4, void *buf_, size_t size)

{

    BYTE *buf;

    if (!rc4->valid){

        DWORD seed=<span style="color: #0000dd;">0;

        RandomInit(rc4,(LPBYTE)&seed,<span style="color: #0000dd;">sizeof(seed));

    }

    for (buf = (LPBYTE)buf_; size-- > <span style="color: #0000dd;">0; buf++){

        BYTE tmp, s_k;

        rc4->i++;

        rc4->j += rc4->s[rc4->i];

        tmp=*(rc4->s+rc4->i);

        *(rc4->s+rc4->i)=*(rc4->s+rc4->j);

        *(rc4->s+rc4->j)=tmp;

        s_k = rc4->s[rc4->i]+rc4->s[rc4->j];

        *buf = rc4->s[s_k];

    }

}

DWORD RandomNext(RC4 *rc4, DWORD count){

    DWORD rnd=<span style="color: #0000dd;">0;

    if(count==<span style="color: #0000dd;">0)return <span style="color: #0000dd;">0;

    RandomGetBytes(rc4,&rnd,<span style="color: #0000dd;">sizeof(rnd));

    return rnd%count;

}

 

////////////////////////////////////////////////////////

 

typedef struct{

    DWORD flags;

    DWORD klass;

    void (*dmark)(void*);

    void (*dfree)(void*);

    double *data;//red is index 1, green is index 2, blue 3, alpha 4

} RGSSCOLOR;

 

typedef struct{

    DWORD unk1;

    DWORD unk2;

    BITMAPINFOHEADER *infoheader;

    RGBQUAD *firstRow;

    RGBQUAD *lastRow;

} RGSSBMINFO;

 

typedef struct{

    DWORD unk1;

    DWORD unk2;

    RGSSBMINFO *bminfo;

} BITMAPSTRUCT;

 

typedef struct{

    DWORD flags;

    DWORD klass;

    void (*dmark)(void*);

    void (*dfree)(void*);

    BITMAPSTRUCT *bm;

} RGSSBITMAP;

 

<span style="color: #339900;">#define ASSERT(x) if(!x){DebugOut("Failed: %s: %d",#x,__LINE__);}

 

void BlendColors(DWORD crDst, DWORD crSrc, BYTE alpha, BYTE *colors){

    int r,g,b,a;

    r=GetRValue(crDst);

    g=GetGValue(crDst);

    b=GetBValue(crDst);

    a=(crDst>><span style="color: #0000dd;">24);

    colors[<span style="color: #0000dd;">0]=(alpha*((int)GetRValue(crSrc)-r)/<span style="color: #0000dd;">255)+r;

    colors[<span style="color: #0000dd;">1]=(alpha*((int)GetGValue(crSrc)-g)/<span style="color: #0000dd;">255)+g;

    colors[<span style="color: #0000dd;">2]=(alpha*((int)GetBValue(crSrc)-b)/<span style="color: #0000dd;">255)+b;

    colors[<span style="color: #0000dd;">3]=(alpha*((int)(crDst>><span style="color: #0000dd;">24)-a)/<span style="color: #0000dd;">255)+a;

}

 

inline LONG BlurReflect(LONG x, LONG d){

    if(((<span style="color: #0000dd;">abs(x) / d) & <span style="color: #0000dd;">1) == <span style="color: #0000dd;">0)

        return (x < <span style="color: #0000dd;">0) ? (-x % d) : (x % d);

    else

        return (x > <span style="color: #0000dd;">0) ? (d - (x % d)) : (d - (-x % d));

}

 

BOOL EXPORTDLL BitmapRadialBlur(LONG object, LONG ang, LONG samples){

    int x, y, i, count;

    LONG *ct, *st;

    float angle, theta, offset;

    LPBYTE firstRow;

    LPBYTE destbuf,dest;

    RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<<span style="color: #0000dd;">1))->bm->bminfo;

    DWORD rowsize;

    DWORD width,height;

    LPBYTE row;

    DWORD halfwidth,halfheight;

    if(!bitmap)return FALSE;

    width=bitmap->infoheader->biWidth;

    height=bitmap->infoheader->biHeight;

    halfwidth=(width>><span style="color: #0000dd;">1);

    halfheight=(height>><span style="color: #0000dd;">1);

    rowsize=width*<span style="color: #0000dd;">4;

    firstRow=(LPBYTE)bitmap->firstRow;

    samples=max(samples,<span style="color: #0000dd;">2);

    angle=BOUND(ang,<span style="color: #0000dd;">0,<span style="color: #0000dd;">360)*<span style="color: #0000dd;">0.0174532;

    theta=angle/(samples<span style="color: #0000dd;">-1);

    ct=(LONG*)<span style="color: #0000dd;">malloc(samples*<span style="color: #0000dd;">sizeof(LONG));

    if(!ct)return FALSE;

    st=(LONG*)<span style="color: #0000dd;">malloc(samples*<span style="color: #0000dd;">sizeof(LONG));

    if(!st){<span style="color: #0000dd;">free(ct);return FALSE;}

    offset = theta*(samples<span style="color: #0000dd;">-1)/<span style="color: #0000dd;">2;

    for (i = <span style="color: #0000dd;">0; i < samples; i++) {

        ct[i] = <span style="color: #0000dd;">cos(theta*i-offset)*<span style="color: #0000dd;">4096.0;

        st[i] = <span style="color: #0000dd;">sin(theta*i-offset)*<span style="color: #0000dd;">4096.0;

    }

    destbuf = (LPBYTE)<span style="color: #0000dd;">malloc(<span style="color: #0000dd;">4*width*height);

    if(!destbuf){<span style="color: #0000dd;">free(ct);free(st);return FALSE;}

    row=firstRow;

    dest=destbuf;

    for (y=<span style="color: #0000dd;">0; y<height; y++) {

        LPBYTE thisrow=row;

        for (x=<span style="color: #0000dd;">0;x<width;x++) {

            LONG xr = x-halfwidth;

            LONG yr = y-halfheight;

            LONG sumR=<span style="color: #0000dd;">0, sumG = <span style="color: #0000dd;">0, sumB = <span style="color: #0000dd;">0, sumA=<span style="color: #0000dd;">0;

            for (i = <span style="color: #0000dd;">0; i < samples; i++) {

                LPBYTE src;

                LONG xx = halfwidth + ((xr*ct[i])>><span style="color: #0000dd;">12) - ((yr*st[i])>><span style="color: #0000dd;">12);

                LONG yy = halfheight + ((xr*st[i])>><span style="color: #0000dd;">12) + ((yr*ct[i])>><span style="color: #0000dd;">12);

                if ((yy < <span style="color: #0000dd;">0) || (yy >= height) ||

                    (xx < <span style="color: #0000dd;">0) || (xx >= width)){

                        xx=BlurReflect(xx,width);

                        yy=BlurReflect(yy,height);

                        xx=BOUND(xx,<span style="color: #0000dd;">0,width<span style="color: #0000dd;">-1);

                        yy=BOUND(yy,<span style="color: #0000dd;">0,height<span style="color: #0000dd;">-1);

                }

                src=firstRow-(yy*rowsize)+(xx<<<span style="color: #0000dd;">2);

                sumB += src[<span style="color: #0000dd;">0];

                sumG += src[<span style="color: #0000dd;">1];

                sumR += src[<span style="color: #0000dd;">2];

                sumA += src[<span style="color: #0000dd;">3];

            }

            *dest++ = sumB/samples;

            *dest++ = sumG/samples;

            *dest++ = sumR/samples;

            *dest++ = sumA/samples;

            thisrow+=<span style="color: #0000dd;">4;

        }

        row-=rowsize;

    }

    row=firstRow;

    dest=destbuf;

    for (y = <span style="color: #0000dd;">0; y < height; y++) {

        LPBYTE thisrow=row;

        CopyMemory(thisrow,dest,rowsize);

        dest+=rowsize;

        row-=rowsize;

    }

    <span style="color: #0000dd;">free(destbuf);

    <span style="color: #0000dd;">free(ct);

    <span style="color: #0000dd;">free(st);

    return TRUE;

}

 

BOOL EXPORTDLL BitmapGradientFillRect(

                                      LONG object, LONG x, LONG y, LONG cx, LONG cy, DWORD color1, DWORD color2, BOOL vertical){

                                          DWORD tmp=<span style="color: #0000dd;">0;

                                          RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<<span style="color: #0000dd;">1))->bm->bminfo;

                                          DWORD rowsize;

                                          DWORD width,height;

                                          LPBYTE row;

                                          LONG left,top,right,bottom;

                                          BYTE blend[<span style="color: #0000dd;">4];

                                          LONG xStart=x;

                                          LONG yStart=y;

                                          if(!bitmap)return FALSE;

                                          width=bitmap->infoheader->biWidth;

                                          height=bitmap->infoheader->biHeight;

                                          rowsize=width*<span style="color: #0000dd;">4;

                                          right=min(x+cx,width);

                                          bottom=min(y+cy,height);

                                          left=max(x,<span style="color: #0000dd;">0);

                                          top=max(y,<span style="color: #0000dd;">0);

                                          if(right==left||bottom==top)return TRUE;

                                          row=(LPBYTE)bitmap->firstRow-top*rowsize+left*<span style="color: #0000dd;">4;

                                          if(vertical){

                                              for (y=top; y<bottom; y++) {

                                                  LPBYTE thisrow=row;

                                                  BlendColors(color1,color2,(y-yStart)*<span style="color: #0000dd;">255/(cy),blend);

                                                  for (x=left; x<right; x++) {

                                                      thisrow[<span style="color: #0000dd;">0]=blend[<span style="color: #0000dd;">2];

                                                      thisrow[<span style="color: #0000dd;">1]=blend[<span style="color: #0000dd;">1];

                                                      thisrow[<span style="color: #0000dd;">2]=blend[<span style="color: #0000dd;">0];

                                                      thisrow[<span style="color: #0000dd;">3]=blend[<span style="color: #0000dd;">3];

                                                      thisrow+=<span style="color: #0000dd;">4;

                                                  }

                                                  row-=rowsize;

                                              }

                                          } else {

                                              LONG ptr=<span style="color: #0000dd;">0;

                                              for (x=left; x<right; x++) {

                                                  LPBYTE thisrow=row+ptr;

                                                  BlendColors(color1,color2,(x-xStart)*<span style="color: #0000dd;">255/(cx),blend);

                                                  for (y=top; y<bottom; y++) {

                                                      thisrow[<span style="color: #0000dd;">0]=blend[<span style="color: #0000dd;">2];

                                                      thisrow[<span style="color: #0000dd;">1]=blend[<span style="color: #0000dd;">1];

                                                      thisrow[<span style="color: #0000dd;">2]=blend[<span style="color: #0000dd;">0];

                                                      thisrow[<span style="color: #0000dd;">3]=blend[<span style="color: #0000dd;">3];

                                                      thisrow-=rowsize;

                                                  }

                                                  ptr+=<span style="color: #0000dd;">4;

                                              }

                                          }

                                          return TRUE;

}

 

BOOL EXPORTDLL BitmapInvert(LONG object){

    RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<<span style="color: #0000dd;">1))->bm->bminfo;

    DWORD x,y,i;

    DWORD rowsize;

    DWORD width,height;

    LPBYTE row;

    if(!bitmap)return FALSE;

    width=bitmap->infoheader->biWidth;

    height=bitmap->infoheader->biHeight;

    rowsize=width*<span style="color: #0000dd;">4;

    row=(LPBYTE)bitmap->firstRow;

    for (y=<span style="color: #0000dd;">0; y<height; y++) {

        LPBYTE thisrow=row;

        for (x=<span style="color: #0000dd;">0;x<width;x++) {

            thisrow[<span style="color: #0000dd;">0]=~thisrow[<span style="color: #0000dd;">0];

            thisrow[<span style="color: #0000dd;">1]=~thisrow[<span style="color: #0000dd;">1];

            thisrow[<span style="color: #0000dd;">2]=~thisrow[<span style="color: #0000dd;">2];

            thisrow+=<span style="color: #0000dd;">4;

        }

        row-=rowsize;

    }

    return TRUE;

}

 

BOOL EXPORTDLL BitmapBlur(LONG object){

    RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<<span style="color: #0000dd;">1))->bm->bminfo;

    DWORD x,y,i;

    DWORD rowsize;

    LPBYTE row;

    LPBYTE buffer1;

    LPBYTE buffer2;

    LPBYTE otherbuffer=NULL;

    LPBYTE lastrow=NULL;

    DWORD width,height;

    if(!bitmap)return FALSE;

    width=bitmap->infoheader->biWidth;

    height=bitmap->infoheader->biHeight;

    rowsize=width*<span style="color: #0000dd;">4;

    row=(LPBYTE)bitmap->firstRow;

    buffer1=(LPBYTE)<span style="color: #0000dd;">malloc(rowsize);

    buffer2=(LPBYTE)<span style="color: #0000dd;">malloc(rowsize);

    for (y=<span style="color: #0000dd;">0; y<height; y++) {

        LPBYTE prevrow=(y==<span style="color: #0000dd;">0)?row:row+rowsize;

        LPBYTE nextrow=(y==height<span style="color: #0000dd;">-1)?row:row-rowsize;

        LPBYTE thisrow=row;

        LPBYTE bufferptr=(y&<span style="color: #0000dd;">1) ? buffer2 : buffer1;

        otherbuffer=(y&<span style="color: #0000dd;">1) ? buffer1 : buffer2;

        LONG xptr=<span style="color: #0000dd;">0;

        for (x=<span style="color: #0000dd;">0;x<width;x++) {

            LONG prev=(x==<span style="color: #0000dd;">0)?<span style="color: #0000dd;">0:<span style="color: #0000dd;">-4;

            LONG next=(x==width<span style="color: #0000dd;">-1)?<span style="color: #0000dd;">0:<span style="color: #0000dd;">4;

            for(i=<span style="color: #0000dd;">0;i<<span style="color: #0000dd;">4;i++){

                int c=<span style="color: #0000dd;">0;

                c+=thisrow[prev+i+xptr]+thisrow[i+xptr]+thisrow[next+i+xptr];

                c+=prevrow[prev+i+xptr]+prevrow[i+xptr]+prevrow[next+i+xptr];

                c+=nextrow[prev+i+xptr]+nextrow[i+xptr]+nextrow[next+i+xptr];

                *bufferptr++=c/<span style="color: #0000dd;">9;

            }

            xptr+=<span style="color: #0000dd;">4;

        }

        if(y><span style="color: #0000dd;">0){

            CopyMemory(lastrow,otherbuffer,rowsize);

        }

        lastrow=row;

        row-=rowsize;

    }

    if(otherbuffer){

        CopyMemory(lastrow,otherbuffer,rowsize);

    }

    <span style="color: #0000dd;">free(buffer1);

    <span style="color: #0000dd;">free(buffer2);

    return TRUE;

}

 

BOOL EXPORTDLL BitmapSteelEffect(LONG object){

    RGSSBMINFO *bitmap=((RGSSBITMAP*)(object<<<span style="color: #0000dd;">1))->bm->bminfo;

    DWORD rowsize;

    LPBYTE row;

    LPBYTE noise;

    DWORD width,height;

    DWORD x,y,i;

    RC4 rc4;

    if(!bitmap)return FALSE;

<span style="color: #339900;">#define RANGE 100

<span style="color: #339900;">#define RANGE2 50

    width=bitmap->infoheader->biWidth;

    height=bitmap->infoheader->biHeight;

    rowsize=width*<span style="color: #0000dd;">4;

    row=(LPBYTE)bitmap->firstRow;

    noise=(LPBYTE)<span style="color: #0000dd;">malloc(width*height);

    RandomInitSimple(&rc4);

    i=<span style="color: #0000dd;">0;

    for (y=<span style="color: #0000dd;">0; y<height; y++) {

        for (x=<span style="color: #0000dd;">0;x<width;x++) {

            noise[i++]=<span style="color: #0000dd;">256-RANGE+RandomNext(&rc4,RANGE);

        }

    }

    for (y = <span style="color: #0000dd;">0; y < height; y++){

        LPBYTE thisRow=row;

        for (x =<span style="color: #0000dd;">0; x < width; x++){

            int sum =<span style="color: #0000dd;">0;

            for (i =<span style="color: #0000dd;">0; i < RANGE2; i++){

                int x1 =x - i;

                if (x1 < <span style="color: #0000dd;">0) x1 =width + x1;

                sum+=noise[y*width+x1];

            }

            sum =sum/RANGE2;

            thisRow[<span style="color: #0000dd;">0]=sum;

            thisRow[<span style="color: #0000dd;">1]=sum;

            thisRow[<span style="color: #0000dd;">2]=sum;

            thisRow[<span style="color: #0000dd;">3]=0xFF;

            thisRow+=<span style="color: #0000dd;">4;

        }

        row-=rowsize;

    }

    <span style="color: #0000dd;">free(noise);

    return TRUE;

}
 
That's awesome, it works! I can compile it, with a few warnings, but it seems to work!
However, now that I try to use it in RMXP i get an error saying: GetProcAdress: BitmapTestEffect or BitmapTestEffectA

It seems that the script is unable to get the new process or any other process that was already there.
 
I have added BitmapTestEffect myself. But I can't call any of the functions! :/

Now here's the new build message I have (now that I can compile the source)
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapRadialBlur(LONG, LONG, LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|154|warning: converting to `long int' from `double'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|155|warning: converting to `long int' from `double'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|161|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|163|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|171|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|172|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|175|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|176|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|194|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|128|warning: unused variable 'count'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapGradientFillRect(LONG, LONG, LONG, LONG, LONG, DWORD, DWORD, BOOL)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|220|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|221|warning: comparison between signed and unsigned integer expressions|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|207|warning: unused variable 'tmp'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapInvert(LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|259|warning: unused variable 'i'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp||In function `BOOL BitmapTestEffect(LONG)':|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|284|warning: unused variable 'i'|
D:\Games\RPG Maker XP\C Bitmap\C\RGSS Bitnap 2\main.cpp|13|warning: 'void DebugOut(char*, ...)' defined but not used|
||=== Build finished: 0 errors, 16 warnings ===|
 
lol, duh. You have to actually export the symbols XD. It's been like 8 months since I made a DLL for RMXP, so I forgot you still had to explicitly export the symbols. Add "extern "C" __declspec(dllexport)" on the line just before each function you want to export.
 
It compiles but changes nothing. Maybe I'm doing something wrong?
I did it like that
Code:
 

extern "C" __declspec(dllexport)

BOOL EXPORTDLL BitmapTestEffect(LONG object){

...

 
 
Hello,

the export was already defined with EXPORTDLL (line 9) :
Code:
# #define EXPORTDLL __declspec(dllexport) __stdcall

So you just have to modify this line with "extern "C"".

The problem might come from decorated names. When you open your .dll with a text editor and search for the names of your functions, do they have strange suffixes ?
 
When I open the dll with a text editor I see some fucked up things! 99% it not readable but I found this:

RGSS Bitmap 2.dll [NUL] _Z10BitmapBlurl [NUL] _Z12BitmapInvertl [NUL] _Z16BitmapRadialBlurlll [NUL] _Z17BitmapSteelEffectl [NUL] _Z22BitmapGradientFillRectlllllmmi [NUL]

And I can't find my function in there.

By the way, thank you very much guys. Your help is greatly appreciated. :)
 
The names are odd...
You should try to build your .dll with "__cdecl" instead of "__stdcall" (line 9).
After this modification, are the names still weird in the .dll ?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top