哪位帮忙将下面的Delphi代码转换成C语言?
熟悉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.
[解决办法]
我只是按照语法简单翻译过来了,可能问题很多,大家一起修改。提供个基础代码
#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); } }}
[解决办法]
下面的代码可以把任意文件转换成 .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;}