?
Solved

Memory mapping problem

Posted on 2007-09-28
11
Medium Priority
?
687 Views
Last Modified: 2008-01-09
Well, I have an app and a dll, the dll sends info to the app and the app processes it. it works fine but now i want to make my app formless and im facing a problem, here is the code:

APP:
var
         Fx     : THandle;
     Pr      : ^Integer;
(...)

   Fx:=CreateFileMapping( $FFFFFFFF, nil, PAGE_READWRITE,  0, SizeOf(Integer), ' test');

    Pr:=MapViewOfFile(Fx,FILE_MAP_WRITE,0,0,0);

    Pr^ :=Handle; //Here

DLL
var
  Fxx    : THandle;
  Prr   : ^Integer;
(...)
   Fxx:=OpenFileMapping(FILE_MAP_WRITE,False,'test');
   if Fxx<>0 then
   begin
     Prr:=MapViewOfFile(Fxx,FILE_MAP_WRITE,0,0,0);
     PostMessage(Prr^,CM_KSA_IHP,wParam,lParam);
     UnmapViewOfFile(Prr);
     CloseHandle(Fxx);
   end;

That works fine but I dont know how to replace this line: " Pr^ := Handle;" , that doesnt work in a formless application. I tried declaring a THandle var to replace that but doesnt work, i guess it needs the right handle of the window so the dll know where to send the messages...
Is there any way i can get this to work without a form? T_T
0
Comment
Question by:lobo_estepario
  • 5
  • 3
  • 2
10 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 19977539
I'm not sure if I understand you completely,

but did you try declaring a THandle, and doing AllocateHwnd()?

var Handle: THandle;
begin
 Handle := AllocateHwnd();
end;


don't forget to DeallocateHwnd() when you're done.
0
 

Author Comment

by:lobo_estepario
ID: 19977601
The thing is that I cant use Pr^ := Handle; on a formless application. And the dll needs the handle of the form of my app to send the info -> PostMessage(Prr^,CM_KSA_IHP,wParam,lParam);

About your suggestion, i tried to use it but i noticed that function is in the forms unit. Im working on a formless app, like this:

program bla;
uses windows;
const
     WM_USER             = $0400;
   CM_KSA_IHP  = WM_USER + $1000;
var
         Fx     : THandle;
     Pr      : ^Integer;
     HandleDLL      : THandle;

begin
   Fx=CreateFileMapping( $FFFFFFFF,
                               nil,
                               PAGE_READWRITE,
                               0,
                               SizeOf(Integer),
                               'test');

    Pr:=MapViewOfFile(Fx,FILE_MAP_WRITE,0,0,0);
    Pr^ :=¿?¿?; //cant use Pr^ := Handle;
.
.
.
end.

I want to know if i can somehow replace that last line or if just cant work without a valid window handle.

thanks though.
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 600 total points
ID: 19977986
the fact that the AllocateHwnd() function is in the unit forms does not mean that you cannot use it from a formless application.
In fact, AllocateHwnd() is a direct api call.

See this quote from the help on this call: "Call AllocateHWnd to create a window that is not associated with a windowed control. Typically, this method is used to create non-visual windows that respond to messages but that do not appear in the user interface. For example, the TTimer component uses this method to create a window that responds to timer messages from Windows."
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:lobo_estepario
ID: 19982391
Yup you are right, and actually i read that function got moved to class unit. I had never used that function b4, but i googled it and i found a lot of examples of what i tried to do. but i still have some problems adapting the code to use it on a formless app. i need some hints here:
I cant use the function with no parameters like this (), i need a TWndMethod there. I checked some websites and found that thats the window procedure.

i read this website:
http://www.delphidabbler.com/articles?article=1

i need some help to adapt the code to a formless app. usually i would check for messages like this:

       while GetMessage(Msn, 0, 0, 0) do
        begin
           if (Msn.message = wm_hotkey) and (msn.wparam=11 then
           begin
  //do stuff
      end     else
          begin   .
              TranslateMessage(Msn);
              DispatchMessage(Msn);

           end;
        end;

     end.

im not sure im making my self clear. its not actually a memory mapping problem. the problem was that i didnt have a window handle and the dll couldnt send the messages. now i can use the function allocatehwnd but i dont know how to use the parameter. the demo on that website works fine but i dont know how to use or replace this procedure
procedure TMyClass.WndMethod(var Msg : TMessage);
var
  Handled: Boolean;
begin
  // Assume we handle message
  Handled := True;                      
  case Msg.Msg of
    WM_SOMETHING: DoSomething;          
      // Code to handle a message
    WM_SOMETHINGELSE: DoSomethingElse;  
      // Code to handle another message
    // Handle other messages here
    else
      // We didn't handle message
      Handled := False;              
  end;
  if Handled then
    // We handled message - record in message result
    Msg.Result := 0
  else
    // We didn't handle message
    // pass to DefWindowProc and record result
    Msg.Result := DefWindowProc(fHWnd, Msg.Msg,
      Msg.WParam, Msg.LParam);
end;

to work with my formless app T_T
0
 
LVL 7

Assisted Solution

by:Cynna
Cynna earned 600 total points
ID: 19991528
Is this of any help?

procedure TMyClass.WndMethod(var Msg : TMessage);
var  Handled: Boolean;
begin
  Handled := True;                      
  // -------- here are the messages you handle in your "GetMessage(Msn, 0, 0, 0)" loop --------
  if (Msg.Msg=wm_hotkey) and (Msg.WParam=11) then
  begin
       //do stuff
  end
  else Handled := False;              
  // ------------- we are finished, cleanup -------------------
  if Handled then
    // We handled message - record in message result
    Msg.Result := 0
  else
    // We didn't handle message
    // pass to DefWindowProc and record result
    Msg.Result := DefWindowProc(fHWnd, Msg.Msg,
      Msg.WParam, Msg.LParam);
end;
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19996930
how far did you get, and I also wonder, why do you use mapped memory in the first place?
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20584844
I suggest to split the points between Cynna and myself since we both put in effort to give constructive help.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 20585011
Agreed MerijnB - actually, based on the authors last comment, I think he now has working solution.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20585179
too bad he didn't tell us...
0
 
LVL 7

Expert Comment

by:Cynna
ID: 20585741
Yeah...oh well, maybe he simply moved on... A few years back, when I had more time to participate in ExEx, these kind of situations were kind of frustrating to me, but now...well - oh well... ;)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

809 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