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:
Thanks in advance!
-Dargor
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;
}
#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