Memory mapping problem

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
lobo_esteparioAsked:
Who is Participating?
 
MerijnBConnect With a Mentor Sr. Software EngineerCommented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
lobo_esteparioAuthor Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
lobo_esteparioAuthor Commented:
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
 
CynnaConnect With a Mentor Commented:
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
 
MerijnBSr. Software EngineerCommented:
how far did you get, and I also wonder, why do you use mapped memory in the first place?
0
 
MerijnBSr. Software EngineerCommented:
I suggest to split the points between Cynna and myself since we both put in effort to give constructive help.
0
 
CynnaCommented:
Agreed MerijnB - actually, based on the authors last comment, I think he now has working solution.
0
 
MerijnBSr. Software EngineerCommented:
too bad he didn't tell us...
0
 
CynnaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.