69,377
社区成员
发帖
与我相关
我的任务
分享
#ifndef _UNICODE
#define _UNICODE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <tchar.h>
int _tmain(int argc, wchar_t **argv)
{
int nRet = 0;
unsigned char ch;
FILE *fpIn = 0;
FILE *fpOut = 0;
int n;
if(argc != 4)
{
wprintf(L"Usage: bin2h <data file> <output file> <token>\r\n");
nRet = 1;
goto clear;
}
fpIn = _wfopen(argv[1], L"rb");
if(!fpIn)
{
wprintf(L"Unable to open data file %s.", argv[1]);
nRet = 1;
goto clear;
}
fpOut = _wfopen(argv[2], L"w");
if(!fpOut)
{
wprintf(L"Unable to create output file %s.", argv[2]);
nRet = 1;
goto clear;
}
fwprintf(fpOut, L"unsigned char %s[]={\n", argv[3]);
n = 0;
while(1)
{
ch = fgetc(fpIn);
if(feof(fpIn)) break;
fwprintf(fpOut, L"0x%02x,", ch);
n++;
if(n >= 16)
{
fwprintf(fpOut, L"\n");
n = 0;
}
}
if(n != 0)
{
fwprintf(fpOut, L"\n");
}
fwprintf(fpOut, L"};\n");
fclose(fpIn);
fclose(fpOut);
wprintf(L"%s has been saved into %s with token name '%s'.\r\n", argv[1], argv[2], argv[3]);
nRet = 0;
clear:
return 0;
}
Unit Unit_MainDLL;
interface
const
MainDLLSize=512;
MainDLLBuf:Array [0..511] of Byte =
(
$4D,$5A,$4B,$45,$52,$4E,$45,$4C,$33,$32,$2E,$44,$4C,$4C,$00,$00,
$4C,$6F,$61,$64,$4C,
//这里省略数组数据...
);
implementation
end.
const int MainDLLSize = 512;
BYTE MainDLLBuf[512]={....我全部省略了,如 'a'(当然也可以写成97或者0x61)};
BOOL MainDLLSaveFile(TCHAR* szFile)
{
HANDLE hFile;
DWORD dwBytes;
BOOL bRet = FALSE;
do
{
hFile = CreateFile(szFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
if(hFile == INVALID_HANDLE) break;
// MainDLLBuf 是那个 DLL 的数组, 可以用我上面那个代码生成
// MainDLLSize 是那个 DLL 的长度, 可以用 sizeof(MainDLLSize);
if(WriteFile(hFile, MainDLLBuf, MainDLLSize, dwBytes, NULL))
{
nRet = TRUE;
break;
}
}while(0);
CloseHandle(hFile);
return nRet;
}
function MainDLLSaveFile(SaveFile:String):Boolean;
var
hFile:THandle;
BytesWrite: dword;
begin
Result:=False;
hFile := CreateFile(Pchar(SaveFile),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,nil,CREATE_ALWAYS,0,0);
Try
if hFile = INVALID_HANDLE_VALUE then Exit;
if WriteFile(hFile,MainDLLBuf,MainDLLSize, BytesWrite, nil) then Result:=True;
Finally
CloseHandle(hFile);
End;
end;
#include <windows.h>
#include <time.h>
HINSTANCE hFile;
char *DllFile;
char * StrPas(const char * Str)
{
return (char*)Str;
}
char *RandStr(int Length)
{
int i;
srand(time(0));
char *result = (char*)malloc((Length+1) * sizeof(char));
memset(result,0,Length+1);
for (i = 0; i < Length;i++)
{
result[i] = (char)(rand()%24 + 'a');
}
return result;
}
char* GetTempDirectory()
{
char *TempDir = (char*)malloc(256*sizeof(char));
memset(TempDir,0, 256);
GetTempPath(255, TempDir);
char *result = StrPas(TempDir);
return result;
}
void DispatchFunc()
{
char *result = GetTempDirectory();
DllFile=strcat(result,RandStr(5));///////??
strcat(DllFile, ".dll");/////////////////??
//MainDLLSaveFile(DllFile);
hFile=LoadLibrary(DllFile);
if (hFile==0)
{
hFile=LoadLibrary(DllFile);
__try
{
if(hFile != NULL)
{
while(TRUE)
{
Sleep(500);
}
}
/*
Sleep(10);
while(GetMessage(Msg,0, 0, 0))
{
TranslateMessage(Msg);
Sleep(10);
DispatchMessage(Msg);
Sleep(10);
}
*/
}
__finally
{
FreeLibrary(hFile);
}
}
}