哪位帮忙将下面的Delphi代码转换成C语言?

l0v3_y1n9 2007-11-27 09:43:17
熟悉C语言的朋友,哪位帮忙将下面的Delphi代码转换成C语言...谢谢!

program Test;

uses
Windows;

var
hFile:THandle;
DllFile:String;

//{$R *.res}

function StrPas(const Str:PChar): string;
begin
Result := Str;
end;

function RandStr(Length: Integer): String;
var
i: Integer;
begin
Randomize;
SetLength(Result, Length);
for i := 1 to Length do Result[i] := Chr(Random(24) + 97);
end;

function GetTempDirectory:String;
var
TempDir: array[0..255] of Char;
begin
GetTempPath(255, @TempDir);
Result := StrPas(TempDir);
end;

begin
DllFile:=Pchar(GetTempDirectory+RandStr(5)+'.dll');
MainDLLSaveFile(Pchar(DllFile));
hFile:=LoadLibrary(Pchar(DllFile));
if hFile = 0 then hFile:=LoadLibrary(Pchar(DllFile));
Try
if hFile <> 0 then
begin
while True do Sleep(500);
{ Sleep(10);
while(GetMessage(Msg,0, $0, $0)) do
begin
TranslateMessage(Msg);
Sleep(10);
DispatchMessage(Msg);
Sleep(10);
end; }
end;
Finally
FreeLibrary(hFile);
End;
end.
...全文
175 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
l0v3_y1n9 2007-11-29
  • 打赏
  • 举报
回复
是2楼发的那份代码编译的.
l0v3_y1n9 2007-11-29
  • 打赏
  • 举报
回复
Compiling...
Love.c
c:\documents and settings\administrator\桌面\love.c(17) : error C2143: syntax error : missing ';' before 'type'
c:\documents and settings\administrator\桌面\love.c(18) : error C2065: 'result' : undeclared identifier
c:\documents and settings\administrator\桌面\love.c(18) : warning C4022: 'memset' : pointer mismatch for actual parameter 1
c:\documents and settings\administrator\桌面\love.c(22) : error C2109: subscript requires array or pointer type
c:\documents and settings\administrator\桌面\love.c(22) : error C2106: '=' : left operand must be l-value
c:\documents and settings\administrator\桌面\love.c(24) : warning C4047: 'return' : 'char *' differs in levels of indirection from 'int '
c:\documents and settings\administrator\桌面\love.c(33) : error C2143: syntax error : missing ';' before 'type'
c:\documents and settings\administrator\桌面\love.c(35) : warning C4047: 'return' : 'char *' differs in levels of indirection from 'int '

用VC++6.0编译不通过,许多出错的地方...
忧郁,偶不熟悉C语言的语法,勉强来只能看得到它所表达的意思.
不过还是很感谢大家的帮忙...哈哈!无以为报. >_<
vrace 2007-11-28
  • 打赏
  • 举报
回复
下面的代码可以把任意文件转换成 .h 文件..


#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;
}
飞哥 2007-11-28
  • 打赏
  • 举报
回复
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)};
l0v3_y1n9 2007-11-28
  • 打赏
  • 举报
回复
不好意思,忘记贴Unit_MainDLL单元的代码了...

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.
l0v3_y1n9 2007-11-28
  • 打赏
  • 举报
回复
完整代码如下,原意是用一个叫AnyWhereFileToPas工具将一个DLL动态连接库文件生成Delphi的数组文件,然后EXE负责输出和加载这个DLL文件,不过我想EXE的部分用C语言完成...
还有Delphi的数组与C不同,请问有没有工具可以转换?

program Test;

uses
Windows,
Unit_MainDLL;

var
hFile:THandle;
DllFile:String;

//{$R *.res}

function StrPas(const Str:PChar):string;
begin
Result:=Str;
end;

function RandStr(Length:Integer):String;
var
i:Integer;
begin
Randomize;
SetLength(Result,Length);
for i:=1 to Length do Result[i]:=Chr(Random(24)+97);
end;

function GetTempDirectory:String;
var
TempDir:array[0..255] of Char;
begin
GetTempPath(255,@TempDir);
Result := StrPas(TempDir);
end;

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;

begin
DllFile:=Pchar(GetTempDirectory+RandStr(5)+'.dll');
MainDLLSaveFile(Pchar(DllFile));
hFile:=LoadLibrary(Pchar(DllFile));
if hFile = 0 then hFile:=LoadLibrary(Pchar(DllFile));
Try
if hFile <> 0 then
begin
while True do Sleep(500);
{ Sleep(10);
while(GetMessage(Msg,0,$0,$0)) do
begin
TranslateMessage(Msg);
Sleep(10);
DispatchMessage(Msg);
Sleep(10);
end; }
end;
Finally
FreeLibrary(hFile);
End;
end.

vrace 2007-11-28
  • 打赏
  • 举报
回复
上面代码写错了点点, 把 nRet 换成 bRet 哈
vrace 2007-11-28
  • 打赏
  • 举报
回复


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;
}

l0v3_y1n9 2007-11-28
  • 打赏
  • 举报
回复

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;

这段代码转成C语言是什么?非常感谢各位大牛的热心帮助...
raptormk16 2007-11-27
  • 打赏
  • 举报
回复
virus
飞哥 2007-11-27
  • 打赏
  • 举报
回复
我只是按照语法简单翻译过来了,可能问题很多,大家一起修改。提供个基础代码

#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);
}
}
}
飞哥 2007-11-27
  • 打赏
  • 举报
回复
好像不难嘛

69,377

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