Jump to content
Tuts 4 You

[Delphi] Snippets (Rev 0.3)


0xFF

Recommended Posts

Posted (edited)

Before flaming, this is a very old peice of snippet i had laying in my hd, so think before being a smart *** (example by kao is available below) and going "WHAT ????? you could of THIS instead of THIS...", no **** ?


unit ToolHelp;
interface
uses Tlhelp32, Windows;
procedure SuspendProcessThreads( pid: cardinal );
procedure ResumeProcessThreads( pid: cardinal );
Procedure UnprotectRegion(nRegion: Integer; dwRegionSize: DWORD);
Procedure ByteJmp(Address : Integer);
function WCharToStr(wideStr: PChar): String;
procedure DbgLog(DbgStr: String);
implementation
Procedure UnprotectRegion(nRegion: Integer; dwRegionSize: DWORD);
var
oldProtect: DWORD;
Begin
VirtualProtect( Ptr(nRegion), dwRegionSize, PAGE_READWRITE, oldProtect );
End;
Procedure ByteJmp(Address : Integer);
Begin
UnprotectRegion(Address, SizeOf(BYTE));
PBYTE( Ptr(Address) )^ := $EB;
End;
function WCharToStr(wideStr: PChar): String;
begin
Result := WideCharToString( wideStr );
end;
procedure DbgLog(DbgStr: String);
var
Dest: Array [0..254] Of WideChar;
begin
if Length(DbgStr) > 255 then Exit;
FillChar( Dest, SizeOf(Dest), #0 ); // initialize local variable
StringToWideChar( DbgStr, Dest, sizeof(Dest) );
OutputDebugString( Dest );
end;
procedure SuspendProcessThreads( pid: cardinal );
var
lppe: TThreadEntry32;
hSnapShot: Cardinal;
begin
lppe.dwSize := sizeof(lppe);
hSnapShot := CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, pid );
if Thread32First( hSnapShot, lppe ) <> False then
begin
SuspendThread( lppe.th32ThreadID );
while Thread32Next( hSnapShot, lppe ) do
begin
SuspendThread( lppe.th32ThreadID );
end;
end
else
begin
CloseHandle( hSnapShot );
Exit;
end;
CloseHandle( hSnapShot );
end;
procedure ResumeProcessThreads( pid: cardinal );
var
lppe: TThreadEntry32;
hSnapShot: Cardinal;
begin
lppe.dwSize := sizeof(lppe);
hSnapShot := CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, pid );
if Thread32First( hSnapShot, lppe ) <> False then
begin
ResumeThread( lppe.th32ThreadID );
while Thread32Next( hSnapShot, lppe ) do
begin
ResumeThread( lppe.th32ThreadID );
end;
end
else
begin
CloseHandle( hSnapShot );
Exit;
end;
CloseHandle( hSnapShot );
end;
end.
Edited by rotem156
  • Like 1
Posted (edited)

it should be


function WCharToStr(wideStr: PChar): String;
begin
Result := WideCharToString( wideStr );
end;

to


function WCharToStr(wideStr: PWideChar): String;
begin
Result := WideCharToString( wideStr );
end;
Edited by Encrypto
Posted

it should be


function WCharToStr(wideStr: PChar): String;
begin
Result := WideCharToString( wideStr );
end;

to


function WCharToStr(wideStr: PWideChar): String;
begin
Result := WideCharToString( wideStr );
end;

Depends on your Delphi version.

Around Delphi 2009 or something they changed Char from AnsiChar to WideChar. PChar is a pointer to Char so it changed accordingly.

Posted

@Rotem156: Could you please learn programming before you post such utter garbage? Pretty please?

1) function DbgLog - absolutely unnecessary conversions and local buffer initialization. This will do just fine:


OutputDebugString(PChar(DbgStr));

2) SuspendProcessThreads and ResumeProcessThreads are broken and non-working in so many ways:

a. SuspendThread and ResumeThread needs thread handle, not thread id. See MSDN.

b. th32ProcessID argument for CreateToolhelp32Snapshot does not work with TH32CS_SNAPTHREAD. Instead, you should enumerate all threads and compare th32OwnerProcessID field. See MSDN.

c. CreateToolhelp32Snapshot can fail but you do check the return value before using it. Again, see MSDN;

d. 2 calls to CloseHandle and call to Exit shows bad design. Here is much more readable way:


result := Thread32First( hSnapShot, lppe );
while result <> False do begin
//
//do some magic with lppe
//
result := Thread32Next( hSnapShot, lppe );
end;
CloseHandle( hSnapShot );

