File access in DLL for windows hook

Posted on 2002-07-25
Last Modified: 2010-04-04
With some help from old answers I have managed to get a couple of windows hooks working (thanks to past contributors!) ... but now when I try to access files ... problems!

I tried something REALLY SIMPLE using TFileStream, like so...

  outFile := 'c:\test.out';
  with TFileStream.Create(transFile, fmCreate or fmShareCompat) do try
    Write('TEST', 4);

... but it crashes the application from which the hook event eminated ... No data written.

So I'm assuming there are some things I have to do to be a little more careful when attempting this stuff.

Now here comes the ugly part... Those with a faint heart, go no further ...

I am using Delphi 3 (because of certain BDE compatibility issues within the hooking app)

Wait, it gets worse!

The app being hooked is a 16-bit app.

I'm not sure if these a contributing factors, but heck if I knew why it was happening I'd just fix it and stop bothering you.
Question by:Hamlet081299
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 17

Accepted Solution

geobul earned 300 total points
ID: 7176869

Shouldn't it be 'outFile'?

with TFileStream.Create(outFile, fmCreate or fmShareCompat) do try

What kind of hook are you using?

Regards, Geo
LVL 20

Expert Comment

ID: 7176913
You can't write a string like that. This should work:

Write(pchar('TEST')^, 4);

Regards, Madshi.

Author Comment

ID: 7177314
oh shoot ... could be just a nasty cut and paste error ...

Originally I was reading some data from transfile and writing it to outfile.  When I had problems I simplified, but completely missed that booboo.

I'll check it out tomorrow --- Geo might have just earned himself the easiest 300pts ever!

And Madshi ... yes you can write a string like that.  Delphi easily handles literal constants as PChars.  I'm not sure why you would think that wouldn't work.  

Ironically when I got the email notification, I thought "Aha, Madshi is onto it, all my problems will be solved..."  ;-)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 20

Expert Comment

ID: 7177347
Ha, you got me!!   :-)   That really works. I thought it doesn't work because "Write(strVar, Length(strVar))" doesn't work, either. But it seems that once more I underestimated Delphi's intelligence...

Regards, Madshi.
LVL 17

Expert Comment

ID: 7177426
If it's the problem then you are right - easy. You just pasted the proper piece of code here, short but sufficient. Sometimes such errors are so difficult to find.

About 'Write': I didn't see that! If I had seen it I would have said the same as Madshi did. New things to learn every day.

Regards, Geo

Author Comment

ID: 7178681
Yeah, it's handy with constants that generally Delphi makes a smart decision about how to treat it.

I just tried it out and found it was just a stupid cut and paste!!!

Thanks Geo
LVL 17

Expert Comment

ID: 7179473
Thanks a lot. The pleasure was mine.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Base1 Encode/Decode 3 100
enhance the following code 3 53
Delphi: barcode reading on android platform 1 99
Browsing a TTreeView in Delphi 5 47
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question