3) function like WCharToStr which does nothing but calls WideCharToString with same parameters is pointless. One should use WideCharToString directly.

  • Like 1
Posted (edited)

@kao: this was a very old snippet i had, no need to get excited about it... was just sharing the target of it..

showing how excited you're about fixing someone's code, means you're not a programmer yourself, there for i'm not excited from your "knowledge".

@Encrypto: what Killboy said it true, this was written in a newer verion of Delphi (XE) so by default they changed WideChar to be default.

Edited by rotem156
  • Like 1
Posted

Indeed its true, but for backwards compatibility its good to design code that way. :) I have many projects in delphi XE and its a PAIN to get it to work with d7. So when I write something, I make sure that its backwards compatible, because to be honest, Delphi 7 rules! :D

Posted

result := Thread32First( hSnapShot, lppe );
while result <> False do begin
//
//do some magic with lppe
//
result := Thread32Next( hSnapShot, lppe );
end;
CloseHandle( hSnapShot );

No need for "Is not equal to False" on boolean eval, it is much simpler like this:


Result := Thread32First( hSnapShot, lppe );
While Result Do Begin
//
//do some magic with lppe
//
Result := Thread32Next( hSnapShot, lppe );
End;

Delphi 7 rules! biggrin.png

So true, Delphi 7 Second Edition is what I use - def worth searching for if you're using 7.1 or 7.0 wink.png

Have fun!

BoB

Posted

Before flaming, this is a very old peice of snippet i had laying in my hd, so think before being a smart *** (example by kao is available below) and going "WHAT ????? you could of THIS instead of THIS...", no **** ?

plus

showing how excited you're about fixing someone's code, means you're not a programmer yourself, there for i'm not excited from your "knowledge".

Hold on a minute... YOU were the one who posted this in a public forum for all to read/see. Nobody else, YOU. If you don't want people to suggest or comment then either don't post it all in the first place OR post something which cannot be corrected or which nothing can be suggested for. Sheesh, you could even post it on PasteBin or such and share the link which would mean that way your post would be the only one people get to read, thus avoiding the responses that obviously cause you so much anger...

If people have suggestions for your code instead of becoming defensive and attacking people, especially in such a generic way, you should at least take the time to see what they have to say.

The worst case scenario is that you waste 30 seconds reading another persons post, the best case scenario though is that you might take something away from it, learning something. Either way, it is much better than posting an offering and then becoming indignant when people make comments that don't fit your desired reaction of: "Wow", "I'm adequately awestruck now" or "Omfgwtf!?!?! This guy is coding in the flesh!!!".

It is a public forum where people can share and exchange ideas and information, a place where we can all come and interact and i honestly have not seen anywhere that special consideration was to be given to 'rotem156', so pull your head in and conduct yourself a little more appropriately.

To say that somebody is not a programmer based on their perceived enthusiasm, then to tell them that because they've disagreed with you that you have no interest in their "knowledge" is plain arrogance, twofold. Who do you think you are that your opinion in any matter at all is more important than that of another (senior) member? What degree or doctorate do you hold for coding/application design that allows you to disregard any and all comments or suggestions?

HR,

Ghandi

  • Like 2
Posted

Don't get frustrated with him ghandi, most, if not all of this is just copy pasted from some other site. He's a known ripper.

Posted (edited)

Don't get frustrated with him ghandi, most, if not all of this is just copy pasted from some other site. He's a known ripper.

Yup, he copied it from here:

http://www.cheatengi...edc6b92063cb1be

And that code was under the "Random spam (Post all your useless crap here)" forum. :D

Edited by Saduff
Posted

Yup, he copied it from here:

http://www.cheatengi...edc6b92063cb1be

And that code was under the "Random spam (Post all your useless crap here)" forum. biggrin.png

Nah thats him on CEs forums. Hes known on there as a ripper since everything he ever posted on that forum was easily found online elsewhere that he copied.

Posted

Ripper or not, I agree with Ghandi, he should be taking the advice and learning from it, He also said it was "Old" source code, So if its old source he should have fixed it before publishing it, with his latest knowledge in coding with Delphi. The source is basic and really is not important if he ripped it or not, Even rippers have to learn something they copied and pasted eventually. Its not hard to tell in Delphi if someone has copy and pasted due to "Coding Style" they way they use indentation, Declare variables, Spaces, naming of functions ect.. ect.. just grab some other snippets that he has posted in the past and see if the coding style is the same. But like I said its completely irrelevant. I hope he did learn something new or found new ways to improve his coding as good Delphi programmers are getting harder to find.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...